Getting Word Active Document File Name

Add-in Express™ Support Service
That's what is more important than anything else

Getting Word Active Document File Name
Getting Word Active Document File Name 
Dan Ewing




Posts: 6
Joined: 2007-12-10
Hello Everyone,
I have been using VB.Net for about 7 months now. I am making a program. part of it involves finding the active document name. I want this code written in a form that is separate from the Add In Express COM object. I was wondering how you would do this. Code examples would greatly help.
Thanks for the help in advanced,
Dan
Posted 10 Dec, 2007 02:46:43 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Dan.

To get the active instance of Word you can use the Marshal.GetActiveObject method. Then you can get the active document using the ActiveDocument property of Word application.

Dim wordApp As Microsoft.Office.Interop.Word.Application
Dim doc As Microsoft.Office.Interop.Word.Document

wordApp = Marshal.GetActiveObject("Word.Application")
doc = wordApp.ActiveDocument

......

Marshal.ReleaseComObject(doc)
Marshal.ReleaseComObject(wordApp)
Posted 10 Dec, 2007 08:00:37 Top
Dan Ewing




Posts: 6
Joined: 2007-12-10
Thank you very much Sergey. Will this allow for compatibility between different versions of word? say word 2000 to word 2007. Also would I have to add the word 2003 COM object as I usually would? Otherwise i get the following error:
Microsoft.Office.Interop.Word.Application' is not defined.

Thanks again.
Dan
Posted 10 Dec, 2007 11:48:49 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Dan, the code above will work for all versions of Word.
Yes, you need to use PIAs for Word 2000 or above to compile the code.
Posted 10 Dec, 2007 12:01:02 Top