Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Currently working on using one base template to generate multiple variations within one ms word document in vb .net. The template has bookmarks for header, body, disclaimer, columns within table and footer.

When I use the template the first time, everything works. The second time around, the bookmarks are missing. Only the first template exists.

Is there an easy way of using one template and have multiple instances of it within the ms word document being generated in VB .NET?

Example:
C:\Temp\DefaultTemplate.dot

Within VB .net application I use:
Dim p_WordApp as new Microsoft.Office.Interop.Word.Application

With p_WordApp.Selection
     'First instance
     .InsertFile("C:\Temp\DefaultTemplate.dot")

     .GoTo(What:=-1, Name="bmHeader")
     .TypeText("TESTING")
     '
     'Additional bookmark text fills
     '

     'Second instance
     .InsertFile("C:\Temp\DefaultTemplate.dot")

     .Goto(What:=-1, Name="bmHeader")
     'The line above will go back to the first instance of the template.
End With
Posted
Updated 19-Nov-13 9:50am
v3

1 solution

Generally, *.doc file is not a MS Word template document. It could be treated as template by the user, but - formally - it's not! Please, see: Word templates[^] and read related article: Custom Document Templates[^].

Basically, you need to create *.dot file as a template, then to create documents based on this template ;)
 
Share this answer
 
Comments
mikelou2009 19-Nov-13 15:42pm    
Hi Maciej Los and thank you for your response.

I have made the changes you suggested, create a MS Word template instead of using the standard MS Word document. But I am still getting the same result.

Within my VB .NET application, I create a new instance of Micosoft.Office.Introp.Word.Application. First section of this document, I use the template by inserting it into the active word application. The second time I use the template, all of the bookmarks refers back to the first instance.
Maciej Los 19-Nov-13 16:04pm    
Are you sure you have made changes as i suggested? Please, see this: How to: Programmatically Create New Documents
mikelou2009 19-Nov-13 16:13pm    
I forgot to point to the new DLL. Made the changes, but noticed that the documents was a new document instead of being inserted into the current active one.

Basically, I have a master document that I want to insert multiple versions of the template into. The original call I made [WordApp.Selection.InsertFile("C:\Temp\DefaultTemplate.doc")] would insert the file into the master document. With the change suggested, I end up with multiple documents instead of just one master.
Maciej Los 19-Nov-13 16:48pm    
OK, i understand your issue better now... Remember, adding text into bookmark destroys bookmark. You need to refer to the range occupied by bookmark then add text and after that rebuild bookmark (create new one based on old one with new text). This is the only way i know ;)
If i'm wrong, please, notify me.
How to: Programmatically Update Bookmark Text

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900