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.

link|improve this question

73% accept rate
@Flimzy thanks for the assist – datatoo Jun 25 '11 at 2:50
feedback

1 Answer

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.

link|improve this answer
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 '11 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. – 1888 Sep 15 '11 at 15:08
not sure I can get this working, but you have provided some direction, thanks +1 – datatoo Sep 15 '11 at 19:21
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.