Problems accesing created bookmark in VBA
- From: epsilon_9 <epsilon9@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 11 Jan 2007 04:49:00 -0800
Hi
I am trying t create a macro for outlook where e-mail signatures are
inserted depending on if recipients names are resolved or not i.e if there is
an "@" in any of the recipients fields
I have created a class module with a MailItem object and it handles certain
events. I created a bookmark (_MailAutoSig) that the word editor of outlook
apparently uses to insert signatures. I have been able to create the bookmark
when a mail is opned but in another MailItem eventhandler
(item_BeforeCheckNames) I am unable to access the bookmark. I keep gettin an
error indicating that the bookmark does not exist even after using the edit
got menu command of word editor to access the bookmark.
Extracts from my code are below. Any help will be appreciated
The following is the class module (clsInspector)
Option Explicit
Dim WithEvents oAppInspectors As Outlook.Inspectors
Dim WithEvents oOpenMail As Outlook.MailItem
Dim oAppInspector As Outlook.Inspector
Dim oDoc As Word.Document
Dim oSel As Word.Selection
Dim oCB As Office.CommandBar
Dim colCBControls As Office.CommandBarControls
Dim oCBB As Office.CommandBarButton
'Class Initialize
Private Sub Class_Initialize()
'oAppInspectors Returns a handle to the Inspectors collection
Set oAppInspectors = Application.Inspectors
End Sub
Private Sub oAppInspectors_NewInspector(ByVal Inspector As Inspector)
'Event gets triggered every time a Window or Item is opened in Outlook
Interface
'Like: E-mail, Contacts, Tasks
If Inspector.CurrentItem.Class <> olMail Then
'Only deal with Email Items...else exit
Exit Sub
End If
'Set a reference to the e-mail inspector
Set oAppInspector = Inspector
'Set a reference to the e-mail to trap the Open event
Set oOpenMail = Inspector.CurrentItem
End Sub
Private Sub oOpenMail_BeforeCheckNames(Cancel As Boolean)
'Dim oRcpnt As Outlook.Recipient
'Dim oRcpnts As Outlook.Recipients
If oAppInspector.EditorType = olEditorWord Then
Set oDoc = oAppInspector.WordEditor
Set oSel = oDoc.Application.Selection
If InStr(oOpenMail.To, "@") Or InStr(oOpenMail.CC, "@") Or
InStr(oOpenMail.BCC, "@") Then
'oSel.GoTo What:=wdGoToBookmark, Name:="\StartOfDoc"
oDoc.Bookmarks.ShowHidden = True
Debug.Print oDoc.Bookmarks.Count
oSel.GoTo What:=wdGoToBookmark, Name:="_MailAutoSig"
'oDoc.Bookmarks("_MailAutoSig").Select
Set oCB = oDoc.CommandBars("AutoSignature Popup")
If Not oCB Is Nothing Then
Set colCBControls = oCB.Controls
End If
If Not colCBControls Is Nothing Then
For Each oCBB In colCBControls
If oCBB.Caption = "AD Signature" Then
oCBB.Execute
Exit For
End If
Next
End If
End If
End If
End Sub
Private Sub oOpenMail_Open(Cancel As Boolean)
'Event gets triggered if oOpenMail is opened!
If oAppInspector.EditorType = olEditorWord Then
' next statement will trigger security prompt
' in Outlook 2002 SP3
Set oDoc = oAppInspector.WordEditor
Set oSel = oDoc.Application.Selection
If Not oDoc.Bookmarks.Exists("_MailAutoSig") Then
oSel.TypeParagraph
oSel.TypeParagraph
oDoc.Bookmarks.Add Range:=oSel.Range, Name:="_MailAutoSig"
End If
Debug.Print oDoc.Bookmarks.Count
'oSel.GoTo What:=wdGoToBookmark, Name:="_MailAutoSig"
'Set oCB = oDoc.CommandBars("AutoSignature Popup")
'If Not oCB Is Nothing Then
'Set colCBControls = oCB.Controls
'End If
'If Not colCBControls Is Nothing Then
'For Each oCBB In colCBControls
'If oCBB.Caption = "AD Signature" Then
'oCBB.Execute
'Exit For
'End If
'Next
'End If
oSel.GoTo What:=wdGoToBookmark, Name:="\StartOfDoc"
End If
End Sub
The following is the declaration and initialisation of the class module in
the ThisoutlookSession module
Dim TrapInspector As clsInspector
Private Sub Application_Startup()
On Error Resume Next
'Event gets triggered when you start Outlook
'Initialize class "clsInspector"
Set TrapInspector = New clsInspector
Thanks
Andy.
.
- Follow-Ups:
- Re: Problems accesing created bookmark in VBA
- From: Michael Bauer [MVP - Outlook]
- Re: Problems accesing created bookmark in VBA
- Prev by Date: Re: Set Signature Setting using VBA
- Next by Date: Re: BeforeUninstall isn't working in my code!!!
- Previous by thread: sending selected email as attachment in new email
- Next by thread: Re: Problems accesing created bookmark in VBA
- Index(es):