Word - Mail-Merge RRS feed

  • Question

  • Hi,

    I've been trying for a while to figure out how to do mail-merge from lightswitch to Word without success.  I mean I can get one record (row) to Word but would like to have one Word page per row.  I tried this from Beth Massi:

    Module IncentivesReports
            Public Sub RunTest(ByVal emp As VisualCollection(Of ParamEmployee))
                If AutomationFactory.IsAvailable Then
                        'Create the XML data from our entity properties dynamically
                        Dim myXML = <employees>
                                        <%= From prop In emp.Details.Properties.All
                                            Select <<%= prop.Name.ToLower %>><%= If(prop.Value, "-") %></>

                        Using word = AutomationFactory.CreateObject("Word.Application")
                            Dim doc = word.Documents.Open("C:\TestTemplate.docx")

                            'Grab the existing bound custom XML in the doc
                            Dim customXMLPart = doc.CustomXMLParts("urn:microsoft:employees")

                            Dim all = customXMLPart.SelectSingleNode("//*")
                            Dim replaceNode = customXMLPart.SelectSingleNode("/ns0:root[1]/employees[1]")

                            'replace the <customer> node in the existing custom XML with this new data
                            all.ReplaceChildSubtree(myXML.ToString, replaceNode)

                            word.Visible = True

                        End Using
                    Catch ex As Exception
                        Throw New InvalidOperationException("Failed to create customer report.", ex)
                    End Try
                End If
            End Sub

    End Module

    From this example, I can get the employee name on the first page but not the second one on a second page.  I also tried a recompiled version (2013) of Office integration pack with the same result:


     Private Sub Print_Execute()
               Dim WordFile = "C:\Mailmerge.docx"

                If File.Exists(WordFile) Then

                    'Map the content control tag names in the word document to the entity field names
                    Dim custFields As New List(Of OfficeIntegration.ColumnMapping)
                    custFields.Add(New OfficeIntegration.ColumnMapping("EmployeeName", "EmployeeName"))
                    'custFields.Add(New OfficeIntegration.ColumnMapping("CompanyName", "CompanyName"))
                    'custFields.Add(New OfficeIntegration.ColumnMapping("Phone", "Phone"))

                   Dim doc As Object = OfficeIntegration.Word.Export(ParamEmployees, False)               

                    OfficeIntegration.Word.SaveAsPDF(doc, "C:\test" & x.ToString & ".pdf", True)

                End If
                End Sub

    I want to run the command from a Editable Grid Screen with all the employees, one employee per Word page.

    I'm sure there's a way but can't find it.  Anyone can help?



    Thursday, April 23, 2015 7:49 PM


  • Hello Richard,

    unfortunately I cannot give you the final solution. But taking the source from the old Office Automation it should work similar for the newer Version.

    wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;	
    object oPageBreak = Word.WdBreakType.wdPageBreak;
    wrdRng.Collapse(ref oCollapseEnd);
    wrdRng.InsertBreak(ref oPageBreak);
    wrdRng.Collapse(ref oCollapseEnd);
    wrdRng.InsertAfter("We're now on page 2:");

    Hope this helps

    Kind Regards


    • Proposed as answer by Angie Xu Tuesday, May 5, 2015 12:59 PM
    • Marked as answer by Angie Xu Wednesday, May 6, 2015 1:31 AM
    Friday, April 24, 2015 4:22 PM