activating a word doc

hearthhrob4gals

Board Regular
Joined
Aug 20, 2014
Messages
142
hi,

my below code works well when no word document is opened prior to running the macro. However, if there is already an opened word document, and then another word document is opened through the macro thn instead of executing data from the newly opened doc, it takes data from the previously opened doc.

Code:
Set wordapp = CreateObject("word.Application")
  wordapp.Documents.Open DOCFilenm
  wordapp.Visible = True
  wordapp.Activate
  Documents.Open(DOCFilenm).Activate
  Set wdDoc = wordapp.ActiveDocument
     ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
     ActiveDocument.MailMerge.OpenDataSource Name:=ExcelFilenm, Format:=wdOpenFormatAuto, Connection:="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ExcelFilenm" _
           , SQLStatement:="SELECT * FROM `'60 Days$'`", SQLStatement1:="", SubType:=wdMergeSubTypeAccess
     
     For x = 1 To LS60
         With ActiveDocument.MailMerge
            .Destination = wdSendToNewDocument
            .SuppressBlankLines = True
            With .DataSource
                .FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
                .LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
            End With
            .Execute Pause:=False
        End With
        ChangeFileOpenDirectory DirPath
        On Error Resume Next
        dt = ExcelWbk.Sheets("60 Days").Range("F" & x + 1).Value
        dt = Replace(dt, "/", "-")
        dt = Replace(dt, "\", "-")
        dt = Format(dt, "mm-dd-yyyy")
        On Error GoTo 0
        EmpId = ExcelWbk.Sheets("60 Days").Range("C" & x + 1).Value
        CostCenter = ExcelWbk.Sheets("60 Days").Range("B" & x + 1).Value
        CostCenter = Trim(Left(CostCenter, 4))
        CardMemName = ExcelWbk.Sheets("60 Days").Range("A" & x + 1).Value
        CardMemName = Left(Trim(CardMemName), 1)
        ExcelWbk.Sheets("60 Days").Range("Z" & x + 1).Formula = "=RIGHT(A" & x + 1 & ",LEN(A" & x + 1 & ")-FIND(""#"",SUBSTITUTE(A" & x + 1 & ","" "",""#"",LEN(A" & x + 1 & ")-LEN(SUBSTITUTE(A" & x + 1 & ","" "",""""))),1))"
        ExcelWbk.Sheets("60 Days").Range("Z" & x + 1).Copy
        ExcelWbk.Sheets("60 Days").Range("Z" & x + 1).PasteSpecial xlPasteValues
        CardMemName = CardMemName & " " & ExcelWbk.Sheets("60 Days").Range("Z" & x + 1).Value
        
 


  wdDoc.Close

problem is that it should consider DOCFilenm as active doc ,however if any xyz word doc was open before running the macro it consider that file as active instead of DOCFilenm
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Why not set a variable to reference the document you are opening and in the code replace ActiveDocument with that variable?

You've actually got a variable you could use for that, wdDoc.

Though you should change this code,
Code:
Documents.Open(DOCFilenm).Activate
  Set wdDoc = wordapp.ActiveDocument
to something like this,
Code:
Set wdDoc =wordapp.Documents.Open(DOCFilenm)
and replace ActiveDocument with wdDoc throughout the rest of the code.
 
Last edited:
Upvote 0
Why not set a variable to reference the document you are opening and in the code replace ActiveDocument with that variable?

You've actually got a variable you could use for that, wdDoc.

Though you should change this code,
Code:
Documents.Open(DOCFilenm).Activate
  Set wdDoc = wordapp.ActiveDocument
to something like this,
Code:
Set wdDoc =wordapp.Documents.Open(DOCFilenm)
and replace ActiveDocument with wdDoc throughout the rest of the code.





Thanks...now i am getting this error (System Error &H80010108 (-2147417848). (The object invoked has disconnected from its clients.
)...any idea how this can be fixed.

and this is the revised code

Code:
  Set wordapp = CreateObject("word.Application")  wordapp.Documents.Open DOCFilenm
  wordapp.Visible = True
  wordapp.Activate
  Set wdDoc = wordapp.Documents.Open(DOCFilenm)
  
     wdDoc.MailMerge.MainDocumentType = wdFormLetters
     wdDoc.MailMerge.OpenDataSource Name:=ExcelFilenm, Format:=wdOpenFormatAuto, Connection:="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ExcelFilenm" _
           , SQLStatement:="SELECT * FROM `'60 Days$'`", SQLStatement1:="", SubType:=wdMergeSubTypeAccess
     
     For x = 1 To LS60
         With wdDoc.MailMerge
            .Destination = wdSendToNewDocument
            .SuppressBlankLines = True
            With .DataSource
                .FirstRecord = wdDoc.MailMerge.DataSource.ActiveRecord
                .LastRecord = wdDoc.MailMerge.DataSource.ActiveRecord
            End With
            .Execute Pause:=False
        End With
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,045
Members
449,206
Latest member
Healthydogs

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