Results 1 to 2 of 2

Thread: Help with Mail Merge to Word Doc

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    1

    Help with Mail Merge to Word Doc

    Hi,
    I'm trying to Mail Merge to a Word Document from VB.
    I have a Word Template with Mail Merge Fields.
    The problem I have is that I need to insert an HTML table into one of the Mail Merge Fields, however it fails whenever I try to input from the text file.
    I intended to create some VB code to find a particular Mail Merge Field and Insert the HTML in there.
    However I can not seem to get it to work.
    Here is the code I'm using:

    VB Code:
    1. Dim oApp As Word.Application
    2.     Dim oDoc As Word.Document
    3.  
    4.     On Error GoTo errHandler
    5.    
    6.     'Start a new document in Word
    7.     Set oApp = CreateObject("Word.Application")
    8.     Set oDoc = oApp.Documents.Open(s_Templates & TemplateFile)
    9.    
    10.    
    11.     With oDoc.MailMerge
    12.         .OpenDataSource Name:=s_TempFiles & "MergeData.txt" 'Specify your data source here
    13.         .Destination = wdSendToNewDocument
    14.        .Execute False
    15.     End With
    16.    
    17.  
    18.  
    19.     oDoc.Close False
    20.     oApp.Documents(1).SaveAs (s_Drive & Subject & ".doc")
    21.     oApp.Documents(1).Close
    22. ' Release references.
    23.   oApp.Quit
    24.   Set oDoc = Nothing
    25.   Set oApp = Nothing

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Help with Mail Merge to Word Doc

    Does s_TempFiles contain a valid filepath with an ending bakslash? "\".
    Whats the error message?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width