dcsimg
Results 1 to 9 of 9

Thread: [RESOLVED] setting font for mailmerge fields

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2019
    Posts
    5

    Resolved [RESOLVED] setting font for mailmerge fields

    Please, can anyone help with a mail merge problem involving Word 2007 and VB6.

    The following routine creates a mail merge using data from a text file with a header line containing the field names. This program works fine, but uses the default
    fonts throughout.
    Can anyone suggest a simple way of applying a different font to each field?

    Private Sub cmdIdCards_Click()

    'text file already created earlier in this routine with header line containing
    ' field names fname.midname,sname. Following line contain data, comma separated etc

    Dim oApp As Word.Application
    Dim oDoc As Word.Document
    Set oApp = CreateObject("Word.Application")
    Set oDoc = oApp.Documents.ADD

    With oDoc.MailMerge
    With .Fields
    For iCount = 1 To 3
    oApp.Selection.TypeParagraph
    Next iCount
    .ADD oApp.Selection.Range, "Fname"
    oApp.Selection.TypeParagraph
    .ADD oApp.Selection.Range, "midname"
    oApp.Selection.TypeParagraph
    .ADD oApp.Selection.Range, "Sname"
    oApp.Selection.TypeParagraph
    End With

    Dim oAutoText As Word.AutoTextEntry
    Set oAutoText = oApp.NormalTemplate.AutoTextEntries.ADD("VisitCard", oDoc.Content)
    oDoc.Content.Delete
    .MainDocumentType = wdMailingLabels
    .OpenDataSource Name:=App.Path & "\Ccards.txt" 'Specify the data source here

    oApp.MailingLabel.CreateNewDocument Name:="Sigel DP720", Address:="", AutoText:="VisitCard"
    .Destination = wdSendToNewDocument
    .Execute

    ' this section takes font parameters from form which calls this process. This allows fonts for mailmerge to be set before calling this

    oApp.ActiveDocument.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter

    oApp.ActiveDocument.SaveAs App.Path & "\Ccards_" & strDate & ".doc"
    oApp.ActiveDocument.Close
    End With

    oDoc.Close (wdDoNotSaveChanges)
    oApp.NormalTemplate.Saved = True
    oApp.ActiveDocument.PrintOut
    oApp.Quit
    Set oDoc = Nothing
    Set oApp = Nothing

    End Sub

  2. #2
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,503

    Re: setting font for mailmerge fields

    Are you wanting to change the font dynamically for each field, depending on what is in the field? If no, then the font formatting should be in the document where the merge fields are added.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2019
    Posts
    5

    Re: setting font for mailmerge fields

    Thanks for your response jdc2000. I want to hard code, within this routine, the font, size etc for the three fields listed. There is no dependence on field content.
    I have a working solution for setting the same font to all fields, taking values passed from the form, and this works well in other applications. In this case, however, I want to specify in the code and not in the document (if I knew where to find it!)

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2019
    Posts
    5

    Re: setting font for mailmerge fields

    Thanks for your response jdc2000. I want to hard code, within this routine, the font, size etc for the three fields listed. There is no dependence on field content.
    I have a working solution for setting the same font to all fields, taking values passed from the form, and this works well in other applications. In this case, however, I want to specify in the code and not in the document (if I knew where to find it!)

  5. #5
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,503

    Re: setting font for mailmerge fields


  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,628

    Re: setting font for mailmerge fields

    in your code, you can try like
    Code:
    .Add oApp.Selection.Range, "Fname"
    oApp.Selection.Paragraphs(1).Range.Font.Name = "arial"
    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
    New Member
    Join Date
    Dec 2019
    Posts
    5

    Re: setting font for mailmerge fields

    Many thanks Westconn1, just what was needed. A couple of points which others might find useful. Your solution will apply the same font to all following fields and, surprisingly, concatenates the contents of following fields. To overcome this I have added an extra "oApp.Selection.TypeParagraph" line after the font definition line.
    The following extract shows this more clearly.


    With oDoc.MailMerge
    With .Fields
    For iCount = 1 To 3
    oApp.Selection.TypeParagraph
    Next iCount
    .ADD oApp.Selection.Range, "Fname"
    oApp.Selection.Paragraphs(1).Range.Font.Name = "arial"
    oApp.Selection.Paragraphs(1).Range.Font.Size = 10
    oApp.Selection.Paragraphs(1).Range.Font.Bold = True
    oApp.Selection.TypeParagraph
    .ADD oApp.Selection.Range, "midname"
    oApp.Selection.Paragraphs(1).Range.Font.Name = "arial"
    oApp.Selection.Paragraphs(1).Range.Font.Size = 12
    oApp.Selection.Paragraphs(1).Range.Font.Bold = True
    oApp.Selection.Paragraphs(1).Range.Font.Italic = True
    oApp.Selection.TypeParagraph
    .ADD oApp.Selection.Range, "Sname"
    oApp.Selection.Paragraphs(1).Range.Font.Name = "arial"
    oApp.Selection.Paragraphs(1).Range.Font.Size = 14
    oApp.Selection.Paragraphs(1).Range.Font.Bold = True
    oApp.Selection.Paragraphs(1).Range.Font.Italic = True
    oApp.Selection.TypeParagraph
    End With

    Many thanks, problem solved!!

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,628

    Re: [RESOLVED] setting font for mailmerge fields

    i would always recommend to avoid working with the selection object or active any thing, instead always work with fully qualified ranges
    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
    New Member
    Join Date
    Dec 2019
    Posts
    5

    Re: [RESOLVED] setting font for mailmerge fields

    Quote Originally Posted by westconn1 View Post
    i would always recommend to avoid working with the selection object or active any thing, instead always work with fully qualified ranges
    I am sure that you are correct, but unfortunately, with my limited knowledge, I don't understand the concept of 'fully qualified ranges'. Could you give an example of how that would apply to my code, please. Many thanks

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width