Results 1 to 5 of 5

Thread: Excel VBA Does Not Execute Code in Order when Opening Word Document

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    10

    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?

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    10

    Re: Excel VBA Does Not Execute Code in Order when Opening Word Document

    Quote Originally Posted by westconn1 View Post
    try doevents after the msgbox, though it should not be needed
    No change, same as before.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    10

    Re: Excel VBA Does Not Execute Code in Order when Opening Word Document

    Quote Originally Posted by westconn1 View Post
    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
  •  



Click Here to Expand Forum to Full Width