![]() |
|
|
|
#1
|
||||
|
||||
|
To show the principles involved, from Outlook, create a message that contains some text and four bookmarked locations bmTest1 to bmTest4. Save it as a template called 'Bookmark Test.oft'.
Then create the following macro in a new VBA module. Change the template path as appropriate. Then run the macro. It will create a new message based on the template and fill the four named bookmarks with the four values indicated. In practice you would collect the values from a userform http://www.gmayor.com/Userform.htm or from some other location, but this post is just to demonstrate the basics. Code:
Option Explicit
Sub CreateMessageWithBM()
Dim olItem As MailItem
Dim olEmail As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object
Dim vBM As Variant
Dim i As Long, j As Long
Dim strBMText As String
Const strBookmarks As String = "bmTest1|bmTest2|bmTest3|bmTest4"
vBM = Split(strBookmarks, "|")
Set olItem = CreateItemFromTemplate("D:\Word 2016 Templates\Bookmark test.oft")
With olItem
.BodyFormat = olFormatHTML
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
.Display
For j = 0 To UBound(vBM)
Select Case j
Case Is = 0: strBMText = "This is the value for bookmark 1"
Case Is = 1: strBMText = "This is the value for bookmark 2"
Case Is = 2: strBMText = "This is the value for bookmark 3"
Case Is = 3: strBMText = "This is the value for bookmark 4"
End Select
FillBM wdDoc, CStr(vBM(j)), strBMText
Next j
End With
lbl_Exit:
Exit Sub
End Sub
Private Sub FillBM(odoc As Object, strbmName As String, strValue As String)
'Graham Mayor - http://www.gmayor.com
Dim oRng As Object
With odoc
On Error GoTo lbl_Exit
Set oRng = .Bookmarks(strbmName).Range
oRng.Text = strValue
oRng.Bookmarks.Add strbmName
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Automate repeating text
|
tchav | Word | 3 | 04-18-2016 04:17 AM |
| Can I automate an email by category | brandy | Outlook | 1 | 01-08-2013 11:55 AM |
| How to automate all slides | Pemberton | PowerPoint | 1 | 11-09-2011 04:39 PM |
| Looking for a way to automate moving emails to folder | middletree | Outlook | 1 | 09-28-2010 01:24 PM |
| Automate Excel tabname | deegz | Excel | 2 | 12-08-2008 02:57 AM |