Results 1 to 3 of 3

Thread: MS Word automation - please help!

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    15

    MS Word automation - please help!

    I am using automation with MS Word 2007 to open a .docx and send some string data to it for printing purposes. I cannot figure out how to insert the current date and time on the document at runtime (from VB form code). There is a insertdatetime() function, but I don't know how to send parameter. In the code below...for 'opara2', there is a line where I display the string "generated on" and then after that is where I want Word to insert the current date. Can anyone help??


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Dim oWord As Word.Application
    Dim oDoc As Word.Document
    Dim oTable As Word.Table
    Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph
    Dim oPara3 As Word.Paragraph, oPara4 As Word.Paragraph
    Dim oRng As Word.Range
    Dim oShape As Word.InlineShape


    oWord = CreateObject("Word.Application")
    oWord.Visible = True
    oDoc = oWord.Documents.Add

    oPara1 = oDoc.Content.Paragraphs.Add
    oPara1.Range.Text = "YOUR LUCKY NUMBERS"
    oPara1.Range.Font.Color = Word.WdColor.wdColorBlue
    oPara1.Range.Font.Bold = True
    oPara1.Range.Font.Size = 36
    oPara1.Format.SpaceAfter = 12
    oPara1.Range.InsertParagraphAfter()

    oPara2 = oDoc.Content.Paragraphs.Add
    oPara2.Range.Text = "Generated on " &
    oPara2.Range.Font.Color = Word.WdColor.wdColorRed
    oPara2.Range.Font.Bold = True
    oPara2.Range.Font.Size = 36
    oPara2.Format.SpaceAfter = 24 '24 pt spacing after paragraph.
    oPara2.Range.InsertParagraphAfter()

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: MS Word automation - please help!

    try this:

    vb Code:
    1. Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    2.     Dim oWord As Word.Application
    3.     Dim oDoc As Word.Document
    4.     Dim oTable As Word.Table
    5.     Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph
    6.     Dim oPara3 As Word.Paragraph, oPara4 As Word.Paragraph
    7.     Dim oRng As Word.Range
    8.     Dim oShape As Word.InlineShape
    9.  
    10.     oWord = CreateObject("Word.Application")
    11.     oWord.Visible = True
    12.     oDoc = oWord.Documents.Add
    13.    
    14.     oPara1 = oDoc.Content.Paragraphs.Add
    15.     oPara1.Range.Text = "YOUR LUCKY NUMBERS"
    16.     oPara1.Range.Font.Color = Word.WdColor.wdColorBlue
    17.     oPara1.Range.Font.Bold = True
    18.     oPara1.Range.Font.Size = 36
    19.     oPara1.Format.SpaceAfter = 12
    20.     oPara1.Range.InsertParagraphAfter()
    21.  
    22.     oPara2 = oDoc.Content.Paragraphs.Add
    23.     oPara2.Range.Text = "Generated on " & Now.ToString("dd MMMM yyyy hh:mm:ss tt")
    24.     oPara2.Range.Font.Color = Word.WdColor.wdColorRed
    25.     oPara2.Range.Font.Bold = True
    26.     oPara2.Range.Font.Size = 36
    27.     oPara2.Format.SpaceAfter = 24 '24 pt spacing after paragraph.
    28.     oPara2.Range.InsertParagraphAfter()

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    15

    Re: MS Word automation - please help!

    Thanks - just what I was looking for! Unfortunately, I still have a lot to learn and I am grateful for this forum and the helpful people here..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width