Create Word document from template

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asedt
    New Member
    • Jun 2008
    • 130

    Create Word document from template

    Hi

    I have a Word document that I will use as a template, i need to replace words in the document like replace "<NameField >" with a name and so on. And save the new Word file on anoter place.

    I'm working with VB 2005 starting with a windows application template. (Windows Forms)

    I have looked into some stuff, like Visual Studio Tools for Office, i but i need guidelines, i'm stuck reading about PIA's and stuff.

    Any help is appreciated.
  • asedt
    New Member
    • Jun 2008
    • 130

    #2
    I was first thinking that you maybe can do ths with a macro. And the only need to run the macro from the application.

    I have the values that i want in the word file in a text file like:

    <NameField>
    My name

    So a small macro for replacing and a way to call it from my program will maybe work.

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      I think that you should be able to do this with Interop:

      Comment

      • asedt
        New Member
        • Jun 2008
        • 130

        #4
        Ok i have solved some parts, maybe not the best way but it works.

        First i added the reference for Word 11 (Interop.Word 8.3.0.0)

        To start word i use:
        Code:
        Dim WDApp As Word.Application
        WDApp = CreateObject("Word.Application")
        WDApp.Visible = False
        or
        Code:
        Dim WDApp As Word.Application
        WDApp = New Word.Application()
        WDApp.Visible = False
        Dunno the difference...

        Then to create a word documet from template:
        Code:
                WDApp.Documents.Add(Template:="C:\template.dot", Visible:=False)
        And after that to save as and quit:
        Code:
        WDApp.Documents.Item(1).SaveAs("C:\newdoc.doc")
        
        WDApp.Quit()
        WDApp = Nothing
        Ok then i also need to do som replacing in the word document:
        Code:
        WordApp.Selection.Find.ClearFormatting()
        WordApp.Selection.Find.Text = "<Replaceme>"
        WordApp.Selection.Find.Replacement.ClearFormatting()
        WordApp.Selection.Find.Replacement.Text = "NewText"
        WordApp.Selection.Find.Execute(Replace:=Word.WdReplace.wdReplaceAll, Forward:=True, Wrap:=Word.WdFindWrap.wdFindContinue)
        This worked when i when i tried.

        But the i dicided to try put it in a sub:
        Code:
        Private Sub Replace(ByRef WordApp As Word.Application, ByVal OLDText As String, ByVal NEWText As String)
                WordApp.Selection.Find.ClearFormatting()
                WordApp.Selection.Find.Text = OLDText
                WordApp.Selection.Find.Replacement.ClearFormatting()
                WordApp.Selection.Find.Replacement.Text = NEWText
                WordApp.Selection.Find.Execute(Replace:=Word.WdReplace.wdReplaceAll, Forward:=True, Wrap:=Word.WdFindWrap.wdFindContinue)
            End Sub
        Code:
        Replace(WDApp, "<Replace ME>", "New Text")
        It worked on my developer computer, but when i tested on a target system i got this error message:

        Code:
        System.NullReferenceException: Object reference not set to an instance of an object.
          at VSTO.Form1.Replace(Application& WordApp, String OLDText, String NEWText)
        I have problem soliving this, do i use the ByRef corectly for WordApp? Can anyone help me with this? thx

        Comment

        • asedt
          New Member
          • Jun 2008
          • 130

          #5
          Hmm i don't get this to work. I tried witout the sub, becaus I was thinking it was what creating the problem.

          Code:
          WDApp.Selection.Find.ClearFormatting()
          WDApp.Selection.Find.Text = "<OLDText>"
          WDApp.Selection.Find.Replacement.ClearFormatting    ()
          WDApp.Selection.Find.Replacement.Text = "NEWText"
          WDApp.Selection.Find.Execute(Replace:=Word.WdReplace.wdReplaceAll, Forward:=True, Wrap:=Word.WdFindWrap.wdFindContinue)
          But i still get the same problem on the target system, but not at my developer machine, :((

          So do anyone know how to replace stuff in a Word document?, do I do it wrong? I think i copied my code from a msdn page abaut VSTO ("How to: Search for and Replace Text in Documents)

          Comment

          Working...