PDA

View Full Version : Solved: mailitem.reply/replyall vs mailitem.forward



dakotafig
01-21-2012, 09:09 PM
I have some code that I have been working on to deal with the outlook problem of not defaulting to HTML format. The code works great for repy and reply all, but errors with a forward. By the way, I like this way much better than the mailitem.bodyformat = HTML because that method tends to screw with the font. So, here is my code, and does anyone have any ideas as to why the forward doesn't work? This is the line that the forward doesn't work:
ActiveInspector.CommandBars.FindControl(ID:=5564).Execute

Thanks.








OptionExplicit





Private WithEvents oExpl As Explorer


Private WithEvents oItem As MailItem


Private bDiscardEvents As Boolean


Private olFormat As OlBodyFormat








Private Sub Application_Startup()


'set default reply format to HTML





Set oExpl = Application.ActiveExplorer





bDiscardEvents = False


olFormat = olFormatHTML





End Sub








Private Sub oExpl_SelectionChange()















On ErrorResume Next





Set oItem = oExpl.Selection.Item(1)





End Sub








Private Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean)


Call do_format(oItem.Reply, Cancel)


End Sub





Private Sub oItem_ReplyAll(ByVal Response As Object, Cancel As Boolean)


Call do_format(oItem.ReplyAll, Cancel)


End Sub


















Private Sub oItem_Forward(ByVal Forward As Object, Cancel As Boolean)





Call do_format(oItem.Forward, Cancel)





End Sub

















Private Sub do_format(mail_item As MailItem, Cancel As Boolean)





Dim format_needed As Boolean





'Set formating of email





If bDiscardEvents Or oItem.BodyFormat = olFormat Then





format_needed = False





Else





format_needed = True





End If





Cancel = True





bDiscardEvents = True





mail_item.Display





If format_needed Then


ActiveInspector.CommandBars.FindControl(ID:=5564).Execute





Call insert_sig





bDiscardEvents = False





End Sub








Sub insert_sig()





On Error Resume Next











Dim strFolder As String





Dim Signature As String





Dim sig1 As String





Dim sig2 As String





sig1 = "sig1"





sig2 = "sig2"





strFolder = ActiveExplorer.CurrentFolder.FolderPath





strFolder = Mid(strFolder, 3, InStr(3, strFolder, "\") - 3)





If strFolder = sig1 Then





Signature = "sig1"





ElseIf strFolder = ST Then





Signature = "sig1"





Else





'no signature





Exit Sub





End If





ActiveInspector.CommandBars("Menu Bar"). _





Controls("Insert"). _





Controls("Signature"). _





Controls(Signature).Execute


End Sub

Sebastian H
01-23-2012, 02:18 PM
Hi there,
If you're wondering why you haven't received a reply; that may be because your code is very hard to read, with hundreds of empty lines. Can you please delete those? Thanks.

dakotafig
01-23-2012, 02:40 PM
This may sound kind of dumb, but I can't find the thread edit anywhere....... I also put the vba tags around the code so I don't see any spaces in the code at all, but who knows, i have screwed up simpler things before. So, here is stab number two below:

OptionExplicit

Private WithEvents oExpl As Explorer
Private WithEvents oItem As MailItem
Private bDiscardEvents As Boolean
Private olFormat As OlBodyFormat

Private Sub Application_Startup()
'set default reply format to HTML
Set oExpl = Application.ActiveExplorer
bDiscardEvents = False
olFormat = olFormatHTML
End Sub

Private Sub oExpl_SelectionChange()
On ErrorResume Next
Set oItem = oExpl.Selection.Item(1)
End Sub

Private Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean)
Call do_format(oItem.Reply, Cancel)
End Sub

Private Sub oItem_ReplyAll(ByVal Response As Object, Cancel As Boolean)
Call do_format(oItem.ReplyAll, Cancel)
End Sub

Private Sub oItem_Forward(ByVal Forward As Object, Cancel As Boolean)
Call do_format(oItem.Forward, Cancel)
End Sub

Private Sub do_format(mail_item As MailItem, Cancel As Boolean)
Dim format_needed As Boolean
'Set formating of email
If bDiscardEvents Or oItem.BodyFormat = olFormat Then
format_needed = False
Else
format_needed = True
End If
Cancel = True
bDiscardEvents = True
mail_item.Display
If format_needed Then
ActiveInspector.CommandBars.FindControl(ID:=5564).Execute
Call insert_sig
bDiscardEvents = False
End Sub

Sub insert_sig()
On Error Resume Next

Dim strFolder As String
Dim Signature As String
Dim sig1 As String
Dim sig2 As String
sig1 = "sig1"
sig2 = "sig2"
strFolder = ActiveExplorer.CurrentFolder.FolderPath
strFolder = Mid(strFolder, 3, InStr(3, strFolder, "\") - 3)
If strFolder = sig1 Then
Signature = "sig1"
ElseIf strFolder = sig2 Then
Signature = "sig1"
Else
'no signature
Exit Sub
End If
ActiveInspector.CommandBars("Menu Bar"). _
Controls("Insert"). _
Controls("Signature"). _
Controls(Signature).Execute
End Sub

Sebastian H
01-23-2012, 10:57 PM
Why do you use a static ID here?
ActiveInspector.CommandBars.FindControl(ID:=5564).Execute

How about letting VBA find the control like this?
ActiveInspector.CommandBars.FindControl(ID:=CommandBars("YourCmdBar").Controls("YourCtrl").ID).Execute

dakotafig
01-24-2012, 11:15 AM
Ok, well I tried that line:





ActiveInspector.CommandBars.FindControl(ID:=CommandBars("Options").Controls("HTML").ID).Execute


and it says sub or function not defined and the ID:=CommandBars is highlighted. What that line does is actually toggle the html format button on the options tab on my message window. Again the reply and replyall works just fine, it errors on the forward for some reason.

Sebastian H
01-24-2012, 12:26 PM
Change 'CommandBars' to 'ActiveInspector.CommandBars'.

dakotafig
01-24-2012, 01:28 PM
That was it! Thanks a bunch!