none
Repeat Word template in several pages RRS feed

  • Question

  • Dear all,

    I developed a Vb.Net application that reads data from a SQL Server table.

    At the moment, for each row, I open a Word template, fill the predefined bookmark and create the final Word .doc file.

    In this way, if I have 3 rows then I create 3 different Word files.

    Now, I need to create only one Word file, but with a page for each table row.

    Via Vb.Net, Can I open the Word template and fill the bookmark for the first row, then add a break page, reuse the same tamplte to fill the second new page, and so on?

    Please, can someone help me?

    Best regards

       Emanuele

    Friday, April 8, 2016 3:42 PM

All replies

  • Can you do this with a mail merge? Otherwise, I would think you would just apply a style (or multiple styles) to the contents of each page.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, April 8, 2016 8:01 PM
  • You might also be able to construct a new document after the current process finishes and insert each generated document into this new one, if Paul's suggestions don't work for you (those are better options).

    You might also want to ask in Word for Developers Forum instead of here.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, April 9, 2016 4:47 AM
  • Dear Paul,

    thank you for you help.

    I'm sorry, but I don't understand what's mail merge and "appply a style".

    Please, can you show me an example (source code) I can follow?

    Best regards

        Emanuele

    Monday, April 11, 2016 6:58 AM
  • Dear Paul,

    thank you for you help.

    I'm sorry, but I don't understand what's mail merge and "appply a style".

    Please, can you show me an example (source code) I can follow?

    Best regards

        Emanuele

    There is a mail merge example below, but you would have to adapt for a SQL Server data source.

    How to automate Word to perform a mail merge from Visual Basic .NET

    I'm not really sure what your level of knowledge is with respect to the Word object model, but if you're not familiar with mail merge and styles this task could be somewhat challenging.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Monday, April 11, 2016 8:44 PM
  • Dear Paul,

    to explain my specific problem, I try to add some lines of my actual VB.Net principal method:


    Dim MyWord As Object = Nothing
    
    MyWord = CreateObject("Word.Application")
    
    '...
    
    MyWord.Documents.Add(Template:=sFileName, NewTemplate:=False)
    MyWord.Visible = False
    
    '...
    
    ' fill in the bookmark fields
    MyWord.Selection.GoTo(What:=wdGoToBookmark, Name:="BK_CARDCODE")
    text = "Business Partner id"
    MyWord.Selection.TypeText(text:=text)
    
    MyWord.Selection.GoTo(What:=wdGoToBookmark, Name:="BK_DOCNUM")
    text = "my doc. num."
    MyWord.Selection.TypeText(text:=text)
    
    ' ...
    ' fill the predefined table of my Word template
    For rows = 0 To MyDataset.Tables(0).Rows.Count - 1
       With MyWord
          .ActiveDocument.Tables(2).Cell(rows + 3, 2).Select()
          text = "vat %"
          .Selection.Text = text
       End With
       ' ...
    Next
    
    ' ...
    ' final step
    
    sFileName = thepath
                MyWord.ActiveDocument.SaveAs(FileName:=sFileName, FileFormat:=wdFormatDocument, _
     LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword:="", _
    ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:=False)
    
    MyWord.ActiveDocument.Close()
    
    MyWord.Quit(False, False, False)
    
    System.Runtime.InteropServices.Marshal.ReleaseComObject(MyWord)
    
    

    I repeat this procedure for every row I obtain with my specific SQL Server query. In this way I create a new Word document for every row.

    What VB code lines I have to add in my procedure to create only one Word document with one page for each row? I need to add a page break at the end of every single "document" and repeat the usage of my Word template in the new page, and so on.

    Please, can you help me?

    Best regards

       Emanuele

    Thursday, April 14, 2016 7:42 AM
  • Hi, Emanuele Croci

    According to your description, please correct me if I have any misunderstandings on your question, Mailmerge is pretty easy, really. Your existing 'template' can provide the basic document, which yoy simply need to point to the data source, then insert mergefields where you now have bookmarks. Once you've done that, it's basically just a matter of finishing/executing the merge.

    You could refer to this helpful link:

    http://answers.microsoft.com/en-us/office/forum/office_2007-word/adding-pages-to-a-template-using-vba/3a94399b-3e79-4965-a515-4db6ce70c34c

    Tuesday, April 19, 2016 3:23 AM