Christian Sparre

Hello

I have a simple single page template for a letter that I fill out using a simple C# application. But how do I make multiple pages of that template in the same document, eg. adding pages to a "Microsoft.Office.Interop.Word.Document" document for each recipient.

The letters will never be more than a page long. What I'm trying to accomplish is beeing able to make severel letters from one template and then Print them out in one go.

How is this possible



Re: Visual Studio Tools for Office Adding pages to document based on the same template

Cindy Meister

Questions concerning automation of Word should be directed to the office.developer.newsgroup. This forum is specifically for discussing the VSTO technology, not for general Office automation support.

Consider saving the page as an AutoText entry in the template; insert the AutoTextEntry as required.

The alternative is to use the InsertFile method.






Re: Visual Studio Tools for Office Adding pages to document based on the same template

waterseller

I'm having the same issue here... I want to have a word document with multiple pages from a single page template.

Cindy:

Is it possible to be more specific I tried to use the InsertFile method

w_doc = w_app.Documents.Add(My.Settings.optPathModelesWord & "wd_template_LettreRegroupement.dot)

[data insertion in w_doc here ...]

w_doc.Range.InsertFile(My.Settings.optPathModelesWord & "wd_template_LettreRegroupement.dot)


I guess i'm not using the InsertFile method correctly, because the first page gets replaced each time instead of adding a new page from the template




Re: Visual Studio Tools for Office Adding pages to document based on the same template

Cindy Meister

waterseller wrote:
Is it possible to be more specific I tried to use the InsertFile method

w_doc = w_app.Documents.Add(My.Settings.optPathModelesWord & "wd_template_LettreRegroupement.dot)

[data insertion in w_doc here ...]

w_doc.Range.InsertFile(My.Settings.optPathModelesWord & "wd_template_LettreRegroupement.dot)


I guess i'm not using the InsertFile method correctly, because the first page gets replaced each time instead of adding a new page from the template

You're very close. The problem comes from not collapsing the range to its end-point before inserting the new file. Think of a range like a selection, except you can't see it on the screen. If you type or insert something into a selection, it replaces the selection; same thing with a range. So...

Dim rng as Word.Range

rng = w_doc.Range

rng.Collapse Word.WdCollapseDirection.wdCollapseEnd

rng.InsertFile('etc)






Re: Visual Studio Tools for Office Adding pages to document based on the same template

waterseller

Wow thanks for the quick reply

Now the problem i'm having is that my code always uses the same bookmarks of my template so the first page contains the data and the other pages contains the empty templates...

' creation of a new page from the template

Dim rng As Word.Range
rng = w_doc.Range
rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
rng.InsertFile(My.Settings.optPathModelesWord & "wd_template_LettreRegroupement.dot")

' data insertion in the word document

With w_doc
.Bookmarks("bmkNom").Range.Text = M_Database.getValue(Nothing, 0, "String")
.Bookmarks("bmkNom2").Range.Text = M_Database.getValue(Nothing, 1, "String")
[ ... ]
End With

' table insertion in the word document

w_doc.Tables.Add(w_doc.Bookmarks("bmkLots").Range, 2, 4)

Dim tableLots As Word.Table = w_doc.Tables(1)
With tableLots
.PreferredWidth = w_app.InchesToPoints(6.2)
.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
.Rows(1).Range.Font.Bold = 1
.Cell(1, 1).Range.Text = "Numero de lot projete"
.Cell(1, 2).Range.Text = "Numero de lot actuel"
.Cell(1, 3).Range.Text = "Designation secondaire"
.Cell(1, 4).Range.Text = "Cadastre"
End With

[ ... ]

Any ideas

Thank you



edit: I guess i need to tell w_doc what template to use...




Re: Visual Studio Tools for Office Adding pages to document based on the same template

waterseller

It seems like the InsertFile method does not copy the Word.Bookmark objects included in the .dot template

Am i wrong




Re: Visual Studio Tools for Office Adding pages to document based on the same template

Cindy Meister

Bookmark names in a document must be unique. If the same bookmarks are present in text being inserted, Word will remove them. That's why you're seeing what you're seeing.

If you need to retain the bookmarks in both sets, then you'll have to rename the ones already in the document. I usually do this by appending a number, new number for each "copy" of whatever I'm inserting repeatedly. You can't actually rename a Word bookmark. You have to create a new bookmark, based on the same range, then delete the old one.






Re: Visual Studio Tools for Office Adding pages to document based on the same template

waterseller

I guess this solution is not possible because the number of generated letters from the template is unknown.

The solution i've found (i know it is not a "clean" solution) is to save the generated letter in a file, and then use the InsertFile method in another Word.Document in another Word.Application


'Word document genereration from database using a template with Bookmarks

Dim reader As Odbc.OdbcDataReader

Dim w_app_temp As New Word.Application
Dim w_doc_temp As Word.Document = Nothing

Dim w_app_main As New Word.Application
Dim w_doc_main As Word.Document = Nothing

w_doc_main = w_app_main.Documents.Add()
w_app_main.Visible = True

[...]

While reader.Read()
w_doc_temp = w_app_temp.Documents.Add("template.dot")

With w_doc_temp
.Bookmarks("bmkNom").Range.Text = CStr(reader.GetValue(0))
.Bookmarks("bmkAdr1").Range.Text = CStr(reader.GetValue(1))
.Bookmarks("bmkAdr2").Range.Text = CStr(reader.GetValue(2))
[...]
End With

'We save the temporary Word.Document in the default temporary folder
Try
w_app_temp.ActiveDocument.SaveAs(System.Environment.GetEnvironmentVariable("TEMP") & "\" & nomFichierWordTemp & ".doc")
Catch ex As Exception
nomFichierWordTemp = String.Concat(nomFichierWordTemp, New Random(System.DateTime.Now.Millisecond))
w_app_temp.ActiveDocument.SaveAs(System.Environment.GetEnvironmentVariable("TEMP") & "\" & nomFichierWordTemp & ".doc")
Finally
w_doc_temp.Close()
End Try

'Then we insert the Word.Document in the other Word.Document
With w_doc_main.Range
.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
.InsertFile(System.Environment.GetEnvironmentVariable("TEMP") & "\lettreTemp.doc")
.InsertBreak(wdPageBreak)
End With

End While

w_doc_main.Close()
reader.Close()