Opening a Word Document using an Excel (2000) Macro?

E

ekreider

I am short on time.

Please help to put the code needed to open a Word Document from an Excel
Macro.
I know it's simple and I am sure you have posted this thousands of time, but
may time seems to be getting short to finish this project. Please help.

Thanks,

ekreider
 
J

Jon Peltier

This page lays some of the groundwork:

http://peltiertech.com/Excel/XL_PPT.html

The late-binding code to open a Word document from Excel is:

Function OpenWordDoc(sDocFullName As String) As Object
Dim wdApp As Object
Dim wdDoc As Object
Dim sDocName As String

' Reference instance of Word
On Error Resume Next
' Check whether Word is running
Set wdApp = GetObject(, "Word.Application")
If wdApp Is Nothing Then
' Word is not running, create new instance
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
End If
On Error GoTo 0

' Check whether document is open
sDocName = Mid$(sDocFullName, InStrRev(sDocFullName, "\") + 1)
On Error Resume Next
Set wdDoc = wdApp.Documents(sDocName)
If wdDoc Is Nothing Then
' Document not open, so open it
set wdDoc = wdApp.Documents.Open(sDocFullName)
If wdDoc Is Nothing Then
' Document couldn't be opened
' probably should notify the user
End If
Else
' Document is open, no action required
End If
On Error GoTo 0

Set OpenWordDoc = wdDoc
End Function

Call the function like this:

Dim MyDocument As Object ' Word.Document
Set MyDocument = OpenWordDocument("C:\Temp\MyDocument.doc")

The open document is referenced in the variable MyDocument. If you don't
need to reference the document in an Excel variable, call it like a sub:

OpenWordDocument "C:\Temp\MyDocument.doc"

- Jon
 
J

Jon Peltier

This page lays some of the groundwork:

http://peltiertech.com/Excel/XL_PPT.html

The late-binding code to open a Word document from Excel is:

Function OpenWordDoc(sDocFullName As String) As Object
Dim wdApp As Object
Dim wdDoc As Object
Dim sDocName As String

' Reference instance of Word
On Error Resume Next
' Check whether Word is running
Set wdApp = GetObject(, "Word.Application")
If wdApp Is Nothing Then
' Word is not running, create new instance
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
End If
On Error GoTo 0

' Check whether document is open
sDocName = Mid$(sDocFullName, InStrRev(sDocFullName, "\") + 1)
On Error Resume Next
Set wdDoc = wdApp.Documents(sDocName)
If wdDoc Is Nothing Then
' Document not open, so open it
set wdDoc = wdApp.Documents.Open(sDocFullName)
If wdDoc Is Nothing Then
' Document couldn't be opened
' probably should notify the user
End If
Else
' Document is open, no action required
End If
On Error GoTo 0

Set OpenWordDoc = wdDoc
End Function

Call the function like this:

Dim MyDocument As Object ' Word.Document
Set MyDocument = OpenWordDocument("C:\Temp\MyDocument.doc")

The open document is referenced in the variable MyDocument. If you don't
need to reference the document in an Excel variable, call it like a sub:

OpenWordDocument "C:\Temp\MyDocument.doc"

- Jon
 
E

ekreider

Jon,

Thank you for your hard work.
I know it took you a little while to get all this wrtten down.

Thank you again.

Thanks,

ekreider
 
J

Jon Peltier

You're welcome. It actually only took about five minutes, by copying much of
it from the page I cited, then making appropriate changes.

- Jon
 
Top