New word document based on a template

Mar 29, 2012 at 11:18 AM
Edited Mar 29, 2012 at 3:44 PM

Hello Sebastian,


I've just found your project and I think it can help me.

1) I've to create a new document based on a template

2) Replace some text (Example: Replace ~~CustomerID~~ by 93). This text could be replaced several times in the document if it occurs several times.

3) Add a break page at the end of this new document.

4) Go to the new page

5) Add a table (I've found how to do it in your example section).

6) Add a return line after a text.

7) Save the new file (found).


Can you help me ? If all those questions are somewhere can you help me where to find them ? :-)


Thank you & I think you will save my job ! :)



Mar 29, 2012 at 1:44 PM
Edited Mar 29, 2012 at 1:45 PM

Hi Sebastian,


I've already found:

1) wordApplication.Documents.Add("c:\")

2)Replace some text: Not found: Can you help me ?

3) wordApplication.Selection.InsertBreak()

4) wordApplication.Selection.EndKey(WdUnits.wdStory, WdMovementType.wdMove)

6) I use a Convert.ToChar(13)


You did a very cool job :) Keep on the way !  :-)



Mar 29, 2012 at 3:58 PM

hello john,
first: thank you very much.

for the find and replace:

Word.Document newDocument = wordApplication.Documents.Add(); 
Word.Find find = newDocument.Range().Find; 
find.Text ="SearchText";
find.Replacement.Text = "ReplaceText"; 

by the way, use google search with your problem and the keyword "VBA". the VBA community has the best knowledge and experience about programming office.


Mar 29, 2012 at 8:41 PM

Hi Sebastian,


When I try to save the document:

Dim wordApplication As New Word.Application
Dim newDocument As Word.Document = wordApplication.Documents.Add("C:\Users\John\Documents\")
Dim find As Word.Find = newDocument.Range().Find
find.Text = "~~CustomerID~~"
find.Replacement.Text = "93"

I still have ~~CustomerID~~instead of 93...

Any idea ?



Ps: Thanks for your support

Mar 29, 2012 at 10:20 PM
Edited Mar 29, 2012 at 10:21 PM

now i see, you need the replace parameter.  i didnt find a way to set this parameter to the find object. all other params can bet set to the find object and call Execute without parameters

Word.Find find = newDocument.Content.Find;
find.Text = "~~CustomerID~~";
find.Replacement.Text = "007";       
find.Execute(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, WdReplace.wdReplaceAll);

Mar 30, 2012 at 12:14 PM

Hi Sebastian,


Thank you very much !!! :)


See you soon (I think ;) )