Obtaining Active Document Name from WORD for Excel procedure

T

Tim Childs

Hi

I want to return the name of the active document from WORD from within an
Excel macro i.e. the macro in the workbook interrogates WORD to find the
name of the currently active document.

Any help gratefully received

Thanks

Tim
 
C

Chip Pearson

Try the following:

Dim WordApp As Object
Set WordApp = GetObject(, "Word.Application")
If Not WordApp.ActiveDocument Is Nothing Then
MsgBox "Active Document: " & WordApp.ActiveDocument.FullName
End If

Change FullName to Name if you don't want the path information.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
N

Nick Hodge

Tim

No error checking or fancy stuff, but here is the theory. Uses early
binding so a reference to the word object lirary with be necessary via
tools>references in the VBE

Sub getWordDocName()
Dim wd As New Word.Application
Dim wddoc As Word.Document
Set wd = Word.Application
Set wddoc = wd.ActiveDocument
MsgBox wddoc.Name
End Sub

--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
www.nickhodge.co.uk
(e-mail address removed)
 
T

Tim Childs

Chip, Nick

Many thanks for the response including the reference in the Tools Library -
I would not have spotted this otherwise

best wishes

Tim
 
N

NickHK

Tim,
Bear in mind that if you have more than one instance of Word, GetObject will
you one instance essentially randomly.
So it would depend if this is a concern for you.

NickHK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top