Solved

Filling in Word document from VB.NET 2003

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now