Consulting

Results 1 to 4 of 4

Thread: Word VBA to create open Outlook oft file

  1. #1
    VBAX Regular
    Joined
    May 2014
    Posts
    46
    Location

    Lightbulb Word VBA to create open Outlook oft file

    Hiya all,

    I want to try to get word 2007 VBA code to:

    1. Open a outlook oft file (which has some tables etc already in it)
    2. Insert some email addresses of attendees (which I am getting from content controls)
    3. Insert the start date (end date is the same) and insert start/end time (again from content controls)
    4. Insert text into the subject (again from a content control)
    5. Ideally find the string "OC" and navigate to the next cell and insert some text from a content control.

    If anyone can please help with this, ie a example code, I will be very grateful.

    Best

    -Al

  2. #2
    Let's assume your OFT template is as indicated in the code below and that it contains tables, the first one of which has the letters OC in one of the cells. That being the case the following will locate that cell and type the indicated text in the next cell.

    If you are familiar with ranges and Word VBA then accessing the tables and the individual cells is straightforward. The macro shows a few techniques you can adapt.

    Sub MessageFromTemplate()
    Dim olItem As Object
    Dim olInsp As Object
    Dim wdDoc As Object
    Dim oTable As Object
    Dim oRng As Object
    Dim oFound As Object
    Dim oBody As Object
        Set olItem = Application.CreateItemFromTemplate("D:\Word 2010 Templates\TableTemplate.oft")
        With olItem
            .To = "someone@somewhere.com"
            .Subject = "The message Subject"
            .BodyFormat = 2 'html
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            Set oBody = wdDoc.Range(0, 0)        'the start of the message body
            oBody.Text = "Put this text at the start of the message" 'which could be the text from your content control
            Set oTable = wdDoc.Range.tables(1)
            Set oRng = oTable.Range
            With oRng.Find
                Do While .Execute(FindText:="OC")
                    Set oFound = oRng.cells(1).Next.Range
                    oFound.Text = "Test"
                    Exit Do
                Loop
            End With
            'Move the range to the end of Table 1
            Set oBody = wdDoc.Range.tables(1).Range
            oBody.collapse 0
            oBody.End = oBody.End + 1
            oBody.collapse 0
            'And write some text
            oBody.Text = "Text after the table"
            .Display
        End With
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Duplicate - deleted
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    VBAX Regular
    Joined
    May 2014
    Posts
    46
    Location
    Thank you.

Tags for this Thread

Posting Permissions

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