Fill Word-template fields

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • steven

    Fill Word-template fields

    Hello,

    I've created a Word-template for a letter. I've made some fields in the
    template (such as 'customer number', 'name', ...) as follows: insert
    field - DocVariabele with name 'customer number' and 'name'. Now I want
    to fill these fields from my application and print them:


    Dim myWord As New Word.Applicatio n
    myWord.Document s.Add("c:\templ ate.dot")
    'fill the fields
    '...
    'print
    myWord.ActiveDo cument.PrintOut ()
    myWord.Quit(Wor d.WdSaveOptions .wdDoNotSaveCha nges)


    So what code is needed before I print the document to fill my template
    fields?

    Thanks

    Steven
  • Robin Tucker

    #2
    Re: Fill Word-template fields

    You can read/write the document variables collection. Something like this:
    (might not be exactly like this!):


    For i as Integer = 0 To TheDocument.Var iables.Count


    Select Case theDocument.Var iables.Item(i). Name

    Case "a_name"

    theDocument.Var iables.Item(i). Value = "test"

    End Select


    Next


    Something to note: make sure you clean up all references correctly :)


    "steven" <user@example.n et> wrote in message
    news:e6DifecxFH A.1168@TK2MSFTN GP15.phx.gbl...[color=blue]
    > Hello,
    >
    > I've created a Word-template for a letter. I've made some fields in the
    > template (such as 'customer number', 'name', ...) as follows: insert
    > field - DocVariabele with name 'customer number' and 'name'. Now I want to
    > fill these fields from my application and print them:
    >
    >
    > Dim myWord As New Word.Applicatio n
    > myWord.Document s.Add("c:\templ ate.dot")
    > 'fill the fields
    > '...
    > 'print
    > myWord.ActiveDo cument.PrintOut ()
    > myWord.Quit(Wor d.WdSaveOptions .wdDoNotSaveCha nges)
    >
    >
    > So what code is needed before I print the document to fill my template
    > fields?
    >
    > Thanks
    >
    > Steven[/color]


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Fill Word-template fields

      "steven" <user@example.n et> schrieb:[color=blue]
      > Dim myWord As New Word.Applicatio n
      > myWord.Document s.Add("c:\templ ate.dot")
      > 'fill the fields
      > '...
      > 'print
      > myWord.ActiveDo cument.PrintOut ()
      > myWord.Quit(Wor d.WdSaveOptions .wdDoNotSaveCha nges)
      >
      > So what code is needed before I print the document to fill my template
      > fields?[/color]

      \\\
      Option Strict Off
      ..
      ..
      ..
      Dim WordApp As Object = CreateObject("W ord.Application ")
      Dim WordDoc As Object = WordApp.Documen ts.Open("C:\Hal lo.doc")
      WordDoc.FormFie lds("Text1").Re sult = "Bla"
      WordDoc.Save()
      WordDoc.Close()
      WordApp.Quit()
      ///

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://classicvb.org/petition/>

      Comment

      • steven

        #4
        Re: Fill Word-template fields

        Herfried K. Wagner [MVP] wrote:[color=blue]
        > "steven" <user@example.n et> schrieb:
        >[color=green]
        >> Dim myWord As New Word.Applicatio n
        >> myWord.Document s.Add("c:\templ ate.dot")
        >> 'fill the fields
        >> '...
        >> 'print
        >> myWord.ActiveDo cument.PrintOut ()
        >> myWord.Quit(Wor d.WdSaveOptions .wdDoNotSaveCha nges)
        >>
        >> So what code is needed before I print the document to fill my template
        >> fields?[/color]
        >
        >
        > \\\
        > Option Strict Off
        > .
        > .
        > .
        > Dim WordApp As Object = CreateObject("W ord.Application ")
        > Dim WordDoc As Object = WordApp.Documen ts.Open("C:\Hal lo.doc")
        > WordDoc.FormFie lds("Text1").Re sult = "Bla"
        > WordDoc.Save()
        > WordDoc.Close()
        > WordApp.Quit()
        > ///
        >[/color]

        Thanks for the tip, but my document doesn't seem to have any items in
        the 'FormFields' property, and neither does the 'Fields' and the
        'Variables' property. Are there any other properties to look at?

        Thanks in advance.

        Steven

        Comment

        • steven

          #5
          Re: Fill Word-template fields

          Robin Tucker wrote:
          [color=blue]
          > You can read/write the document variables collection. Something like this:
          > (might not be exactly like this!):
          >
          >
          > For i as Integer = 0 To TheDocument.Var iables.Count
          > Select Case theDocument.Var iables.Item(i). Name
          > Case "a_name"
          > theDocument.Var iables.Item(i). Value = "test"
          > End Select
          > Next
          >
          >
          > Something to note: make sure you clean up all references correctly :)
          >[/color]

          Thanks for the tip, but my document doesn't seem to have any items in
          the 'Variables' property, and neither does the 'Fields' and the
          'FormFields' property. Are there any other properties to look at?

          Thanks in advance.

          Steven

          Comment

          • steven

            #6
            Re: Fill Word-template fields

            Thanks, that worked (eventually) great.

            Steven

            Herfried K. Wagner [MVP] wrote:[color=blue]
            > "steven" <user@example.n et> schrieb:
            >[color=green]
            >> Dim myWord As New Word.Applicatio n
            >> myWord.Document s.Add("c:\templ ate.dot")
            >> 'fill the fields
            >> '...
            >> 'print
            >> myWord.ActiveDo cument.PrintOut ()
            >> myWord.Quit(Wor d.WdSaveOptions .wdDoNotSaveCha nges)
            >>
            >> So what code is needed before I print the document to fill my template
            >> fields?[/color]
            >
            >
            > \\\
            > Option Strict Off
            > .
            > .
            > .
            > Dim WordApp As Object = CreateObject("W ord.Application ")
            > Dim WordDoc As Object = WordApp.Documen ts.Open("C:\Hal lo.doc")
            > WordDoc.FormFie lds("Text1").Re sult = "Bla"
            > WordDoc.Save()
            > WordDoc.Close()
            > WordApp.Quit()
            > ///
            >[/color]

            Comment

            Working...