|
-
Jul 14th, 2009, 10:24 AM
#1
Thread Starter
New Member
[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
-
Jul 14th, 2009, 10:36 AM
#2
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 
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.
-
Jul 14th, 2009, 11:57 AM
#3
Thread Starter
New Member
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 )
-
Jul 14th, 2009, 04:52 PM
#4
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 15th, 2009, 02:42 AM
#5
Thread Starter
New Member
Re: Opening a word document via VBA
Thanks westconn1 works now
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|