2

A previous answer suggested here leads me to the conclusion I can merge images, but without control of the datasource how can I make the image different based upon a field condition? For instance if the customer is Canadian the logo will be one thing, if US it will be another. There are actually account groupings and the parent companies have different assigned responses. I need to make conditional merges based upon the data I am receiving.

0

2 Answers 2

3

You can achieve this through one template by using conditional IF fields.

For example, if you only have two possible options you can use this:

{ IF {MERGEFIELD Country} = "USA" "(The USA image)" "(The Canadian image)" }

However if you have more than two options it's more suitable to do something like this:

{ IF {MERGEFIELD Country} = "USA" "(The USA image)" "" }
{ IF {MERGEFIELD Country} = "Canada" "(The Canada image)" "" }
{ IF {MERGEFIELD Country} = "UK" "(The UK image)" "" }

You would want all three on the same line next to each other. If the country code does not match what is merged then the images are hidden and only the image of the one that match is shown.

3
  • 1
    wow this makes so much sense? Wasn't sure how to designate the image source, but was able to create this using insert images in the if statement. Wonderful solution, and so obvious. Thank you.
    – datatoo
    Jul 8, 2012 at 2:16
  • I'm glad my answer can still be of help even after all this time since the question was asked :)
    – Adam
    Jul 8, 2012 at 4:37
  • You could also do nested IFs, e.g.: { IF {MERGEFIELD Country} = "USA" "(The USA image)" "{ IF {MERGEFIELD Country} = "Canada" "(The Canada image)" "{ IF {MERGEFIELD Country} = "UK" "(The UK image)" "" }" }" } (it's just another option, though Adam's way looks cleaner)
    – Bob
    Jul 8, 2012 at 4:53
1

What I did is that I make different template for each group. For your example, one template is for Canadian. Another is for US. (Duplicate Canadian template to be US template and change to Canadian's logo to US's logo)

The following is the code that I did for my project. If Student are in third grade, it shows third grade template. If they are fourth grader, it pulls fourth grade template. In my data, I have a column that explains which template is required.

Sample Data

StudentID     Grade      Template            ....
---------     -----      ----------               
 3424           3        3rd.docx                  
 4424           4        4th.docx                  
 3198           3        3rd.docx                  

VBA code:

Public Sub Print_Student_Report_by_St_ID(StudentID_in As Long, _
                            Optional TemplateName_in As String, _
                            Optional TemplatePath_in As String, _
                            Optional DisplayWord_in As Boolean)
On Error GoTo ErrorHandler

 Dim this_path As String
 Dim this_db As String

     Dim strTemplateFileName As String
     Dim strTemplatePath As String

     'Folder Location
     this_path = Application.CurrentProject.Path & "\"
     this_db = this_path & Application.CurrentProject.Name

     'Find Template
     strTemplatePath = TemplatePath_in
     strTemplateFileName = TemplateName_in

     'Convert St_ID to SQL Statement
     Dim ssql As String

        ssql = SELECT_SOMETHING

     Dim word_app As Word.Application
     Set word_app = CreateObject("Word.Application")

     If DisplayWord_in Then
        word_app.Visible = True
     Else
        word_app.Visible = False
     End If

     Dim word_doc  As Word.Document
     Set word_doc = word_app.Documents.Open(TemplatePath_in & strTemplateFileName)

     If word_doc.MailMerge.State <> wdMainAndDataSource Then

         With word_doc.MailMerge

                 'Add UserName and Password to prevent it ask password everytime if not user windows logon password

                 Dim strConnection As String
                 strConnection = "Provider=SQLOLEDB.1;Initial Catalog=database_name;" _
                                 & "Data Source=domain.com;" _
                                 & "UID=user;PWD=password;"

                 .OpenDataSource _
                 Name:=this_db, _
                 ReadOnly:=True, LinkToSource:=True, _
                 SQLstatement:=ssql, Connection:=strConnection

                .Destination = wdSendToPrinter
                .Execute

         End With

    End If

Exit_Sub:

        word_doc.Close SaveChanges:=wdDoNotSaveChanges
        word_app.Quit wdDoNotSaveChanges

        Set word_doc = Nothing
        Set word_app = Nothing

        Exit Sub

ErrorHandler:

        Select Case Err.Number
        Case 0

        Case Else
                'Show Some Error

        End Select
        Resume Exit_Sub
        Resume

    End Sub

But disadvantage is if you have too many groups. You may need a bunch of templates, which are hard to maintain.

3
  • Sorry to take so long to respond to this. Your answer may be helpful. I need to merge to new documents, as they are faxed to a couple of different vendors for reporting purposes. Any idea on appending all 3rd.docx together, all 4th.docx together, etc?
    – datatoo
    Sep 15, 2011 at 0:09
  • I do not have a code for append. But my guess is you write another Sub that create a new blank document. And Select All (Say 3rd.docx) and paste into new blank one. Do that many times. You need to find last position so it won't replace the top one. The idea is to simulate Select All, Copy and then Paste into bottom to other document. At the end, you get a big large document with all data. Sep 15, 2011 at 15:08
  • not sure I can get this working, but you have provided some direction, thanks +1
    – datatoo
    Sep 15, 2011 at 19:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .