Populating a MS Word template with C#

G

Guest

Does anyone know of some useful articles that explain how to develop word
templates that contain fields that will be populated from a database. I am
new to this and the project I am working on will retrieve a user generated
Word template that I will use to populate the template with database
information. My responsibilities include explaining how the user is to use
the databinding fields within the template, how to bind fields, and display a
printable word document. I don't know if I am using the right terminology, I
am new to not only binding fields to MS Word but also what a template is
within the Word application). Thanks
 
S

SurferJoe

/*

* We came up with another way of doing this using bookmarks in templates

* rather than mail merge templates. These templates are much easier to
make,

* and write to. These examples are written in Delphi, we've not yet
converted

* them to C# though it is likely to get done very soon. The code should be
pretty

* simple to extrapolate.

*/



procedure StartWordTemplate(APath, AName: String);

var FileName: OleVariant;



begin



// Open template -------------------------------------

with objWordApplication do begin

Disconnect;

Connect;

Visible := True;



FileName := APath + AName;





Documents.Open(

FileName,

EmptyParam,

EmptyParam,

EmptyParam,

EmptyParam,

EmptyParam,

EmptyParam,

EmptyParam,

EmptyParam,

EmptyParam,

EmptyParam,

EmptyParam,

EmptyParam,

EmptyParam,

EmptyParam);

{ }

end;

end;



procedure GoToWordBookMark( AName, AString: String);

var

What, Which, Count, Name: OleVariant;

s: WordSelection;



begin

// Word Book Marks -----------------------------------------



What := wdGoToBookmark;

Which := 0;

Count := 0;

Name := AName;

objWordApplication1.Selection.GoTo_(What, Which, Count, Name);



// Populate book Marks ------------------------------



with objWordApplication1 do begin

s := Selection; // Word Type Lib 6-19-2003gr

s.TypeText(AString);



end;



end;



/*

* These two procedures can call macros in the template for certain other
purposes;

*/



RunWordMacro( 'myCreateTable');

RunWordMacro1( 'mySaveDoc', AFileName);



procedure RunWordMacro( AName: String);

var

MacroName: OleVariant;



begin



// Run Word Macros Template -------------------------------------

MacroName := AName;

objWordApplication1.Run( MacroName);



end;



procedure RunWordMacro1( AName, AParamText1: String);

var

MacroName, Param1: OleVariant;



begin



// Run Word Macros Template -------------------------------------

MacroName := AName;

Param1 := AParamText1;



objWordApplication1.Run( MacroName, Param1);



end;



/*

* Finally this closes the session;

*/



EndWordTemplate;



procedure EndWordTemplate;

begin



// Close template -------------------------------------



with objWordApplication1 do begin

Disconnect;

end;



end;



These code examples are provided courtesy of Greg Rowland greg @ waveltd.com



Hope this helps,



G
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top