export data from vb.net into ms-word

Hi,
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.
LVL 3
mte01Asked:
Who is Participating?
 
bruintjeCommented:
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
0
 
bruintjeCommented:
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
bruintje
0
 
mte01Author Commented:
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
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
bruintjeCommented:
are your images coming from the application or just from disk and need to be linked into the document?
0
 
mte01Author Commented:
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?
0
 
bruintjeCommented:
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

?
0
 
mte01Author Commented:
yeah exactly
0
 
mte01Author Commented:
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.
0
 
bruintjeCommented:
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 :)
http://msdn.microsoft.com/office/
http://msdn.microsoft.com/office/understanding/word/default.aspx






0
 
mte01Author Commented:
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
0
 
bruintjeCommented:
thanks for the grade, be sure to check out the msdn site links above they will help a bit
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.