Results 1 to 5 of 5

Thread: Unable to Activate Word Document from Excel VBA

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    10

    Unable to Activate Word Document from Excel VBA

    I am trying to open and activate a word document but am unable to. I get a subscript is out of range error. Here is my code:

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim objWord As Object
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    
    objWord.Documents.Open "C:\Users\...\Desktop\Prototype Request Log\Rename_Me_PR-XXXX.docx"
    Windows("Rename_Me_PR-XXXX.docx").Activate
    Any help would be appreciated

  2. #2
    Junior Member Johannah's Avatar
    Join Date
    Oct 2017
    Location
    Cologne
    Posts
    15

    Re: Unable to Activate Word Document from Excel VBA

    Following. I start to learn to code at university this month and would like to practice some simple code.
    "The science of today is the technology of tomorrow" - Edward Teller

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

    Re: Unable to Activate Word Document from Excel VBA

    probably should be

    Code:
    objword.documents("Rename_Me_PR-XXXX.docx").Activate
    ' OR
    objword.documents("Rename_Me_PR-XXXX.docx").windows(1).Activate
    EDIT:
    Windows("Rename_Me_PR-XXXX.docx").Activate, would work fine for code in a document in the same instance of Word, but as you create a new or additional instance of word to open your document the document you try to activate is not in the documents collection of the application (apparently excel) containing the code, either of the examples posted above should work, or even
    Code:
    objword.Windows("Rename_Me_PR-XXXX.docx").Activate
    Last edited by westconn1; Dec 9th, 2017 at 05:45 AM.
    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    10

    Resolved Re: Unable to Activate Word Document from Excel VBA

    Quote Originally Posted by westconn1 View Post
    probably should be

    Code:
    objword.documents("Rename_Me_PR-XXXX.docx").Activate
    ' OR
    objword.documents("Rename_Me_PR-XXXX.docx").windows(1).Activate
    EDIT:
    Windows("Rename_Me_PR-XXXX.docx").Activate, would work fine for code in a document in the same instance of Word, but as you create a new or additional instance of word to open your document the document you try to activate is not in the documents collection of the application (apparently excel) containing the code, either of the examples posted above should work, or even
    Code:
    objword.Windows("Rename_Me_PR-XXXX.docx").Activate
    Thanks! objDoc.Application.Activate also worked for me.

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

    Re: Unable to Activate Word Document from Excel VBA

    Thanks! objDoc.Application.Activate also worked for me.
    if there were more than one document open in that instance of word, then the result may be unpredictable
    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

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