Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Filling in Word document from VB.NET 2003

Posted on 2007-04-03
1
Medium Priority
?
372 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 1500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

636 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