-
Dec 4th, 2017, 04:02 PM
#1
Thread Starter
New Member
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
-
Dec 5th, 2017, 06:16 AM
#2
Junior Member
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
-
Dec 9th, 2017, 01:04 AM
#3
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
-
Mar 1st, 2018, 02:59 PM
#4
Thread Starter
New Member
Re: Unable to Activate Word Document from Excel VBA
Originally Posted by westconn1
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.
-
Mar 1st, 2018, 03:25 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|