Solved

Filling in Word document from VB.NET 2003

Posted on 2007-04-03
1
362 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

806 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