Macro conversion

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Macro conversion

Post by dasadler »

Some time ago, I was provided a Word macro for pasting as unformatted text. The macro was:

Sub PasteUnformatted()
Selection.PasteSpecial DataType:=wdPasteText
End Sub

This has been very useful as I added it as a button on the Word 2007 Quick Access Toolbar. Is it possible to do the same for Outlook 2007? If so, how would I need to change the macro?
Don

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Macro conversion

Post by Rudi »

I found this on another forum...
It is untested

Code: Select all

Sub Paste_Special_Unformatted()
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection
    On Error Resume Next
    ' get a Word.Selection from the open Outlook item
    Set objDoc = Application.ActiveInspector.WordEditor
    Set objSel = objDoc.Windows(1).Selection
    ' now do what you want with the Selection
    objSel.PasteSpecial Link:=False, DataType:=wdPasteText
    Set objDoc = Nothing
    Set objSel = Nothing
End Sub
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: Macro conversion

Post by dasadler »

Rudi, is that for Word or Outlook? The one I already have works great on Word and I was hoping for similar on Outlook.

Edited... nevermind. It sure looks like Outlook though I wonder if it requires that Words is used as the editor?

I'll find out.
Don

User avatar
HansV
Administrator
Posts: 78530
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Macro conversion

Post by HansV »

In Word 2007 and later, Word is always the e-mail editor.
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Macro conversion

Post by Rudi »

It is for Outlook.
Paste the code into the ThisOutlookSession module

BTW: In order to make it work you need a reference to the Word Object Library.
In VBA choose: Tools – References, and enable the reference for Microsoft Word 12.0 Object Library (Office 2007)

I tested it and it works well.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: Macro conversion

Post by dasadler »

Thanks, I will give it a try. I wonder, though, why the macro is so much more complex than the Word macro that does the same thing with a single line. I don't know if it is relevant but the text I will paste into the email I compose will be from any number of sources... not necessarily from another email.
Don

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Macro conversion

Post by Rudi »

dasadler wrote:Thanks, I will give it a try. I wonder, though, why the macro is so much more complex than the Word macro that does the same thing with a single line. I don't know if it is relevant but the text I will paste into the email I compose will be from any number of sources... not necessarily from another email.
I'm no expert here, but Outlook VBA is apparently more "complex" than Word; maybe due to the form design of an Outlook message, the fact that Outlook uses an internal form of Word as its editor and have to deal with HTML and other formats of script. The code is a few more lines as it is declaring objects, then setting those objects containers. The real line of code doing the action is: objSel.PasteSpecial Link:=False, DataType:=wdPasteText. The effort in the macro is to get a reference to the WordEditor with provides the most flexible means of working within the body of the email.

Quotes taken from here:

Basic item body techniques
Outlook provides three ways to work with the item body (the actual message part of the email), through three different properties:
-- Through the plain text representation provided by the Body property.
-- Through the tagged HTML representation provided for HTML-format messages and posts through the HTMLBody property.
-- Through the Inspector.WordEditor property, which returns a Word.Document object, even if Word is not installed, for each item except “sticky notes” and distribution lists.

17.5 Using WordEditor
The Outlook object model itself provides no direct way to determine the position of the cursor in an item body. However, since the editor for every item body (except on “sticky notes” and distribution lists) is a special version of Microsoft Word, you can use Word techniques not only to add text at the insertion point, but also to add formatted text anywhere in the item, or even to add a picture. To use these techniques in Outlook VBA code, use the Tools | References command to add a reference to the Microsoft Word 12.0 Object Library.
...As an initial example of how to invoke the Word editor programmatically, using the Inspector.WordEditor method...
This macro will paste whatever is on the clipboard into the message body as plain text.
Though we have a reference to the WordEditor and activated the MS Word Object library, the code is not really specific ONLY for Word. As the quote above mentions...even if you do not have Word installed, this code will work.

Application.ActiveInspector.WordEditor is an object to allow VBA to work within the Body Section of the email
objDoc.Windows(1).Selection is a reference to the cursor position in the message body
objSel.PasteSpecial is the instruction to paste whatever is on the clipboard at the position of the cursor

Bottom line, this code will paste anything from the clipboard, no matter what the source was, into the body message as plain text.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: Macro conversion

Post by dasadler »

I tried it and get a compile error. I did ensure the correct reference is enabled.
You do not have the required permissions to view the files attached to this post.
Don

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Macro conversion

Post by Rudi »

Are you sure you have a reference to the Word Object Library?
BTW: I have Office 2013, so mine says 15.0 Object Library.
SC007.jpg
You do not have the required permissions to view the files attached to this post.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: Macro conversion

Post by dasadler »

I think so
You do not have the required permissions to view the files attached to this post.
Don

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: Macro conversion

Post by dasadler »

Oops... I was wrong. I corrected the reference and the macro works fine now. Thank you very much.
Don