Results 1 to 11 of 11

Word Mail Merge and Name File (VBA Code Error)

Threaded View

  1. #1
    Registered User
    Join Date
    08-14-2007
    Posts
    27

    Word Mail Merge and Name File (VBA Code Error)

    Hey guys (and gals),

    I'm really frustrated, I had this working, lost my code, and now I can't figure out where I going wrong trying to get it working again. The code below opens Microsoft Word, uses mail merge, then outputs the Word Doc in PDF, and it names that PDF and saves it to a specified location...

    The first time I run the Macro it works perfectly, but the second time I run it it either crashes or Excel freezes on me. If it crashes and I hit debug, and then run the macro again without changing anything- it works perfectly. I'm really stumped here and frustrated.

    When I step through it...every time the mail merge runs it is creating a new document (IE "Letter1") and each subsequent run of the macro creates Letter2, Letter3, etc. I don't know why it's doing this. Originally the way it worked it just used the source Doc for mail merge without creating a new document...but I can't seem to get it working again.

    Lastly...as you can see in my IF Len(Dir function...if the file exists it creates another file with "-2" on the end of the file name. I would like to make it such that it continues this...(either up to -10 or infitinitely if possible) but my skills are limited so I just have "-1" and "-2". Thank you all for your time!

    Sub Merge()
    
        Dim wd As Object
        Dim wdocSource As Object
        Dim strWorkbookName As String
    
        On Error Resume Next
        Set wd = GetObject(, "Word.Application")
        If wd Is Nothing Then
            Set wd = CreateObject("Word.Application")
        End If
        On Error GoTo 0
    
        Set wdocSource = wd.Documents.Open("C:\Documents and Settings\christopher.kline\Desktop\Work Files\Projects\Pending\Automated Bank Memo\AIB Bank Memo.doc")
    
        strWorkbookName = ThisWorkbook.Path & "\" & ThisWorkbook.Name
        wdocSource.MailMerge.MainDocumentType = wdFormLetters
    
        wdocSource.MailMerge.OpenDataSource _
            Name:=strWorkbookName, _
            AddToRecentFiles:=False, _
            Revert:=False, _
            Format:=wdOpenFormatAuto, _
            Connection:="Data Source=" & strWorkbookName & ";Mode=Read", _
            SQLStatement:="SELECT * FROM `Data$`"
    
        With wdocSource.MailMerge
            .Destination = wdSendToNewDocument
            .SuppressBlankLines = True
            
            With .DataSource
            .FirstRecord = wdDefaultFirstRecord
            .LastRecord = wdDefaultLastRecord
            End With
            
            .Execute Pause:=False
        End With
    
        wdocSource.Close SaveChanges:=True
        wd.Visible = False
    
        Set wdocSource = Nothing
        Set wd = Nothing
    
        If Len(Dir("P:\LNL Finance 2008\BANK MEMOS\Created and Sent" & "\" & "BankTEST- " & Format(Date, "mm-dd-yy") & "-1.pdf")) Then
            ActiveDocument.ExportAsFixedFormat OutputFileName:="P:\LNL Finance 2008\BANK MEMOS\Created and Sent" & "\" & "BankTEST- " & Format(Date, "mm-dd-yy") & "-2.pdf", ExportFormat:= _
            wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
            wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
            Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
            CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
            BitmapMissingFonts:=True, UseISO19005_1:=False
        Else
            ActiveDocument.ExportAsFixedFormat OutputFileName:="P:\LNL Finance 2008\BANK MEMOS\Created and Sent" & "\" & "BankTEST- " & Format(Date, "mm-dd-yy") & "-1.pdf", ExportFormat:= _
            wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
            wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
            Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
            CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
            BitmapMissingFonts:=True, UseISO19005_1:=False
        End If
    
        ActiveDocument.Close (False)
        Word.Application.Quit
    
    End Sub
    Last edited by wealthistime; 04-14-2011 at 03:56 AM. Reason: SOLVED

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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