Switching between 2 word documents via excel VBA - Do I have to use ActiveDocument?

keaden

New Member
Joined
Sep 29, 2010
Messages
26
Hello all,
I have a Userform that runs a macro that opens 2 separate word documents, a template letter and a letter with a few bookmarked text articles.

I have been trying to get the macro to select a specific bookmark in the articles document based on the selection from a drop down box in the Userform, copy that bookmark, make the template letter the active document and paste the copies bookmark article text into a bookmark in the letter template.

My question is: Is it necessary to have the macro switch between active Word documents to copy/paste text from one Word document to another or can you declare both documents as objects or documents and use the macro to copy/paste text between them without switching the active document?

(If code is needed I can post it)

Any help on this would be appreciated.
Thanks
K
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
If you set up references to both documents you should be able to copy/paste between the without activating, just as you would with 2 Excel workbooks/sheets.
 
Upvote 0
Hello again Norie thanks for the help.

I thought that was the case thanks for the confirmation.

I am having a bit of bother with the syntax though

I have this at the start

Code:
Dim DocA As Document
Dim DocB As Document

Maybe I should open the documents and set DocA & DocB as String instead?

I'm struggling with parsing the document names into the references (If that's the right terminology)

would it be something like

Code:
Set DocA = Documents("Articles.docx")
Set DocB = Documents("Letter1.docx")

Would the references need the full file path and name if both the documents are already open?

Also I'm assuming that copying and pasting the bookmark from one document to the other would be something like:

Code:
DocB.Bookmarks("Text1").Text = DocA.Bookmarks("Article1").Text

Sorry for all the questions I'm just looking to copy and paste some pre formatted text into a letter template but maybe I'm over complicating things.

Anyway thanks again for the help
K
 
Upvote 0
Do you have a reference to the instance of Word you are working with?


If you did this is the sort of code you could use to open the 2 documents and set references to them.
Code:
Dim wrdApp As Object ' Word.Application
Dim wrdDocArt As Object ' Word.Document
Dim wrdDocLtr As Object ' Word.Document

    Set wrdApp = CreateObject("Word.Application")

    Set wrdDocArt = wrdApp.Documents.Open("C:\FullPathToFile\Articles.docx"

    Set wrdDocLtr = wrdApp.Documents.Open("C:\FullPathToFile\Letter1.docx"
 
Upvote 0
Norie,
The references I have been using are:

Code:
    Dim objWord As Word.Application


    Dim objDoc As Word.Document


    Dim DocA As Object ' adapted from your previous reply


    Dim DocB As Object ' adapted from your previous reply

The relevant code from the macro is

Code:
If Me.Letter1.Value = True Then 'This fires if the correct radio button is selected in the userform


                Set objWord = New Word.Application


                objWord.DisplayAlerts = False

                Set DocA = objWord.Documents.Open(ActiveWorkbook.Path & "\Letters\Letter1.docx") 'Adapted from your previous reply


                objWord.Visible = True 'this needs to be TRUE so the final document can be seen before saving


                objWord.Selection.GoTo What:=wdGoToBookmark, Name:="InsertPoint" ' goes to insertpoint bookmark where text is to be copied


and

Code:
                If Me("Combo" & y).Text = "Select Article" Then 'This is placeholder text in the combobox control that will be used to select which article to copy and insert into the letter
                    
                    Set objWord = New Word.Application


                    objWord.DisplayAlerts = False


                    Set DocB = objWord.Documents.Open(ActiveWorkbook.Path & "\Forms\Articles.docx")


                    objWord.Visible = True 'If ActiveDocument is not going to be used this may be able to be set to FALSE but make sure to close the document when copy/pasting is done


                    DocB.Selection.GoTo What:=wdGoToBookmark, Name:="Article1General"


                    DocB.Selection.Copy


                    Documents(DocA).Activate 'trying to make the letter document active


'                    objWord.Selection.GoTo What:=wdGoToBookmark, Name:="InsertPoint" 'trying to confirm that the cursor is at the insertpoint bookmark in the letter document


                    objWord.Selection.PasteAndFormat wdPasteDefault 'trying to insert the copied bookmark text

Am I anywhere near close?
Cheers
K
 
Last edited:
Upvote 0
Are you opening the documents in different instances of Word?
 
Upvote 0
You don't need 2 instances of Word, in fact I've got a feeling copying between documents in separate instances could be a problem - might even need to get the clipboard involved.

Try opening one instance of Word at the top of the code and opening both documents in it.
 
Upvote 0
OK so I left the letter instance of word alone because I will need an instance of word to open in the macro but I changed the article opening code to this

Code:
                If Me("Combo" & y).Text = "Select Article" Then
                    
                    Set objDoc = New Word.Document
                    objWord.DisplayAlerts = False
                    Set DocB = objWord.Documents.Open(ActiveWorkbook.Path & "\Forms\Articles.docx")
                    objWord.Visible = True

and that's giving me one instance of word in the task manager.

So my next part of code is throwing up the Run-time error '438'

Code:
                    DocA.Bookmarks("InsertPoint") = DocB.Bookmarks("Article1General").Value

any ideas?
 
Upvote 0
Actually I got this to work

Code:
                If Me("Combo" & y).Text = "Select Article" Then
                    
                    Set objDoc = New Word.Document
                    objWord.DisplayAlerts = False
                    Set DocB = objWord.Documents.Open(ActiveWorkbook.Path & "\Forms\Articles.docx")
                    objWord.Visible = True
                    DocB.Bookmarks("Article1General").Range.Copy
                    DocA.Bookmarks("InsertPoint").Range.Paste
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top