+ Reply to Thread
Results 1 to 2 of 2

Trouble Closing a Word Doc from Excel VBA

Hybrid View

  1. #1
    jaberwocky
    Guest

    Trouble Closing a Word Doc from Excel VBA

    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


  2. #2
    Doug Robbins - Word MVP
    Guest

    Re: Trouble Closing a Word Doc from Excel VBA

    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

    "jaberwocky" <[email protected]> wrote in message
    news:[email protected]...
    > 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
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1