Word template automation [C#]

J

Johnny E. Jensen

Hello

I am creating a Word 2007 mailmerge template. I created an application that
creates a .txt file containing the merge fields an their data.

To prepare Word to create the template I'll do this:
public void CreateTemplate(string Name, string Datasource, string
MergeDataHeader, string UserFolder)

{

object objName = UserFolder + "\\" + Name + ".dot";

_Filename = objName.ToString();

object objFormat =
Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatAuto;

object objType =
Microsoft.Office.Interop.Word.WdDocumentType.wdTypeTemplate;

object objFileFormat =
Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatTemplate;

_Application.Visible = true;

_Application.ScreenUpdating = false;

_Application.DocumentBeforeClose += new
Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(Application_DocumentBeforeClose);

Microsoft.Office.Interop.Word.Document objDocument =
(Microsoft.Office.Interop.Word.Document)_Application.Documents.Add(ref
_Missing, ref _True, ref _Missing, ref _Missing);

objDocument.MailMerge.OpenDataSource(Datasource, ref objFormat, ref
_Missing, ref _Missing, ref _Missing, ref _Missing, ref _Missing, ref
_Missing, ref _Missing, ref _Missing, ref _Missing, ref _Missing, ref
_Missing, ref _Missing, ref _Missing, ref _Missing);

objDocument.SaveAs(ref objName, ref _Missing, ref _Missing, ref _Missing,
ref _Missing, ref _Missing, ref _Missing, ref _Missing, ref _Missing, ref
_Missing, ref _Missing, ref _Missing, ref _Missing, ref _Missing, ref
_Missing, ref _Missing);

objDocument.MailMerge.EditMainDocument();

_Application.ScreenUpdating = true;

try

{

_Application.Activate();

}

catch { }

}


Notice that i'll add the DocumentBeforeClose event - I'll do this to fetch
the filename the users has saved the template. In the Class where this
CreateTemplate() method is, i also creates a delegate with the param 'string
Filename' this delegate i raised within the DocumentBeforeClose event. The
delegate is also instanciated at the outside of the Class. This event is
raised when I click the close button on Word and the Filename contain the
filename that the user has saved the template.

But I can't use that Filename for anything. I's like the event is never
fired, but it is. Is i'll put in a messagebox in the event to display the
filename i'll see it. put if i remove the messagebox and instead use the
filename to be displayed within a textbox, it's never is been displayed.
Also if i put in a pagebreak in the event i'll see the filename, but never
if i remove the pagebreak.

Kind regards

Johnny Jensen
 

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