[RESOLVED] Opening a word document via VBA
Hello
I am looking for help with a VBA macro I am writing in Excel. Basically I want to open a word document from inside an excel macro. I have tried:
Sub open1()
MsgBox "This form should be printed and taken to your local IT contact" & Chr(10) & Chr(10) & "Do not email this form to the team.", vbexclaimation, "L1"
On Error GoTo BadShow
Dim oWord As Object
Set oWord = CreateObject("Word.application")
oWord.Documents.Open "\\server\path\myfolder\forms\L1.doc"
oWord.Visible = True
AppActivate oWord
Set oWord = Nothing
Exit Sub
BadShow:
oWord.Quit
Set oWord = Nothing
End Sub
That I found by using google, but my document opens and instantly closes. I am a bit of a novice with these things so any help would be greatly appreciated
Re: Opening a word document via VBA
Thread moved to Office Development/VBA forum (note that the "VB Editor" in Office programs is actually VBA rather than VB, so the VB6 forum is not really apt)
Welcome to VBForums :wave:
I don't know what the problem is, but I can see why you aren't being explicitly told about it (and the document is closing).
It is happening like that because of the code you have got to deal with errors - it deliberately hides any error messages, and also closes Word.
To stop that from happening, comment out the line "On Error GoTo BadShow" by adding the ' character (or the word REM) before it.
You should then get a message telling you what the issue is, and a line of the code should be highlighted too.
Re: Opening a word document via VBA
Thanks for the help, I will try that when I get to work tomorrow and psot the results (in the right board this time ;) )
Re: Opening a word document via VBA
appactivate oword will most likely generate an error
appactivate needs a string of the caption passed to it, not an object as you are doing
try
appactivate "Word"
or
appactivate oword.name
Re: Opening a word document via VBA
Thanks westconn1 works now:)