| Author |
Topic  |
|
|
milkbou
Taiwan
18 Posts |
Posted - 03/19/2004 : 04:30:46 AM
|
I tried to execute Origin C function by Excel VBA, but just don't know how? 1. I've tried to use run.LoadOC, but it didn't work. 2. I tried to add my Origin C file into Origin.ini, but it didn't work. 3. I tried to change the option of workspace to "Build on Startup", but it didn't work.
If I open Origin manually, it works, but if I use "GetObject" or "CreatObject", it didn't work. Can someone tell me how to make it work?
Following is the sample code:
Dim myobj As Origin.Application Dim graphInfo(1 To 15, 1 To 1) Set myobj = GetObject("", "Origin.Application") Dim bb As Boolean Dim strPath, strFile As String strPath = myobj.LTStr("system.path.program$") strFile = "sclinm\scBoxPlot.opj" strFile = strPath + strFile bb = myobj.Load(strFile) If (Not (bb)) Then strMsg = "Could not open Origin project: " + strFile MsgBox (strMsg) End If graphInfo(1, 1) = waferNo + 1 graphInfo(2, 1) = Sheets(1).Cells(siteNo + 12, 2) graphInfo(3, 1) = Sheets(1).Cells(siteNo + 13, 2) graphInfo(4, 1) = title
If Not myobj.PutWorksheet("Data1", graphInfo, 0, 0) Then Exit Sub If Not myobj.PutWorksheet("Data1", r1.Value, 0, 1) Then Exit Sub myStr = "run.LoadOC(""sclinm\OriginC\scBoxplot"",2)" If Not myobj.Execute(myStr) Then Exit Sub If Not myobj.Run() Then Exit Sub If Not myobj.Execute("DrawBoxPlots") Then Exit Sub If Not myobj.Execute("win -a graph1; clipboard graph1") Then Exit Sub End Sub |
|
|
easwar
USA
1965 Posts |
Posted - 03/19/2004 : 11:07:32 AM
|
Hi milkbou,
If your Origin C file is something you will use repeatedly, the simplest way to make this function available in each session is the following: 1> Go to code builder and right-click on the System branch of the tree on the left. 2> Select Add File and then navigate and add your Origin C file 3> Make sure your OC file compiles fine
Now, when Origin is launched (manually or via COM), your file will be compiled and ready with the rest of the files in the System branch, and so your functions in the file will be immediately accessible.
Then, using COM, you can issue commands such as: myObj.Execute("MyFunc");
If your function takes parameters, then you will need to build a string such as: "MyFunc(Par1Val,Par2Val...)" and then use that in the .Execute method.
As for run.LoadOC not working from your external application, I suspect the problem could be in the string formatting. Try using the string you build first manually from the script window in Origin to see if that works.
Easwar OriginLab
|
 |
|
|
milkbou
Taiwan
18 Posts |
Posted - 03/21/2004 : 8:32:27 PM
|
Hi easwar, I tried the simplest way, but it still can't work through COM. My function doesn't take any arg, it did work to open Origin manually and I don't know why? My origin version is OriginPro SR0 V7.5714.
Thanks!
|
 |
