How do I set up a COM Server interface with Excel? - excel

Versions
Excel 2003Windows XP SimaPro 7.3.0 Developer Version Using a Work computer but was made administrator on this machine Libraries referenced in Excel/VBA: Visual Basic for Applications; Microsoft Excel 11.0 Object Library; OLE Automation; Microsoft Office 11.0 Object Library; Microsoft Forms 2.0 Object Library; COM+ 1.0 Admin Type Library; COM MakeCab 1.0 Type Library; COM+ Services Type Library; SimaPro Library Me: Beginner
What I'm trying to do
I am using a program called SimaPro that stores databases of "Life Cycle Analysis" information. The program has built in COM interface functionality. The program states that it does, indeed support Excel/VBA (but it doesn't specify versions).
I am trying to connect this program and/or COM server to excel so that I can interact with the information through excel.
What I've done
I've done the procedure they list:
-Open SimaPro
-Register COM Server
-Then I pasted the below code into VBA and tried to run it. This code is the sample code provided by the software company, I edited only the SP.Server, SP.Alias, SP.Login, and SP.OpenProject fields (below is as edited).
What Happened
Run-time Error: '-2147418113 (8000ffff)':
Automation Error
Catastrophic Failure
Question(s)
-Is the server name right? I've been reading a little on COM servers and I don't know if the way I put it in is in the right "form"
-Could it have something to do with certain registered/unregistered DLLs? I've worked with the company's IT people, and software programmers. None of them were very familiar with COM but one person suggested the DLLs might be the issue.
Thanks for your help!!
Here is the code that I'm inputting:
Sub CreateProcess()
Dim SP As SimaProServer
Dim PC As Process
Dim PC2 As Process
Dim PL As ProcessLine
Dim Param As ParamLine
Dim Subs As Substance
Set SP = New SimaProServer
SP.Server = "Local Server"
SP.Alias = "C:\Documents and Settings\All Users\Documents\SimaPro\Database\"
SP.Database = "Professional"
SP.OpenDatabase
SP.Login "", ""
SP.OpenProject "PROJECT", ""
' Not project's actual name, not allowed to state name of project
SP.CreateSubstance "Air", Subs
Subs.CASNumber = "4-5-13"
Subs.Name = "Some substance"
Subs.DefaultUnit = "kg"
Subs.Update
SP.CreateProcess ptMaterial, PC
Set PL = PC.AddLine(ppProduct, -1)
PL.ObjectName = "Steel 2"
PL.UnitName = "kg"
PL.Amount = "2"
PL.Comment.Add ("My new created process")
PL.CategoryPath = "Chemicals\inorganic"
PC.Update
' create second material process Case
SP.CreateProcess ptMaterial, PC2
Set PL = PC2.AddLine(ppProducts, 0)
PL.ObjectName = "Case 2"
PL.UnitName = "kg"
PL.Amount = "10"
Set Param = PC2.AddParamLine(ptInputParameter, -1)
Param.Name = "A"
Param.Value = "2,3"
' add input from Steel
Set PL = PC2.AddLine(ppMaterialsFuels, -1)
' input from steel
PL.SetProduct "Introduction to SimaPro 7", ptMaterial, "Steel 2"
PL.Amount = "8"
PL.UnitName = "kg"
Set PL = PC2.AddLine(ppAirborneEmissions, -1)
' input from steel
PL.SetSubstance "Some substance", ""
PL.Amount = "A+1"
PL.UnitName = "kg"
PC2.Update
SP.Logout
SP.CloseDatabase
Set SP = Nothing
End Sub

Given that this was more than one year ago. I'm assuming you got this working. If you haven't yet, I might know what the root cause might be.
I used to get the same error and from your changes for server, alias and login, I was able to make it run. One thing that is different is that you have changed the name of the project to "Project" from "Introduction to SimaPro 7". I honestly have zero (not being humble here) VBA knowledge. So, I'm speculating that there is no project names "Project" to open. I'm not sure if VBA would create a project automatically, if it can't find it. You can either try creating a project named "Project" or just rename it back. I am interested to see if that worked.

Automation Error usually means that there was a problem within the COM library you try to use. As it is a run-time error, it could be something very stupid, as missing parameter or wrong path or access rights. In my opinion, it also means that the library is not very well designed.
As you are not the author of the library you do not have many options. You can try to contact the vendor to get more documentation. You can also pray that the designer thought about logging - check the event log; if you are lucky you may find something interesting there.
Answering your first question, if you referenced the library and the code compiles - that means that you did everything right there.

Related

UI automation with excel

I am new to UI Automation. In my current organisation I was tasked with making an automated tool using GUI(Graphics User Interface) screen reading, but it is not working perfectly with other my colleague's machine because of a difference in screen resolution.
I watched this link on you-tube to try and understand UI Automation with excel, but I can't find much on this topic anywhere else.
Can anyone direct me toward resources on UI Automation? I Would like to know where I can learn it, read about it, and how to implement it with Excel.
Thanks in advance I really appreciate if anyone could help me.
UIAutomation from Microsoft is very powerfull and works well with windows 7, 8, 10 also from visual basic for applications (32 and 64 bits) and can be handy used to do some nice GUI Automation without expensive tools.
Make sure in VBA reference you have UIAutomationCore.Dll references (and weird enough sometimes on some computers you have to copy this to your documents folder)
Below you can see 2 base examples but as MS Automation is a huge library for all routines you can read a lot on MSDN for full documentation.
I use the MS UIA routines in AutoIt and in VBA
For AutoIt its shared over here
https://www.autoitscript.com/forum/topic/153520-iuiautomation-ms-framework-automate-chrome-ff-ie/
For VBA I do not have a standard library but someone did a try with
this
https://github.com/mhumpher/UIAutomation_VBA
Option Explicit
Sub test()
Dim c As New CUIAutomation
Dim oDesktop As IUIAutomationElement
Set oDesktop = c.GetRootElement
Debug.Print oDesktop.CurrentClassName & vbTab & oDesktop.CurrentName & vbTab & oDesktop.CurrentControlType
End Sub
'Test uia just dumps all windows of the desktop to the debug window
Sub testUIA()
Dim allEl As IUIAutomationElementArray 'Returns an element array with all found results
Dim oElement As IUIAutomationElement 'Reference to an element
Dim ocondition As IUIAutomationCondition
Dim i As Long
Dim x As New clsUIA
'Just reference the three mainly used properties. many more are available when needed
Debug.Print x.oDesktop.CurrentName & x.oDesktop.CurrentClassName & x.oDesktop.CurrentControlType
Set ocondition = x.oCUIAutomation.CreateTrueCondition 'Filter on true which means get them all
Set allEl = x.oDesktop.FindAll(TreeScope_Children, ocondition) 'Find them in the direct children, unfortunately hierarchies are strange sometimes
'Just reference the three mainly used properties. many more are available when needed
For i = 0 To allEl.Length - 1
Set oElement = allEl.GetElement(i)
' If oElement.CurrentClassName = "PuTTY" Then
Debug.Print oElement.CurrentClassName & oElement.CurrentName & oElement.CurrentControlType
' Debug.Print oElement.CurrentBoundingRectangle
oElement.SetFocus
DoEvents
Sleep 2000
' End If
Next
End Sub
Seems like you are doing the automation using the coordinates, which changes when you switch to other resolution. If this is the case, please automate your application using ID, Class, Xpath, CSS etc. This link will help you in that: http://www.guru99.com/locators-in-selenium-ide.html
For automation using Excel, please look into the following link:
http://www.guru99.com/all-about-excel-in-selenium-poi-jxl.html
create clsUIA class then insert this code
'clsUIA with some logic like
'start by add the following code
Dim c As New CUIAutomation
Public oCUIAutomation As New CUIAutomation
Function oDesktop() As IUIAutomationElement
Set oDesktop = c.GetRootElement
End Function

Unable to find the right excel file library reference error

Every time I open this Excel file I get this error which is in screenshot 1 and screenshot 2.
When I click OK it takes me to code windows and asks me to select library.
I tried everything to make it work but no luck.
Screenshot one and screenshot two:
Make sure that these references are in this Priority order.
HTML and Internet Controls need to be above OLE Automation.
If you are still having this issue after this, run a Repair on the Office install and it refreshes the .DLL files that may have been corrupted.
Update: 7/6/2017
The pervious answer above will re-associate the references and allow the script to compile.
However, there is a better way.
This issue occurs when the VBA script is shared to other computers that are not identical platforms(OS and MS Office versions) and use early binding. Early binding reduces latency and is the correct way when only intended for one computer.
The answer is to use late binding format in the script and not select any references. Change any data type objects other than object back to object and use the following format:
Sub Late_Binding()
Dim IE_App_obj As Object
Dim MyShell_obj As Object
Dim IE_Window_obj As Object
Dim Windows_cnt As Long
Dim x_cnt As Variant
Dim HTML_Element_obj As Object
Set IE_App_obj = CreateObject("InternetExplorer.Application")
'Use IE_App_obj to Navigate to webpage and control it.'
Set MyShell_obj = CreateObject("Shell.Application")
'Use MyShell_obj to find an existing webpage and control it.'
Let Windows_cnt = MyShell.Windows.Count
For x_cnt = Windows_cnt - 1 To 0 Step -1
On Error Resume Next
If Instr(MyShell_obj.Windows(x_cnt).Document.Title,"WebPage_Title") > 0 Then
Set IE_Window_obj = MyShell_obj.Windows(x_cnt)
Exit For
End If
Next
Set HTML_Element_obj = IE_Window_obj.Document.getElementByID("ID_text")
End Sub
Latency will increase but so will stability.

Creating a MAPI Session in VBA

I need to send some eMails from an excel-sheet. I currently use Excel2010, but it should also work with 2013. I searched the net for a solution and found a function, which unfortunatly doesn't work for me.
Sub SendMail(Recepient As String, Address As String, Subject As String, Contents As String)
Dim mapi_session As MSMAPI.MAPISession
Dim mapi_messages As MSMAPI.MAPIMessages
'Set mapi_session = New MSMAPI.MAPISession
Set mapi_session = CreateObject("MAPI.Session")
...
At line Set mapi_session = New MSMAPI.MAPISession it throws an error, saying "Unable to create object by ActiveX component (I translated from german)". I did some more investigation and came across a thread that suggested to use CreateObject instead, but it still throws the same error.
I included the msmapi32.ocx beforehand.
I'm pretty new to VBA - any idea what the problem might be?
Thanks in advance,
Frank
CDO 1.21 (that is what "MAPI.Session" is) is no longer being installed by Outlook. You can download it from https://www.microsoft.com/en-us/download/details.aspx?id=3671, but keep in mind that it is no longer being developed or supported by Microsoft.
What do you do with CDO 1.21? You really need to change your code to use only the Outlook Object Model. You can also use Redemption (I am its author) - it's RDO family of objects is (just like CDO 1.21) an in-proc MAPI wrapper and its RDOSession object is similar to MAPI.Session in CDO 1.21 (but has quite a few extra goodies).
Try Set mapi_session = CreateObject("MSMAPI.MAPISession").

Excel: Detecting OLE Automation?

we have a third party program which uses OLE to write Data to Excel. Unfortunately, the programmers of that application do not understand that making excel invisible and hiding updates speed up excel enormously.
At this moment, large jobs take up to 15 minutes to complete which is quite annoying.
Therefore I want to write an Addin, which shuts down visibillity etc. when the program writes to excel. Is there a way to do that? It is the only application which uses this technique, so detecting COM / OLE Interaction would be sufficient.
Rather that have excel check to see if it is in the hands of OLE automation, it is possible to check windows for the name of the process that is runnning the OLE
something like
Function isRunning(ProcessName) As Boolean
Dim objWMIcimv2 As Object
Dim objList As Object
Set objWMIcimv2 = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2") 'Connect to CIMV2 Namespace
Set objList = objWMIcimv2.ExecQuery _
("select * from win32_process where name='" & ProcessName & "'")
If objList.Count > 0 Then
isRunning = True
End If
End Function
From there you can suppress screen updating pretty easily. I am making a guess that the third party application doesn't usually sit running idle on the users desktop. if it does then obviously this approach won't help you.
Code inspired from this link on terminating programs

Language independent way to get "My Documents" folder in VBA Excel 2003

I need a Language independent way to get "My Documents" folder in VBA Excel 2003.
What I have:
Public Function MyDocsPath() As String
MyDocsPath = Environ$("USERPROFILE") & "\My Documents\"
End Function
Because the program will be used in at least 2 lang MS Windows, and the "My Documents" name changes for each language.
Is there a way, or should I try to figure out the system lang and become specific?
This may suit:
Set WshShell = CreateObject("WScript.Shell")
strDocuments = WshShell.SpecialFolders("MyDocuments")
From: http://msdn.microsoft.com/en-us/library/0ea7b5xe.aspx
Although the special folder name is MyDocuments, it refers to the documents folder for several versions of Windows.
You may use "Documents", as the localized versions point to the same location.
' Application.PathSeparator can be used, but this
' is unlikely to work on non-Windows environments
MyDocsPath = Environ$("USERPROFILE") & "\Documents\"
(Seeing that this is a 10 years old question it may not have been the case back then. :)

Resources