Opening a Word Document via a macro

M

Matthew McManus

Can anyone provide me with some code for a macro to open a wor
document.

I have excel2000, and word 2000, running on xp.

I have tried what has been suggested previously, something like:

Sub WDTest()
Dim WD as Object

Set WD = CreateObject("Word.Application")

WD.Documents.Open "C:\My Documents\WordDoc.Doc"
End sub

but this has caused me many problems. I get the error message tha
"WordDoc.doc" has been locked for editing by me. I have tried it on
number of otherwise fine documents (before I run this macro, I coul
open them normally) After I have run the macro I can't access the
again unless I reboot. The same applies to the excel document I a
trying to run the macro from. Also I frequently get the error messag
that I need to close Microsoft Word/Excel, but it doesn't seem to b
open - again I need to reboot to fix the problem.

Any ideas would be greatly appreciated.

Matthe
 
D

Dave Peterson

I'm not sure what you do after this portion finishes running, but I'm betting
that you have multiple instances (some hidden) of word running.

Next time you get the error, close all of your versions of Word that you see.

copy this code into Notepad and save it as UnHideWord.VBS

Dim WD
On Error Resume Next
Set WD = GetObject(, "Word.Application")
If Err.Number = 429 Then
MsgBox "Word is not running"
Else
WD.Visible = True
End If
On Error GoTo 0
Set WD = Nothing

========
Do the same thing with this code, but call it UnHideXL.VBS

dim myXL
On Error Resume Next
Set myXL = GetObject(, "Excel.Application")
If Err.Number = 429 Then
msgbox "excel is not running"
else
myxl.visible = true
end If
On Error GoTo 0
Set myxl = nothing


========

Each time you run one of these, you'll either unhide a visible instance so you
can close it.

Repeat until you get the "...is not running" message.
 

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