This project has moved. For the latest updates, please go here.

New word document based on a template

Mar 29, 2012 at 10:18 AM
Edited Mar 29, 2012 at 2: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 ! :)

 

John

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

Hi Sebastian,

 

I've already found:

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

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 !  :-)

 

John

Coordinator
Mar 29, 2012 at 2: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"; 
find.Execute();

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.

Sebastian

Mar 29, 2012 at 7: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\netofficeTest.dot")
Dim find As Word.Find = newDocument.Range().Find
find.Text = "~~CustomerID~~"
find.Replacement.Text = "93"
find.Execute()
newDocument.SaveAs("C:\Users\John\Documents\test.doc")

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

Any idea ?

 

John

Ps: Thanks for your support

Coordinator
Mar 29, 2012 at 9:20 PM
Edited Mar 29, 2012 at 9: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 11:14 AM

Hi Sebastian,

 

Thank you very much !!! :)

 

See you soon (I think ;) )

 

Regards,

 

John