Using Python how do i handle a VBA API asking for type 'object' - api

I'm brand new to Python, I'm using 3.3. I began to explore COM, and what you can do with it is incredible for an engineer. I'm an engineering undergrad student, so I've got enough programming skills to get by, but I don't actually understand what I'm really doing.
The project I'm starting with has an excellent API help if I were writing in VBA, VB.NET, C# or C++ but I'm doing this project in Python and I'm two lines of code away from being done. I'm struggling with the second last "Method" of the program.
the API help offers this VBA description of the Method I'm trying to access.
For VBA:
Dim instance As (a handle?? to the class??? of this method, I know I've got a proper object)
Dim NElementNumber As Integer
Dim NStepNum As Integer
Dim DispPlane As Object (API says if you don't have a value to use, use Nothing, they literally type Nothing in in the provided examples)
Dim NUnits As Integer
Dim value As Object
value = instance.Method(NElementNumber, NStepNum, DispPlane, NUnits, ErrorCode)
and my python shell spits back to me
"Results = instance.Method(i,1,nothing,3,0)
File "<COMObject <unknown>>", line2, in Method
TypeError: Objects of type 'object' can not be converted to a COM VARIANT"
so I'm confused because, I have an idea what the problem is, obviously somehow i need to make the other program see that 'Nothing' entry as an object like it's expecting. But how do I go about doing that? what kind of Object are there many kinds of Objects?

Related

Accessing Dynamic Dims

I am creating what I believe is the way to create dynamic dims e.g.
Dim browser(DataGridView1.Rows(e.RowIndex).Cells(0).Value)
which basically is creating a something like Dim browser(1)
However I am trying to write a test button to call this browser by using:
browser1.load("http://google.com")
and I have even tried
browser(1).load("http://google.com"
I get the browser part underlined.
What is the correct way to reference them?
I'm not sure what the end goal is, but this construct:
Dim browser(1)
Is effectively the same as:
Dim browser(1) as Object
i.e. it's creating an array of Object of length 2, which is fine in that it can hold anything but makes your life pointlessly hard because you then have to cast the contents every time you want to do something with them; it's bad enough storing a single thing (such as a web browser widget) as an Object:
'this is bad
Dim browser as Object = New WebBrowserWidget
Because you have to cast it every time you use it (when Option Strict is On, which is really the only way you should ever program)
DirectCast(browser, webBrowserWidget).Load("http://google.com)
But to then add the indirection of storing it in an array:
'this is worse
Dim browser(1) as Object
browser(0) = New WebBrowserWidget
browser(1) = New WebBrowserWidget
And casting them to use them:
DirectCast(browser(0), webBrowserWidget).Load("http://google.com)
Just give these things a proper type when you declare them, or use type inference by providing something on the right hand side of an = that is a particular type:
'infer the type from the right hand side
Dim browser = New WebBrowserWidget
'or declare, give a type and create a new instance to assign to it
Dim browser as New WebBrowserWidget
Then it's a lot easier to use it, because Intellisense will know it's a browser and offer you help with the methods and properties, such as Load:
browser.Load("http://google.com")
If you code with Option Strict Off then you can store things in an object and just cross your fingers and hope you get everything right when youre coding, but it's like writing javascript - fumbling round eyes closed not knowing whether you got it right til it blows up at runtime:
Dim x as Object = New WebBrowserWidget
x.Load(...) 'don't get any intelllisense for this, but it'll work at runtime
x.Laod(...) 'dont get any compiler error even though this is a typo - it'll just explode at runtime

Remove namespace or classname from VB.Net when used in VBA [duplicate]

Base Reference: Ten Code Conversions for VBA, Visual Basic .NET, and C#
Note: I have already created and imported a *.dll, this question is about aliases.
Let's say the programmatic name of a Test class is TestNameSpace.Test
[ProgId("TestNamespace.Test")]
public class Test ...
Now, say a C# solution has been sealed and compiled into a *.dll and I'm referencing it in a Excel's VBE. Note: at this point I cannot modify the programmatic name as if the *.dll wasn't written by me.
This is in VBA : Instead of declaring a variable like this:
Dim myTest As TestNameSpace.Test
Set myTest = new TestNameSpace.Test
I'd prefer to call it (still in VBE)
Dim myTest As Test
Set myText = new Test
In C# you would normally say
using newNameForTest = TestNamespace.Test;
newNameForTest myTest = new NewNameForTest;
Note: Assume there are no namespace conflicts in the VBA project
Question: is there an equivalent call in VBA to C# using or VB.NET imports aliases?
Interesting question (constantly using them but never thought about their exact meaning). The definition of the Imports statement (same for using) is pretty clear: its only function is shortening the references by removing the corresponding namespaces. Thus, the first question to ask is: has VBA such a thing (namespaces) at all? And the answer is no, as you can read from multiple sources; examples: Link 1 Link 2
In summary, after not having found a single reference to any VBA statement doing something similar to Imports/using and having confirmed that VBA does not consider the "structure" justifying their use (namespaces), I think that I am in a position to say: no, there is not such a thing in VBA.
Additionally you should bear in mind that it wouldn't have any real applicability. For example: when converting a VB.NET code where Imports might be used, like:
Imports Microsoft.Office.Interop.Word
...
Dim wdApp As Application
the code would be changed completely, such that the resulting string will not be so long:
Dim wdApp As Word.Application ' Prefacing the library's display name.
I think that this is a good graphical reason explaining why VBA does not need to have this kind of things: VB.NET accounts for a wide variety of realities which have to be properly classified (namespaces); VBA accounts for a much smaller number of situations and thus can afford to not perform a so systematic, long-named classification.
-------------------------- CLARIFICATION
Imports/using is a mere name shortening, that is, instead of writing whatever.whatever2.whatever3 every time you use an object of the given namespace in a Module/ Class, you add an Imports/using statement at the start which, basically, means: "for all the members of the namespace X, just forget about all the heading bla, bla".
I am not saying that you cannot emulate this kind of behaviour; just highlighting that having an in-built functionality to short names makes sense in VB.NET, where the names can become really long, but not so much in VBA.
The answer is no: there is a built-in VBE feature that recognizes the references added to a project and creates aliases at run-time(VBE's runtime) if there are no name collisions
In case of name conflicts in your registry all . dots will be replaces with _ underscores.
» ProgId's (Programmatic Identifiers)
In COM, it is only used in late-binding. It's how you make a call to create a new object
Dim myObj = CreateObject("TestNamespace.Test")
» EarlyBinding and LateBinding
In early binding you specify the type of object you are creating by using the new keyword. The name of you object should pop up with the VBA's intellisense. It has nothing to do with the ProgId. To retrieve the actual namespace used for your object type - open Object Explorer F2 and locate it there
This article explain where the names come from in Early Binding Section
use the same link for When to use late binding
for MSDN Programmatic Identifiers section please see this

Late Binding & Type Issues In VB

I'm trying to run my code which was originally created using Visual Studio through another application where late bindings are disallowed and this option cannot be altered unfortunately. I am very new to programming in general and struggling to get my head around the issue. Here is the code im using in the invoke code stage:
Dim objIEShell As Object = CreateObject("Shell.Application")
Dim objIEShellWindows As Object = objIEShell.Windows
Dim objIEWin As Object
For Each objIEWin In objIEShellWindows
If InStr(objIEWin.LocationURL,"google")>0 Then
objIEWin.Quit
objIEWin = Nothing
End If
Next
The code simply closes all instances of Internet Explorer with "google" in the URL. This is the error message I get when trying to compile it:
Message: Error compiling code
error BC30574: Option Strict On disallows late binding. At line 2
error BC32023: Expression is of type 'Object', which is not a collection type. At line 4
From the research I've done so far I realise the first error message on line 2 is to do with the type difference between objIEShell and the Windows method. I think I have to convert objIEShell like this, CType(objIEShell,?), but I don't know the type of the .Windows method or how to find this out. Also any insight on how the fix the second error would be greatly appreciated as I'm not sure where to start with that one either.
This dates back to the wonky days when Microsoft still had plans to make Explorer behave like a web browser. Makes it pretty hard to arrive at the correct code, it is a combination of two separate COM components that don't have much to do with each other.
You need to first add two references to those components so the compiler understands the names. Use Project > Add Reference > COM tab and tick "Microsoft Internet Controls" and "Microsoft Shell Controls and Automation". That adds the Shell32 and SHDocVw namespaces.
Now you can write the code early-bound like this:
Dim objIEShell = New Shell32.Shell
Dim objIEShellWindows = CType(objIEShell.Windows, SHDocVw.IShellWindows)
Dim objIEWin As SHDocVw.WebBrowser
For Each objIEWin In objIEShellWindows
If InStr(objIEWin.LocationURL, "google") > 0 Then
objIEWin.Quit()
End If
Next
The CType() expression is probably the most unintuitive one, the Shell.Windows property is of type Object to break the dependency between those two components. The cast is the necessary voodoo to keep the compiler happy.

convert early binding to late binding without changing object type

This seems like a simple question but I after chasing forums for several hours I think it might be impossible.
I often want to convert a program from early binding to late binding. Usually, it is a vba, visual basic for applications, program that runs under Excel 2010 and windows 7 pro.
For discussion purposes, let’s pretend it is the following.
Sub EarlyBind()
' use IDE > Tools > references > and select “Microsoft Internet Controls”
Dim shellWins1 as shdocvw.shellwindows
Line1: Set shellWins1 = New SHDocVw.ShellWindows
MsgBox TypeName(shellWins1) ' this will display “IShellWindows”
' other code that expects to be working with an IshellWindows object …..
End Sub
In my experience, converting such a program to late binding is sometimes hard.
For instance, I found some forums that suggest I change it to
Set shellwins1 = createobject("Shell.applicaton")
But that creates a IShellDispatch5 object, not an IshellWindows object. That means I have to change other code to accommodate the new object type. And, of course I have to test that other code for subtle differences.
So, my goal is to find a general solution that will allow me to rewrite “Line1” to create the CORRECT object type with late binding. I also wish to avoid the need setting a reference to "Microsof Internet Controls. In other words, I want the code to look like this:
Sub LateBind()
Dim shellWins1 as object
Line1: Set shellWins1 = createobject(“xxxxxx.yyyyyy”).zzzzzz
MsgBox TypeName(shellWins1) ‘ this should display “IShellWindows”
….. other code that expects to be working with an IshellWindows object …..
End Sub
I know how to use the vba IDE to find the dll associated with the object. In this case the dll is Library SHDocVw C:\Windows\SysWOW64\ieframe.dll.
I have installed OleView and can find the associated IshellWindows “magic numbers” for the clsId, TypeLib, and Inteface (for instance the interface is 85CB6900-4D95-11CF-960C-0080C7F4EE85).
But, I don’t know how to convert them into a program id that can be used in line1 in the sample code posted above.
I hope someone here can help.
------ With MeHow's help, I now have the answer! ------
To switch 'set myObj = new xxxx.yyyyy' to late binding for arbitrary object types
Change set myObj = new xxxx.yyyyy
into set myObj = CreateObject("xxxx.yyyyy")
Very often that will work.
But, in the some cases, (e.g. "shDocVw.ShellWindows.") it gives error 429 ActiveX component cannot be created.
When that occurs I AM COMPLETELY OUT OF LUCK. It is impossible to use late binding with that EXACT object class. Instead I must find a substitute class that does approximately the same thing. (e.g. "Shell.Application").
Your short answer is
IShellWindows is an interface.
It
Provides access to the collection of open Shell windows.
Therefore
Take a look at the CreateObject() method.
Note:
Creates and returns a reference to a COM object. CreateObject cannot
be used to create instances of classes in Visual Basic unless those
classes are explicitly exposed as COM components.
IShellWindows is not exposed as a COM component so that's why there is no way to say CreateObject("SHDocVw.IShellWindows")
When you open your registry (regedit) and search for a key type in IShellWindows. If you find anything that means you've found your Prog ID and if you don't find anything it means that nothing like IShellWindows is registered as a prog Id therefore it would make sense to assume that you can't late bind IShellWindows
I bumped into your question trying to find something for myself. But I don't know if you have tried the following -
Set shellwins1 = createobject("Shell.Application")
MsgBox TypeName(shellWins1.Windows)
This answers your question for datatype. It prints IShellWindows for me. I'm not sure though if it could actually solve your purpose for latebinding meaning if this would be the object required though the datatype is what you need.
So, I would advise you to give it a try.
There is a slightly better approach outlined at https://www.experts-exchange.com/questions/28961564/How-to-find-the-class-id-of-an-arbitrary-object-Example-Set-x-CreateObject-New-1C3B4210-F441-11CE-B9EA-00AA006B1A69.html#a41743468.

Conversion from type 'String' to type 'String' is not valid

I'm in the process of porting an old excel addin that was writen in VBA to VB .NET. The Excel addin interacts with a number of external com objects. The code sorta looks like this:
Dim hurr as Object
Dim durr as String
hurr = CreateObject("COM Object")
durr = hurr.getString
What I'm trying to do is read the string from the COM object and get it in durr for use later in my program.
That second line results in the exception posted above. If I try casting with CStr/CType I get the same exception. The visual studio watch window reports the type of hurr.getString as a "System.__ComObject" whereas the VBA watch window reports the type as "Variant/Object/String". Microsoft.VisualBasic.Information.TypeName(hurr.getString) says the type is "String". Any ideas how I should go about getting this working?
Thanks!
This is ridiculous but I figured I would post the answer here for completeness. The solution was to add a pair of brackets to the end of hurr.getString
so the working code looks like this:
Dim hurr as Object
Dim durr as String
hurr = CreateObject("COM Object")
durr = hurr.getString()
The above code gave me the casting exception and for whatever reason it needs brackets to work here. I'm just going to add that working with late binding com objects is horrible and should be avoided at all costs.