Automate MS Word Doc with VB.Net

I amtrying to pull info from a Sql Server to populate a word Doc using a VB.Net App any ideas


Answer this question

Automate MS Word Doc with VB.Net

  • Nokoff

    Object reference not set to an instance of an object what does that mean

    Dim Policy As DataTable

    Dim PolicyNumber As DataColumn

    Dim wordApp As Word.Application

    Dim wordDoc As Word.Document

    Dim Int As Integer

    Dim G As Word.Document

    Dim L As Word.Document

    Dim M As Word.Document

    Dim rng As Word.Range

    GalicLPC.Fields.Update.ToString("PolNum")

    PolicyNumber.DataType.GetField("PolNum")

    I am trying to update a field in Ms word with information i am getting from a Sql database any reason why i get hat unhandeled exception...and is this even the right track any help out there Sad



  • Darryn Lavery -- MSFT


    Dim PolNum As Word.Field


     



    Is this making the field in a word document available in a VB.Net app...i have a field called PolNum in my ms word document....is this so i can access it through my app



  • gary23

    You need to create Data Connection, Data Adapter, and a Dataset. For more details on these steps, follow this walkthrough: http://msdn.microsoft.com/library/default.asp url=/library/en-us/vbcon/html/vbwlkwalkthroughdisplayingdatafromsingletableinwindowsform.asp.

    Let say we have a Data Connection to a Northwind database, and a Data Adapter with the query "Select * From Customers". To get all the values in "ContactName" column:



    Dim myDataSet As New DataSet
    Me.SqlDataAdapter1.Fill(myDataSet)
    Dim myDataTable As DataTable = myDataSet.Tables(0)
    For Each myDataRow As DataRow In myDataTable.Rows
       
    Dim myContactName As String = myDataRow("ContactName")
       
    ' Do something with the value.
    Next

     


    Your PolicyNumber column should work in the similar faction.

    Please let me know if you think the MSDN documentation needs improvement (why it is hard for you to find the answers from reading them ).

    Regards,


  • Derek Newkirk

    I'm not an expert, so this may or may not help, but it looks like 
       Dim wordDoc As Word.Document
    should be 
       Dim wordDoc As New Word.Document

    Good Luck.

  • nbezalel


    Dim LPC As Word.Document

    PolNumber.DataBindings.Add("Text", PolicyNumber1, "Policy.PolicyNumber")


     



    Well i binded the data to the textbox but what should i do to make the data show up on the ms word doc



  • Hong_Zhang75252

    Ok so i guess the better question would be how to set...Policy Number to the DataColumn so i can return the values in the column

  • UdiAms

    I am a little confused, where does the contact name come it at and what value are you using the DataSet or the contact name


    Dim PolicyNumber As New DataSet

    Me.SqlDataAdapter1.Fill(PolicyNumber)

    Dim Policy As DataTable = PolicyNumber.Tables(0)

    For Each myDataRow As DataRow In Policy.Rows

    Dim PolicyNumber1 As String = myDataRow("PolicyNumber")

    ' Do something with the value.

    Next


     



    Now I wanna take the value of the Policy number from the database and insert it into a MS word Document field....So when i enter a policy number and click btnExtraxt and where the field is in the letter will be the policynumber i inputted in the textbox2.text



  • Paul D.

    Trying to get the data from the policy rows to be recongnized by the PolNumber Text...So that when i fill it in with the number and click the extract button it fills in the field in MS word...thats possibly right


    PolNumber.Text = Policy.Rows


     



  • jerrodbug

    Sorry I'm not familiar with programming Office so I can only point you to certain documentation.
    - For VS 2003 Programming with Office, you can check out the content at http://msdn.microsoft.com/library/default.asp url=/library/en-us/dv_wrcore/html/oriIntegratingOfficeComponents2.asp.
    - Here's an article on how to insert text into Word: http://msdn.microsoft.com/library/default.asp url=/library/en-us/dv_wrcore/html/wrtskHowToInsertTextInWordDocument.asp.

    As for the exception above, it means you attempt to use an object without initializing it. In this case, PolicyNumber is Nothing, so when you try to access PolicyNumber.DataType, it will throw this NullReferenceException.

    You can check out http://msdn.microsoft.com/library/default.asp url=/library/en-us/vbcon/html/vboriCodeExamplesForDataAccess.asp for code samples for Data Access.

    Regards,


  • Aner Ben-Artzi

    Hi, it can be done, but not without some understanding of how DataSet, DataTable, and Word documents work.

    From your question, it looks like you want to
    1. Get a policy number from a text box
    2. Query a database to get the policy row from a data table
    3. Fill in the Word document with the values from the data table.

    To do 2., please try this walthrough on how to use a parameterized query with Windows Form.
    http://msdn.microsoft.com/library/default.asp url=/library/en-us/vbcon/html/vbwlkwalkthroughdisplayingdatainwindowsformusingparameterizedquery.asp. You can apply the same techniques to get the data you need, not displaying them, but using them to fill in the Word document.

    To do 3., here's an article on how to insert text into a Word document
    http://msdn.microsoft.com/library/default.asp url=/library/en-us/dv_wrcore/html/wrtskHowToInsertTextInWordDocument.asp.

  • Chad Wach

    Do they have any books with expamples like this....I need to get one How do I debug in the application

  • shwe

    Dim PolNum As Word.Field

    Your line of code above just declares a variable named 'PolNum' of type World.Field.

    I believe you need to do something with the variable to actually associate it with the PolNum field in your MS Word document. Please refer to the MSDN articles showing how to do this. You can debug through your application and check.

    Regards,



  • icelava

    I am using Visual Studio 2003 and i had particular things i want to populate...I want to pull a first and last name along with an address from a database and put it into a letter...using VB.net i would like to be able to type in a name and in my word document i have a field called <<Name>> and in my database its called Name, so my textbox should recongnize and match them up and add it to the word document....it that possible

  • livehed

    Hi,

    If you're using Visual Studio 2005, a good place to start is Visual Studio Tools for Office:
    http://msdn2.microsoft.com/en-us/library/d2tx7z6d(en-US,VS.80).aspx

    There is a section for automating Microsoft Word tasks:
    http://msdn2.microsoft.com/en-us/library/78whx7s6

    Regards


  • Automate MS Word Doc with VB.Net