[Solved] Tutorial 9: Trying to access a running word instance using VB

Oct 31, 2014 at 4:09 PM
Edited Oct 31, 2014 at 6:13 PM
Hi, i'm trying to write an AddIn for WinWord that should work with a currently running WinWord instance. I'm using Win 7 64bit, MS Office 2010, Visual Studio Express Desktop 2013 and VisualBasic.

I've tried to adapt the example from Tutorial 9: https://netoffice.codeplex.com/wikipage?title=Tutorial09_DE_VB
Dim wordType As Type = Type.GetTypeFromProgID("Word.Application")
Dim wordProxy As Object = Activator.CreateInstance(wordType)
oWordApplication = New NetOffice.WordApi.Application(Nothing, wordProxy)
I had to replace Excel.Application(null, excelProxy) by Application(Nothing, wordProxy) since Visual Studio didn't accept null as argument (i think null works in C#, but it seems not to be valid within VB). So, i tried Nothing as nearest equivalent.

But this doesn't seem to work. If i run the AddIn from within WinWord with a document open, i receive a TargetInvocationException when trying to access oWordApplication.ActiveDocuments:
Type:TargetInvocationException
Message:Ein Aufrufziel hat einen Ausnahmefehler verursacht.
Target:System.Object InvokeDispMethod(System.String, System.Reflection.BindingFlags, System.Object, System.Object[], Boolean[], Int32, System.String[])
Stack:   bei System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
   bei System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   bei NetOffice.Invoker.PropertyGet(COMObject comObject, String name, Object[] paramsArray)

Type:COMException
Message:Dieser Befehl ist nicht verfügbar, weil kein Dokument geöffnet ist.
Target:<Empty>
Stack:<Empty>
It seems that oWordApplication doesn't point to my running WinWord instance. In the task manager, i can see two WINWORD.EXE processes, so it seems the AddIn is starting a second WinWord instance (with no ActiveDocuments) instead of giving me access to my already-running WinWord instance.

What am i doing wrong? Any help would be greatly appreciated!
Oct 31, 2014 at 6:12 PM
First problem solved - i've changed to oWordApplication = NetOffice.WordApi.Application.GetActiveInstance(True) which gives me access to the running WinWord application. But now, when running my add-in, a second WINWORD.EXE process appears in the Task Manager, and it doesn't disappear if i exit WinWord. I've tried to call
oWordApplication.Dispose()
oWordApplication = Nothing
but this doesn't help. If i call oWordApplication.Quit() from within my AddIn, the winword window gets closed and the "first" WINWORD.EXE process disappears, but the second process keeps running. How do i close that process cleanly from within my AddIn?
Coordinator
Nov 2, 2014 at 10:17 PM
oops, of course null is not allowed in VB.
thx for pointing this out.

your addin is a COM addin or just an external application? (important)

it looks like you create a second word instance in your code, but i need more code to clearify the problem.

*Sebastian
Nov 3, 2014 at 6:50 AM
My AddIn is a COM AddIn, created with the NetOffice Developer Toolbox -> VS Project Wizard, Type "Automation AddIn". I'm trying to "translate" a WinWord VBA project to VB.

Regarding the word instances: I'll try to create a minimal example to track down the cause.
Nov 3, 2014 at 8:18 AM
Edited Nov 3, 2014 at 8:19 AM
Oh - sorry, i should have taken a look into Examples\Word\VB\COMAddin Examples\COMAddinRibbonExample first! This works perfectly for me. Thanks a lot for those example projects - they are really helpful. Problem solved!