Solved

Filling in Word document from VB.NET 2003

Posted on 2007-04-03
1
366 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
[X]
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
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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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