|
|
easwar
USA
1965 Posts |
Posted - 03/22/2004 : 12:31:10 PM
|
Hi milkbou,
One possibility is that the Origin application reference has not been added to your external application.
I presume you are calling Origin from Excel. Do the following: -> bring up VB editor in Excel -> go to the Tools->References menu, which then brings up a dialog Is there a box with a check mark that says "OriginLab Origin Application (version 7.5) Type Library" at the top? If not, scroll down the list and find this item and check the box against this item. Then your VB code that calls Origin will compile properly.
If you are building a VB project (and not using VB inside of Excel), then you need to look under the Project->References menu to make sure Origin application check box is checked as described above.
Now, I tried the following simple test. Can you follow my steps and then report back if it works or not? If it fails, can you tell me where exactly it fails?
1> I first created a simple Origin C function file called VBATest.c which has the following lines of code:
#include <Origin.h> // File to test VBA access // The following function will be called - it just prints message in the script window void VBATestFunc() { printf("Function was called successfully from VB!\n"); }
2> I saved this file under my \OriginC\ folder in the Origin EXE installation area with file name as VBATest.c 3> I tested this function manually in Origin to make sure it works 4> I then cleared my workspace by using the Code Builder menu command File->New Workspace 5> I went to the script window and typed: run.loadoc("vbatest"); vbatestfunc; and then I got the message: Function was called successfully from VB! So at this point the run.loadoc() command is working fine from script window. 6> I then launched Excel, added a button to one of the sheets, and brought up the VB editor 7> I made sure Origin application reference is all set as explained at the top of this message 8> I then put the following code under my button code section:
Private Sub CommandButton1_Click() Dim OriginApp As Origin.Application Set OriginApp = GetObject("", "Origin.ApplicationSI") OriginApp.Execute ("run.loadOC(""VBATest.c"");") OriginApp.Execute ("vbatestfunc") End Sub
9> I closed the VB editor and returned to Excel 10> I went to Code Builder in Origin and cleared my workspace using File->New Workspace command so that the VBAtest.c file is no longer part of my workspace 11> I went to Excel and clicked the button, and I got "Function was called successfully from VB!" in my script window. So my Excel button loaded and compiled my file, and then called a function from that file which printed the message in the script window.
Can you try the above steps and see if it works for you? If not, report back on where you fail?
Now, the earlier point I was making is that if you add your VBAtest.C file to the System folder of Code Builder, then Origin will always load and compile your file on start up. Then from Excel, you need not issue a run.loadOC command. You can just call the function.
To try the above method of system folder, do the following: 12> go to Code Builder in Origin, and start a new workspace 13> right-click on the System folder of the tree, and pick Add Files and navigate to and add the file VBATest.c 14> Click the Build button to make sure it builds file 15> Close the Origin application and launch it again 16> Go to the script window and type the command: vbatestfunc and you will get the message from the function , which shows that your function file automatically loaded and compiled. 17> Now modify the code behind your Excel button as follows:
Private Sub CommandButton1_Click() Dim OriginApp As Origin.Application Set OriginApp = GetObject("", "Origin.ApplicationSI") OriginApp.Execute ("vbatestfunc") End Sub
where the run.loadoc command has been taken out. Now if you click the button in Excel, you should get the same result.
Easwar OriginLab
|
 |
|
|
milkbou
Taiwan
18 Posts |
Posted - 03/22/2004 : 10:52:52 PM
|
Hi Easwar, I've tried the proposed-steps, and it works. I went to Code Builder in Origin and cleared the workspace using File->New Workspace command so that the VBAtest.c file is no longer part of my workspace, in this time, VBATestFunc shouldn't be valid in Script window, but it's a valid function in script window. It means that no matter the command "run.LoadOC("VBTest.c")" ran or not, CommandButton1_Click will be executed successfully.
But this is not the way I want. In my case, my VBA code will start Origin by COM, then I will load my OriginC function, and run it. At last, I'll close the Origin COM object. So my test is to compile and build my OC files to make sure everything works fine manually. Then close Origin and open Excel, execute VBA code to initialize Origin by COM, load my OC files, execute my functions, at last close COM object.
In my test, I make Origin visible by the following command: Dim OriginApp As Origin.Application Set OriginApp = GetObject("", "Origin.ApplicationSI") OriginApp.Execute ("doc -mc 1") myResult1 = OriginApp.Execute("run.loadOC(""VBATest.c"");") => myResult1 =true myResult2 = OriginApp.Execute("VBATestFunc") => myResult2 =false
If myResult1=true, then myResult2 should be true, too. I don't know what's wrong with this.
|
 |
|
|
easwar
USA
1965 Posts |
Posted - 03/23/2004 : 09:35:10 AM
|
Hi milkbou,
quote:
I went to Code Builder in Origin and cleared the workspace using File->New Workspace command so that the VBAtest.c file is no longer part of my workspace, in this time, VBATestFunc shouldn't be valid in Script window, but it's a valid function in script window. It means that no matter the command "run.LoadOC("VBTest.c")" ran or not, CommandButton1_Click will be executed successfully.
This is probably because you have added VBAtest.c to your system folder and so this file and the functions in it are always available in Origin (from script window etc). When you clear the workspace, look under the system folder to see if this file is there.
You should do (a) make this file part of your system folder so that it is always available OR (b) use run.loadoc to load this file when needed NOT BOTH.
If the file is part of system folder, issuing a run.loadoc command will do nothing since the file is already present in your workspace.
quote:
myResult1 = OriginApp.Execute("run.loadOC(""VBATest.c"");") => myResult1 =true myResult2 = OriginApp.Execute("VBATestFunc") => myResult2 =false
I tested and I get "true" for both myResult1 and myResult2.
Easwar OriginLab
|
 |
