Word template automation [C#]

Word template automation [C#]

Post by Johnny E. » Fri, 26 Oct 2007 16:02:05


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 =

object objType =

object objFileFormat =

_Application.Visible = true;

_Application.ScreenUpdating = false;

_Application.DocumentBeforeClose += new

Microsoft.Office.Interop.Word.Document objDocument =
_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);


_Application.ScreenUpdating = true;





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

1. c# word automation need code to delete all macros in a word document

2. Calling a C# assembly from Excel or Word (VBA) => Automation Error


I have tried the example from the KB article 817248
( http://www.yqcomputer.com/ ) in Visual Basic AND in C# with
.NET Framework 2.0:

<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)> _
Public Class ComClass1
Public Const ClassId As String = "6DB79AF2-F661-44AC-8458-62B06BFDD9E4"
Public Const InterfaceId As String = "EDED909C-9271-4670-BA32-109AE917B1D7"
Public Const EventsId As String = "17C731B8-CE61-4B5F-B114-10F3E46153AC"

Public Sub New()
End Sub

Public Function myFunction() As Integer
Return 100
End Function

End Class

Then I registered the assembly with regasm.exe and the options "/tlb:
TestProj /codebase". Everything worked great if I called the function from
Visual Basic 6:

Dim myObject As TestProj.COMClass1
Set myObject = New TestProj.COMClass1
MsgBox myObject.myFunction

If I try to call exactly the same code from VBA in Excel or Word I get an
automation error at line "Set myObject = New TestProj.COMClass1" saying that
the assembly is not found (I have added the reference of course). If I try it
in VBA of MS Access it works, too. With .NET 1.1 it works in Excel and in
Word with no errors, but I need it under .NET 2.0.

Can somebody help me please?
Thanks a lot in advance,


3. word automation 2003 from webserver asp.net c#

4. Word automation using C#

5. c# word automation

6. Word Automation with C#

7. Speech Automation in word from c#.net

8. Word Automation And C#

9. word automation with asp.net (c#)

10. c# word automation revisited

11. C# / .NET Word Automation. Using CustomDocumentProperties

12. Tab Character C# Word 2003 Automation

13. Word Automation - replicating document pages with C#

14. Access automation error calling a Word template