-
Mar 1st, 2018, 03:06 PM
#1
Thread Starter
New Member
Excel VBA Does Not Execute Code in Order when Opening Word Document
I have been working on a macro to open a word document and write information to it. I am finding that for some reason, the word document is being opened before the macro even starts!
For example:
Code:
Sub ExampleCode()
msgbox "beginning of macro"
.
.
.
wDoc ="[file path]"
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open(wDoc)
objWord.Visible = True
objDoc.Application.Activate
objWord.ActiveWindow.View.ReadingLayout = False
When running the macro, the word document will open first, then I will get the message box. Any help?
-
Mar 1st, 2018, 03:20 PM
#2
Re: Excel VBA Does Not Execute Code in Order when Opening Word Document
then I will get the message box
try doevents after the msgbox, though it should not be needed
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
-
Mar 1st, 2018, 09:53 PM
#3
Thread Starter
New Member
Re: Excel VBA Does Not Execute Code in Order when Opening Word Document
Originally Posted by westconn1
try doevents after the msgbox, though it should not be needed
No change, same as before.
-
Mar 2nd, 2018, 03:14 AM
#4
Re: Excel VBA Does Not Execute Code in Order when Opening Word Document
i tested with your code, the msgbox shows first then i get file not found for the invalid wdoc when objword tries to open the document, this all seems correct to me, i do not know how anyone else can test with a problem that seems specific to you machine
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
-
Mar 16th, 2018, 09:11 AM
#5
Thread Starter
New Member
Re: Excel VBA Does Not Execute Code in Order when Opening Word Document
Originally Posted by westconn1
i tested with your code, the msgbox shows first then i get file not found for the invalid wdoc when objword tries to open the document, this all seems correct to me, i do not know how anyone else can test with a problem that seems specific to you machine
I figured this one out, it wasn't VBA related. A cell had conflicting hyperlinks... I fixed the hyperlink and the problem was solved.
Tags for this Thread
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
|