locked
Creating Microsoft Word document RRS feed

  • Question

  • An application I am developing needs to generate a Word document. The output would be similar to the following:

    1. Blah - This is some blah text.
    2. Blah 2 - This is some more blah text.
    3. Blah 3 - This is even more blah text.

    And so on.

    I have a collection right now, and I think I should have VB step through each item in the collection, putting the appropriate text up, but I'm not sure how to get it to work. I've looked at support.microsoft.com/kb/316383 (or something similar to this at Microsoft) (there is http://support.microsoft.com/kb/313193, which may be useful).

    I can dim a paragraph beforehand, but how should I dim a whole bunch of paragraphs, depending on how many items there are? (That is, I don't know how many item there will be.)

    Also, if I use the Office Word library provided by Microsoft to generate a document, how does that affect distribution?

    Thanks.

    samcanlinux

    Tuesday, April 13, 2010 8:48 PM

Answers

  • samcanlinux

     

    Try some thing like this?

     

    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            'add items to collection
            Dim mycollection As New Collection
            mycollection.Add("Blah - This is some blah text.")
            mycollection.Add("Blah 2 - This is some more blah text.")
            mycollection.Add("Blah 3 - This is even more blah text.")
    
            Dim oWord As Word.Application
            Dim oDoc As Word.Document
            Dim oPara1 As Word.Paragraph
    
            'Start Word and open the document template.
            oWord = CreateObject("Word.Application")
            oWord.Visible = True
            oDoc = oWord.Documents.Add
    
            For Each collectionitem In mycollection
                'Insert a paragraph.
                oPara1 = oDoc.Content.Paragraphs.Add
                oPara1.Range.Text = collectionitem
                oPara1.Range.Font.Size = 10
                oPara1.Range.Font.Bold = False
                oPara1.Format.SpaceAfter = 24    '24 pt spacing after paragraph.
                oPara1.Range.InsertParagraphAfter()
            Next
    
    
    
            oDoc.SaveAs("C:\mydoc.doc") 'save word document
    
        End Sub
    End Class
    

    • Marked as answer by Jeff Shan Monday, April 19, 2010 9:26 AM
    Tuesday, April 13, 2010 9:33 PM
  • Thank you for replying...I looked at a thread (http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/d4d3e7d6-3d16-418b-879a-0f8cadbcb3fb/) and also at http://support.microsoft.com/kb/313193, and I pieced something together I think will work. Controlling Word using Visual Basic isn't very intuitive, unfortunately.

    Thank you for your help.

    samcanlinux

    • Marked as answer by Jeff Shan Monday, April 19, 2010 9:26 AM
    Tuesday, April 13, 2010 9:53 PM

All replies

  • Hello Samcanlinux

    can you have a look here, and try to get something from them>

    http://www.builderau.com.au/program/dotnet/soa/Easily-utilise-Microsoft-Word-functionality-in-your-NET-application/0,339028399,339198903,00.htm


    Just Be Humble Malange!
    Tuesday, April 13, 2010 9:01 PM
  • samcanlinux

     

    Try some thing like this?

     

    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            'add items to collection
            Dim mycollection As New Collection
            mycollection.Add("Blah - This is some blah text.")
            mycollection.Add("Blah 2 - This is some more blah text.")
            mycollection.Add("Blah 3 - This is even more blah text.")
    
            Dim oWord As Word.Application
            Dim oDoc As Word.Document
            Dim oPara1 As Word.Paragraph
    
            'Start Word and open the document template.
            oWord = CreateObject("Word.Application")
            oWord.Visible = True
            oDoc = oWord.Documents.Add
    
            For Each collectionitem In mycollection
                'Insert a paragraph.
                oPara1 = oDoc.Content.Paragraphs.Add
                oPara1.Range.Text = collectionitem
                oPara1.Range.Font.Size = 10
                oPara1.Range.Font.Bold = False
                oPara1.Format.SpaceAfter = 24    '24 pt spacing after paragraph.
                oPara1.Range.InsertParagraphAfter()
            Next
    
    
    
            oDoc.SaveAs("C:\mydoc.doc") 'save word document
    
        End Sub
    End Class
    

    • Marked as answer by Jeff Shan Monday, April 19, 2010 9:26 AM
    Tuesday, April 13, 2010 9:33 PM
  • Thank you for replying...I looked at a thread (http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/d4d3e7d6-3d16-418b-879a-0f8cadbcb3fb/) and also at http://support.microsoft.com/kb/313193, and I pieced something together I think will work. Controlling Word using Visual Basic isn't very intuitive, unfortunately.

    Thank you for your help.

    samcanlinux

    • Marked as answer by Jeff Shan Monday, April 19, 2010 9:26 AM
    Tuesday, April 13, 2010 9:53 PM