|
|
milkbou
Taiwan
18 Posts |
Posted - 03/24/2004 : 02:03:54 AM
|
Hi Easwar, Thanks for your patience, I removed my OC files from system folder and tried it again, then everything was working fine. But I got another problem, if I didn't execute "myobj.Execute ("doc -mc 1")" to make Origin visible, then it will be failure when executing "myobj.Execute("win -a graph1;clipboard graph1")". Do you know why? |
 |
|
|
easwar
USA
1965 Posts |
Posted - 03/24/2004 : 09:03:59 AM
|
Hi milkbou,
I tried copying graph to clipboard with making Origin visible, and not making Origin visible, and in both cases it worked fine for me. What do you mean by "failure when executing"? Do you not get the proper image in the clipboard, or does the .Execute method return an error?
Also, instead of issuing the script command "win -a graph1;clipboard graph1", you should instead use the CopyPage method. Please see Programming help file for sytnax - we will update our Excel example to use this new method instead of the script command.
Easwar OriginLab
Edited by - easwar on 03/24/2004 09:33:07 AM |
 |
|
|
milkbou
Taiwan
18 Posts |
Posted - 03/24/2004 : 8:57:50 PM
|
Hi Easwar, 1. I often got an error message "Automation Server throws an Exception" when executing "myobj.Execute("win -a graph1;clipboard graph1")", then Origin will be closed, I didn't know what's happened?
2. I'll try "System.CopyPage" utility and feedback my test results.
|
 |
|
|
milkbou
Taiwan
18 Posts |
Posted - 03/24/2004 : 9:51:27 PM
|
Hi Easwar, I've read the programming guide of copyPage, but I have no idea about how to use this command to copy graph to clipboard. Only properties information available in the help. |
 |
|
|
Iris_Bai
China
Posts |
Posted - 03/26/2004 : 06:18:45 AM
|
hi.milkbou: You can use the following sample code to test CopyPage method.The sample code will create an Graph page in Origin workspace and copy and paset to Excel. Example:
Private Sub CommandButton1_Click() 'Open a new instance of the Origin application Dim oApp As Origin.Application Set oApp = GetObject("", "Origin.Application") 'Create new GraphPage in Origin workspace Const ORIGIN_WINTYPE_GRAPH = 3 Dim name As String, realname As String name = "MyGraph" name = oApp.CreatePage(ORIGIN_WINTYPE_GRAPH, name, "Origin") 'Copy page from Origin Workspace using CopyPage method bb = oApp.CopyPage(name) If (Not (bb)) Then MsgBox ("Could not copy page") Exit Sub End If
'Now create a new chart and paste image With ActiveSheet.ChartObjects.Add _ (Left:=225, Width:=375, Top:=150, Height:=325) .Chart.Paste End With 'Set Modified flag so that Origin does not prompt for saving this current project oApp.IsModified = False oApp.Execute ("exit") End Sub
Edited by - Iris_Bai on 03/26/2004 06:19:42 AM |
 |
|
|
milkbou
Taiwan
18 Posts |
Posted - 03/28/2004 : 8:30:27 PM
|
Hi Iris, The sample code was tested on my computer, but got one problem when executing CreatePage and CopyPage. The error message is "Object doesn't support this property or method". I searched the help of Origin on-line programming help(Origin Automation Server References) and found no topics about CreatePage and CopyPage. My version is OriginPro 7.5 SR0 v7.5714.
|
 |
|
|
Iris_Bai
China
Posts |
Posted - 03/28/2004 : 10:40:30 PM
|
Hi milkbou,
The two automation server methods provided form OriginPro 7.5 Sr1 or maybe sr2. So you need to update your Origin by Help -> Check for Update... to get the newest version.
Iris |
 |
|
| |
Topic  |
|
|
|