Use of record excel macro and JChem functions

User 2a5f9cc733

10-10-2012 12:17:58

Whilst recording an excel macro I wanted to convert a smiles to a structure using the JChem Structure from Smiles function but  i find that  the action is not recorded - any ideas how to make this work?

ChemAxon bd13b5bd77

10-10-2012 14:39:08

Hi Chris,


because Excel can record only a Microsoft based macro but you can force the convert action from code.


Let me give you a code example shortly.


Viktor

ChemAxon bd13b5bd77

10-10-2012 17:24:20

Hi Chris,


we provide a Developer's Guide please check it:


http://www.chemaxon.com/jchem4excel/developerguide/welcome_topic.html


the generic  API routines for VBA, COM can be found here :


http://www.chemaxon.com/jchem4excel/developerguide/introduction.html


you can download VBA macro demo codes from here (VB and VBA examples subfolder in the ZIP file):


http://www.chemaxon.com/download.php?d=/data/download/jchem4xl/JChem_for_Excel_API_Guide.zip


 


Now I have taken it out for you 1 of these examples:


Public Sub ActionDemo()
Dim vaArray() As Variant
Dim sActionList As String
Dim lIndex As Long


' this is the correct interface you should use
Dim nativeInterface As Object
Dim objAddin As Office.COMAddIn


   ' error handling on
   On Error GoTo ERROR_HANDLER


   Set objAddin = Application.COMAddIns("JChemExcel.JChemExcelAddin")
   Set nativeInterface = objAddin.Object


   ' get them back from service
   vaArray = nativeInterface.GetAvailableActions()
   If (IsArray(vaArray)) Then
      For lIndex = LBound(vaArray) To UBound(vaArray)
         sActionList = sActionList & CStr(vaArray(lIndex)) & vbCrLf
      Next
   End If
   'Call MsgBox(sActionList & vbCrLf & "Executing the first action on the list ... ")
   Debug.Print sActionList & vbCrLf & "Executing the first action on the list ... "
   ' run action
  'Call nativeInterface.ExecuteAction("ConvertFromSMILESToStructureAction")
  


   ' skip error handling block
   Exit Sub


ERROR_HANDLER:
   Call MsgBox("Cannot demonstrate JC4XL actions! " & Err.Description, vbCritical)
End Sub


 


 

ChemAxon bd13b5bd77

10-10-2012 17:27:35

Continuing from the ActionsDemo code you can derive a small code snippet.


This is an answer to your problem:


Public Sub ConvertFromSmilesToStructureAction()


' this is the correct interface you should use
Dim nativeInterface As Object
Dim objAddin As Office.COMAddIn


   ' error handling on
   On Error GoTo ERROR_HANDLER


   Set objAddin = Application.COMAddIns("JChemExcel.JChemExcelAddin")
   Set nativeInterface = objAddin.Object


   ' run action
   Call nativeInterface.ExecuteAction("ConvertFromSMILESToStructureAction")


   ' skip error handling block
   Exit Sub


ERROR_HANDLER:
   Call MsgBox("Cannot demonstrate JC4XL actions! " & Err.Description, vbCritical)
End Sub


 


This will convert the content of the activecell to JCXL structure. Basically these 3 lines of code will do the conversion and can be put into your macro or into a routine that you macro can call:


 Set objAddin = Application.COMAddIns("JChemExcel.JChemExcelAddin")
 Set nativeInterface = objAddin.Object
 ' run action
 Call nativeInterface.ExecuteAction("ConvertFromSMILESToStructureAction")


If you have further questions please let me know.


Viktor