PDA

View Full Version : Word VBA to create open Outlook oft file



bigal.nz
11-02-2014, 04:58 PM
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

gmayor
11-03-2014, 08:05 AM
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

gmayor
11-03-2014, 08:07 AM
Duplicate - deleted

bigal.nz
11-08-2014, 08:55 PM
Thank you.