Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Filling in Word document from VB.NET 2003

Posted on 2007-04-03
1
363 Views
Last Modified: 2011-09-20
How can I fill in certain sections of a Word document from a VB.NET 2003 WinForm application? I have a contract in Word format, and I will like to fill in certain sections from my VB app.

Thanks
0
Comment
Question by:SETP
1 Comment
 
LVL 28

Accepted Solution

by:
iboutchkine earned 500 total points
ID: 18849849
you have to define bookmarks and then enter info at the bookmark. Unfortunately I have only VB6 example, but it will be easy to convert to .NET

Defined bookmarks in your Word document.

From your VB application, you can access bookmark with code like this:

With pob_WordApp
      .ActiveDocument.bookmarks("bookmarkname").Select
      .Selection.InsertBefore "NewValue"
End With

'we have to define bookmarks in word document and then take some value and substitute bookmarks

Here is some code which will open a specified word document and set some text into a specified bookmark.
You can easily amend this to do what you need.

Private Sub PrintDocument(ByVal strDocument As String)
   Dim appWord As Object
   Dim docLetter As Object
   Dim bmkGoto As Object
   On Error Resume Next
   Set appWord = GetObject(, "Word.Application")
   If appWord Is Nothing Then
       Set appWord = CreateObject("Word.Application")
   Else
       blnExistingSession = True
   End If
   On Error GoTo 0
       Set docLetter = appWord.Documents.Open(App.Path & "\" & strDocument)
   SetBookmark docLetter, "Bookmark Name", "Value
   docDepetal.PrintOut False
   docDepetal.Close False
   If Not blnExistingSession Then
       appWord.Quit False
   End If
   Set docDepetal = Nothing
   Set appWord = Nothing
End Sub

Private Sub SetBookmark(ByRef docLetter As Object, ByVal strBookmark As String, ByVal strText As String,
Optional ByVal strColour As String = "Black", Optional ByVal strFont As String = "NA")
   Dim rngBookmark As Object
   Dim fntText As Object
   Set fntText = New Word.Font
   With docLetter
       lngPos = .Bookmarks(strBookmark).End
       Select Case strColour
       Case "Red"
           fntText.ColorIndex = wdRed
       Case "Green"
           fntText.ColorIndex = wdGreen
       Case "Black"
           fntText.ColorIndex = wdBlack
       Case "Blue"
           fntText.ColorIndex = wdBlue
       End Select
       docLetter.Application.Selection.Collapse
       fntText.Name = strFont
       Set rngBookmark = .Range(Start:=lngPos + 1, End:=lngPos + 1)
       docLetter.Application.Selection.Font.ColorIndex = fntText.ColorIndex
       If strFont = "NA" Then
           rngBookmark.Text = strText
       Else
           rngBookmark.Select
           docLetter.Application.Selection.Font.ColorIndex = fntText.ColorIndex
           If strText <> "NA" Then
               docLetter.Application.Selection.InsertSymbol Font:=strFont, CharacterNumber:=CLng(strText),
Unicode:=True
           Else
               rngBookmark.Text = strText
           End If
           rngBookmark.Select
           docLetter.Application.Selection.Expand (wdWord)
           docLetter.Application.Selection.Font.ColorIndex = fntText.ColorIndex
       End If
   End With
   DoEvents
End Sub
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question