PDA

View Full Version : Trouble with Outlook 2013 VBA code



Brent Laftin
02-16-2015, 02:51 PM
Hi,

I have some VBA code that works well with Outlook 2010 but after upgrading to Outlook 2013 I cannot make it work. The below code does the following when it is run in Outlook 2010: 1) Reply to active email, 2) Remove Character # 74, 3) Clear Clipboard, 4) Select all, 5) Copy all, 6) Close email

Here's my code, any help converting to Outlook 2013 would be awesome!


Sub CopyAll_and_openSite()

Dim myinspector As Outlook.Inspector
Dim myItem As Outlook.MailItem
Dim Insp As Inspector
Dim obj As Object
Dim objApp
Dim objInsp
Dim colCB
Dim objCBB
On Error Resume Next

Set objApp = GetObject("", "Outlook.Application")
If objApp Is Nothing Then
Set objApp = Application.CreateObject("Outlook.Application")
End If
Set objInsp = objApp.ActiveInspector
If TypeName(objInsp) = "Nothing" Then
MsgBox "No inspector window found"
Exit Sub
Else
Set colCB = objInsp.CommandBars

Set objCBB = colCB.FindControl(, 354) ' Reply
objCBB.Execute

End If

Set obj = Nothing
Set Insp = Nothing

Set Insp = Application.ActiveInspector
Set obj = Insp.CurrentItem

obj.body = Replace(obj.body, Chr$(74), "") 'remove character 74

Set obj = Nothing
Set Insp = Nothing

Set objApp = GetObject("", "Outlook.Application")
If objApp Is Nothing Then
Set objApp = Application.CreateObject("Outlook.Application")
End If
Set objInsp = objApp.ActiveInspector
If TypeName(objInsp) = "Nothing" Then
MsgBox "No inspector window found"
Exit Sub
Else

Set colCB = objInsp.CommandBars

Set objCBB = colCB.FindControl(, 3634) ' clear clipboard
objCBB.Execute

Set objCBB = colCB.FindControl(, 756) ' select all
objCBB.Execute

Set objCBB = colCB.FindControl(, 19) ' copy
objCBB.Execute

Set myinspector = Application.ActiveInspector 'new
Set myItem = myinspector.CurrentItem 'new
myItem.Close olDiscard 'new

Set objCBB = colCB.FindControl(, 2011) ' Close email
objCBB.Execute

End If

Set myinspector = Application.ActiveInspector 'new
Set myItem = myinspector.CurrentItem 'new
myItem.Close olDiscard 'new

Set objCBB = Nothing
Set colCB = Nothing
Set objInsp = Nothing


End Sub

Thanks in advance,

Brent

gmayor
02-16-2015, 11:57 PM
The problem is undoubtedly the use of CommandBars code.

It is difficult to see the point of your process or why you need to remove chr(74) - Upper case J from the message, as the process doesn't appear to work here in 2010 either.

If we can assume that you are running the code from Outlook VBA and not from another Office product then the following should create a reply to the currently selected message and copy the content to the clipboard. I can see that it doesn't go quite far enough, but you will need to explain the process in more detail to get a handle on it.



Option Explicit
Sub CopyAll_and_openSite()

Dim olMailItem As Outlook.MailItem
Dim olReply As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object

Set olMailItem = ActiveExplorer.Selection.Item(1)
Set olReply = olMailItem.Reply 'create a reply to the current mail item

With olReply
.BodyFormat = olFormatHTML
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range
oRng.Copy
.Display 'This line is required even if later you send the message
With oRng.Find
Do While .Execute(FindText:=Chr$(74), MatchCase:=True)
oRng.Text = "" 'Remove 'J' from the reply body?
Loop
End With
'.Send 'Send the reply
End With
olMailItem.Close olDiscard 'close the opriginal mail message

lbl_Exit:
Set olMailItem = Nothing
Set olReply = Nothing
Set olInsp = Nothing
Set wdDoc = Nothing
Set oRng = Nothing
Exit Sub
End Sub

Brent Laftin
02-17-2015, 04:40 PM
The problem is undoubtedly the use of CommandBars code.

It is difficult to see the point of your process or why you need to remove chr(74) - Upper case J from the message, as the process doesn't appear to work here in 2010 either.

If we can assume that you are running the code from Outlook VBA and not from another Office product then the following should create a reply to the currently selected message and copy the content to the clipboard. I can see that it doesn't go quite far enough, but you will need to explain the process in more detail to get a handle on it.



Hi Graham,

Thank you so much for taking the time to look at this for me. I'm far from a VBA programmer, just good at stringing useful code(s) together to sometimes make a workable macro. Here's why I put this one together:

We have a system that requires copious notes that are either hand-typed in or copy/pasted emails. Our system can't handle any smiley faces or other various graphic icons, my work-around for this was to remove chr(74) and replace it with a lower case J. This seems to get rid of the pesky icons that cause an error in our system. I'm sure there's a better way to go about this but it was all I could come up with.

As for the rest of the macro, I simply need to assign it to a Quick Access Toolbar macro command that appears in the toolbar of any open email that when the toolbar button is pushed:

1) Hits Reply to the open email
2) Clears the clipboard
3) Select All and Copy
4) Close the email

Hopefully this helps clarify my goal. Thanks again for your gracious help.

Brent

gmayor
02-18-2015, 12:37 AM
To change uppercase J to lower case j change the line


oRng.Text = "" 'Remove 'J' from the reply body?
to

oRng.Text = "j"

but I don't see how this relates to graphics, and it will make unwarranted changes to the letter J. Without seeing a sample, it is difficult to address the real issue of unwanted graphics characters.

As for adding the command to the QAT (Quick Access Toolbar) I am pretty sure that this is the cause of the original issue. Why not just add the command to Outlook manually? It doesn't have to be added each time the macro is run?

The purpose of creating a reply then copying and then closing eludes me. What are you going to do with the copied content?

Are you just trying to edit a message that has pasted material before sending it, and the Reply part is just an awkward means to an end? That being the case there is no need for the extra message. You can edit the body of the message directly. Save a typical problem message as MSG then attach it to the forum (go advanced) so we can see what the problem is.

Brent Laftin
02-18-2015, 09:02 AM
Hi Graham,

Thanks again for your help. I was able to tweak your code just a bit to solve my problem. I really appreciate you looking at this for me.

Brent