[Webinar] Learn how to a build a cloud-first strategyRegister Now


export data from vb.net into ms-word

Posted on 2006-04-20
Medium Priority
Last Modified: 2012-08-13
i need a simple example that demonstrate how to export data and images (create tables) from vb.net to MS-Word.
all i was able to find is how to export data into excell.
thanks for the help.
Question by:mte01
  • 6
  • 5
LVL 44

Expert Comment

ID: 16495731
Hello mte01,

you could look at a small sample from
source: http://experts.about.com/q/VB-NET-3306/Automation-Inserting-table-MS.htm
Dim appMSWord As New Microsoft.Office.Interop.Word.Application
Dim docNew as Microsoft.Office.Interop.Word.Document = appMSWord.Documents.Add()

appMSWord.Visible = true

'Once the application is launched and you have a document open, use the following code:
tblTable = appMSWord.ActiveDocument.Tables.Add(appMSWord.Selection.Range, n, m)"

'To set the text of cells use the code:
tblTable.Cell(Row, Column).Range.Text = "Text" '"1, 1" represents row 1, column 1

a more general sample of automating word from vb.net
source: http://support.microsoft.com/?kbid=303294

hope this helps a bit

Author Comment

ID: 16496994
hi bruintje
this example: http://support.microsoft.com/?kbid=303294 is working but it doesnt help creating word documents, it only set the properties of the word document.
and the first example is not working, for they dont say the type of the object tblTable, and they have a problem with the reference thing that i couldnt solve...
i need a simple example that insets text and images into a word document..
any more idea's ??
thanks again
LVL 44

Expert Comment

ID: 16497058
are your images coming from the application or just from disk and need to be linked into the document?
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!


Author Comment

ID: 16497104
i have the images on the disk, but im building a report.. so dont know if it would be nice to give the client a folder that contain the word document and a folder containing the images (like the HTML)
dont know if this answers ur question?
LVL 44

Expert Comment

ID: 16497119
sorry i was not clear i meant

was this what you're looking for

1. create new document
2. insert some images from disk
3. insert some text from your application
4. save the document and send it to the client


Author Comment

ID: 16497151
yeah exactly
LVL 44

Accepted Solution

bruintje earned 2000 total points
ID: 16497395
i mixed the code from the MS article with some late binding instead of referencing this makes using intellisense moot but you have no problem with versions on the client side

this was only a button on a form

  Private Sub Button1_Click(ByVal sender As System.Object, _
                            ByVal e As System.EventArgs) _
                            Handles Button1.Click
    Dim oWord As Object
    Dim oDoc As Object
    Dim oTable As Object
    Dim oSelection As Object
    Dim strValue As String

    'Create an instance of Word and make it visible.
    oWord = CreateObject("Word.Application")
    oWord.Visible = True
    'Create a new document
    oDoc = oWord.Documents.Add()
    oSelection = oWord.Selection.Range
    'Get a new table of 2 x 2
    oTable = oDoc.Tables.Add(oSelection, 2, 2)
    'Insert a new string into cell 1,1
    strValue = "hello there"
    oTable.Cell(1, 1).Range.Text = strValue
    oSelection = oTable.Cell(1, 2).Range
    oSelection.InlineShapes.AddPicture(FileName:= _
      "C:\Documents and Settings\Username\My Documents\My Pictures\bull.gif", _
      LinkToFile:=False, SaveWithDocument:=True)

    'Clean up. We'll leave Word running.
    oSelection = Nothing
    oTable = Nothing
    oDoc = Nothing
    oWord = Nothing

  End Sub

if you want intellisense then using a reference is easier

Author Comment

ID: 16497442
thanks it's working perfectly.. just one more request
to insert notmal text (without tables) and if i can set this text to bold italic....
and if u know someplace where i can learn to set the properties of the font and how to do other things, i cant seem to find a tutorial on such things.
LVL 44

Expert Comment

ID: 16497785
well you could look at a sample http://www.kdkeys.net/forums/thread/691.aspx

but most of the time i use the macro recorder in word or excel to get to the VBA code and port it to other languages

with the recorder doing the initial work as i did for the sample above and the object browser to see everything in the object models its enough to start with

the vb editor in word is opened by using ALT+F11
the object browser window in that editor is F2

great place on the net with a great editor :)


Author Comment

ID: 16498040
it's just that i didnt understand the way the macro works... (sorry)
but the first example was helpfull.
anyways thanks bruintje for your help
LVL 44

Expert Comment

ID: 16498065
thanks for the grade, be sure to check out the msdn site links above they will help a bit

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Integration Management Part 2
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

868 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