Results 1 to 2 of 2
  1. #1
    century
    Guest

    Saving docs (Word/97)

    I have a mailmerge coded in VBA in access and via automation I am trying to close the main and merged documents and also saing the merged documents to a specific name. I get an automation error when I try and do this. Does anybody have some code that will do this?

    The code I'm using is:

    Public Function MergeQuotes()

    ' Declare the Word object
    Dim objWord As Word.Document, x As String, i As Integer, y As Variant

    ' Set the path for the main document to mail merge to
    Set objWord = GetObject("G:LicensesQuotelevel.doc")

    ' Set the mail merge data source
    objWord.MailMerge.OpenDataSource _
    Name:="G:LicensesLicenses.mdb", _
    LinkToSource:=True, _
    Connection:="TABLE MSYQFL", _
    SQLStatement:="Select * from [MSYQFL]"

    objWord.MailMerge.Destination = wdSendToNewDocument
    objWord.MailMerge.Execute

    With ActiveDocument
    .SaveAs Filename:="G:LicensesMergedquotes.doc", Fileformat:=wdWordDocument
    .Close
    End With

    objWord.Close savechanges:=wdDoNotSaveChanges

    End Function

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Saving docs (Word/97)

    I always had the best luck using a Word template that was already a mailmerge document and linked to the datasource already. Then all I had to do was something like this:

    <pre>Public Function BatchMsg(DisplayMsg As Boolean, _
    ByVal TableName As String, _
    ByVal strTmpltNm As String, _
    Optional CCEmail, _
    Optional BCCEmail, _
    Optional AttachmentPath, _
    Optional strSubject) As Boolean
    ' modified 1/4/99
    On Error GoTo BatchMsg_err
    Dim blnOK As Boolean
    Dim varResult As Variant
    Dim strTemplateDir As String
    Dim strDocSrc As String
    Dim lngErr As Long

    blnOK = True
    strTemplateDir = ReturnWrdTemplatePath()
    strDocSrc = ReturnDocSrcPath()

    If blnOK Then
    Dim objWord As Word.Document
    On Error Resume Next
    Set objWord = CreateObject("Word.Document")
    If Err = 429 Then
    MsgBox "Word already open"
    GoTo BatchMsg_exit
    End If
    On Error GoTo BatchMsg_err

    ' Make Word visible and maximize
    objWord.Application.WindowState = wdWindowStateNormal

    objWord.Application.Visible = True
    ' Mail merge to e-mail
    objWord.Application.Activate
    Documents.Add Template:=strTemplateDir & "" & strTmpltNm, _
    NewTemplate:=False
    With ActiveDocument.MailMerge
    .Destination = wdSendToEmail
    .MailAsAttachment = False
    .MailAddressFieldName = "Email"
    .MailSubject = "Seminar Registration Confirmation"
    .SuppressBlankLines = True
    With .DataSource
    .FirstRecord = wdDefaultFirstRecord
    .LastRecord = wdDefaultLastRecord
    End With
    .Execute Pause:=True
    End With
    blnOK = True
    End If
    BatchMsg_exit:
    On Error Resume Next
    objWord.Activate
    objWord.Application.Quit (False)
    Set objWord = Nothing
    BatchMsg = blnOK
    Exit Function</pre>

    In this case, I was sending the merged document as email rather than creating a new Word document, but the basic principles are the same. I found that Word 97, at least, didn't like being surprised by its datasource, so it was easier to create a linked template and then open a new document based on it than to try to wrestle Word into accepting a new datasource in code. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Charlotte

Posting Permissions

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