Results 1 to 9 of 9

Thread: Merge field syntax problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Location
    Townsville, Qld, Australia
    Posts
    135

    Merge field syntax problem

    I am trying to insert a mail-merge field and I can't find the correct syntax.

    This is what I've got so far and it doesn't work.

    Code:
        With wrdDoc
    .Content.InsertAfter Format(MyDate, "dddd, d MMMM yyyy")
        .Content.InsertParagraphAfter
        .Content.InsertAfter "Dear "
        End With
    
            ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
            , Text:=" MERGEFIELD First"
    (The code works up to the "End with", it's the next line that is the problem.)

    I would be very grateful if someone knows the correct syntax. I've tried some other versions of this, also unsuccessfully.
    Last edited by Resource Dragon; Nov 26th, 2011 at 07:50 AM. Reason: Aim for additional clarity.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Merge field syntax problem

    is the document already a mailmerge object?

    try recording a macro to generate some sample code
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Location
    Townsville, Qld, Australia
    Posts
    135

    Re: Merge field syntax problem

    No, wrdDoc is a new, blank document until the code populates it, based on choices the user has made. (The code lives in an excel file, and opens word and outlook.)

    I've tried recording a macro in Word, to insert fields and I get:
    Code:
    ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField, Text:="""First"""
    which doesn't work when I use it in the context above.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Merge field syntax problem

    first you must make it into a merge document and set the datasource, you can not add merge fields to a standard document
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Location
    Townsville, Qld, Australia
    Posts
    135

    Re: Merge field syntax problem

    Dear Pete

    I'm not quite sure what you mean by "make it into a merge document", if I'm looking at Word 2007 I've got options like template & macro-enabled (or I can have a regular document). Am I barking up the wrong tree here?

    The code does link wrdDoc to a data source: here is the extended version of the code. (One problem that I can see with what I have done is that I've set "myContacts" but I haven't referred to them in the subsequent code. Here is the more complete version of the code I have written to date.

    Code:
    Sub CreateNewWordDoc()
    MyDate = Date
      Set wrdApp = CreateObject("Word.Application")
        wrdApp.Visible = True
        Set wrdDoc = wrdApp.Documents.Add ' create a new document
        
        Set OutApp = GetObject(, "Outlook.Application")
        Set myNameSpace = OutApp.GetNamespace("MAPI")
        Set myFolder = myNameSpace.GetDefaultFolder(olFolderContacts)
        
        Set myContacts = myFolder.Items
           
        With wrdDoc
    .Content.InsertAfter Format(MyDate, "dddd, d MMMM yyyy")
        .Content.InsertParagraphAfter
        .Content.InsertAfter "Dear "
        End With
            ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
            , Text:=" MERGEFIELD First"
    With wrdDoc
        .Content.InsertParagraphAfter
        .Content.InsertAfter "This a test, please ignore."
    file_name = "Test Merge " & Format(Date, "yyyy") & ".doc"
    If Dir(ActiveWorkbook.Path & "\MyNewWordDoc.doc") <> "" Then
            Kill ActiveWorkbook.Path & "\MyNewWordDoc.doc"
            End If
            .SaveAs (ActiveWorkbook.Path & "\" & file_name)
      End With
    Thank you

    The Dragon

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Merge field syntax problem

    in word, try recording a macro of the steps to create a mailmerge document and setting the datasource, you can then incorporate some of that into your code

    vb Code:
    1. wrddoc.mailmerge.createdatasource Name, PasswordDocument, WritePasswordDocument, HeaderRecord, MSQuery, SQLStatement, SQLStatement1, Connection, LinkToSource
    Last edited by westconn1; Nov 28th, 2011 at 03:58 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Location
    Townsville, Qld, Australia
    Posts
    135

    Re: Merge field syntax problem

    I recorded a macro in Word, and this is what I got:

    Code:
    Sub Macro1()
    '
        ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
        Selection.TypeText Text:="Dear "
        WordBasic.MailMergeUseOutlookContacts
        ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
            , Text:="""First"""
        Selection.TypeParagraph
        Selection.TypeText Text:="Test only."
    End Sub
    (The document that this produces looks like:

    Dear &#171;First&#187;
    Test only.

    BTW, this code was recorded in Word 2007, I know that "WordBasic" is anachronistic but that's what I got.

    I've also been going back to Word, to see what the Word help has to say, and the code looks quite different again.
    Last edited by Resource Dragon; Nov 28th, 2011 at 05:29 AM. Reason: Add further detail.

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Merge field syntax problem

    you should not need to use the old word basic, there is vba code to do all, just some hard to find
    UseAddressBook Method


    Selects the address book that's used as the data source for a mail merge operation.

    Syntax

    expression.UseAddressBook(Type)

    expression Required. An expression that returns a MailMerge object.

    Type Required String. The address book to be used as the mail merge data source. The string corresponds to the application's file extension, as specified in the registry (for example, "olk" for Outlook, "scd" for Schedule+, or "pab" for the Personal Address Book).
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Location
    Townsville, Qld, Australia
    Posts
    135

    Re: Merge field syntax problem

    You’ve lost me. Which expression returns a MailMerge object? If I try using, say, wrdDoc as a MailMerge object, VBA complains that the object or method is not recognised.

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