Trouble Closing a Word Doc from Excel VBA


J

jaberwocky

I'm using office 2003.
I have an Excel spreadsheet that contains data that are mailmerged into
a word document. To simplify the process for others, I made an EXCEL
macro that flags the active row, launches the mailmerged word
document(which filters all except the flagged data), merges to a new
document (fixing merge fields), and then closes the original document.
Only the last step fails. This should be the simplest step, and I've
tried using quotes to no avail. What am I doing wrong?

Sub MakeReferral()
'save row number in holdrow variable
holdrow = Selection.Row
Cells(holdrow, 101).Value = 1
Dim oWord As Word.Application
Dim myDoc As Word.Document
Set oWord = CreateObject("word.application")
oWord.Application.Visible = True
Set myDoc =
oWord.Application.Documents.Open("p:\aftercare\referral.doc")
With myDoc.ActiveWindow
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
'close referral.doc
'*********************************************************************************
'this causes "runtime error '424'Object_required"
Windows(referral.doc).Close savechanges:=wdDoNotSaveChanges
End With
Set oWord = Nothing
'Clean up
Cells(holdrow, 101).Value = ""
End Sub
 
Ad

Advertisements

D

Doug Robbins - Word MVP

Replace

Windows(referral.doc).Close savechanges:=wdDoNotSaveChanges

With

myDoc.Close wdDoNotSaveChanges

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top