Simple Word macro help

I write most posts in Word, so I’d like to make it a bit easier to italicize, add URLs, etc. But when Word pastes in from the clipboard, it often adds a space. Here’s what I have for adding an opening URL tag:


 Sub Msgb_URL()
    Selection.TypeText Text:="[url="
    Selection.PasteAndFormat (wdPasteDefault)
    Selection.TypeText Text:="]"
End Sub

That puts a space between the ‘=’ and the beginning of the URL. Same thing often happens with text that I’m putting in italics or bold.

What shall we use to fill the empty spaces?

I’m more an Excel VBA guy, but here’s what I came up with. Clunky as hell, but it’ll get you by until someone comes along with something more elegant.

First, you’ll need to add a reference to the MSForms 2.0 library, which you can do easily enough by going to Insert > UserForm. (This will add a form, but you can ignore it.) Then, use this:

Sub Msgb_URL()

Dim DObj As MSForms.DataObject
Dim URL As String
Set DObj = New MSForms.DataObject
DObj.GetFromClipboard
URL = Replace(DObj.GetText, " ", "")

Selection.TypeText Text:="[url="
Selection.TypeText (URL)
Selection.TypeText Text:="]"

End Sub

Oh how I loathe the Ribbon.

What group is “insert form” in?

While I look that up, any chance this bit of code could be used to add a keyboard-based macro to Outlook to simplify pasting unformatted text?

Oh, sorry, I meant the Insert menu in the VBA window. If you’re looking at your Word doc, go to the Developer tab and choose Visual Basic, then Insert > User Form from there.

Not sure whether Outlook gives access to user forms, but if so then yes. I’ll check a bit later. :slight_smile:

Ah, thanks. I even looked in the Developer Ribbon but didn’t explore different menus.

ETA: Still hate the Ribbon.

Yet another reason to hate the Ribbon: you can’t add ‘&’ in front of a menu command’s name to create a keyboard shortcut for it anymore. The upshot of that is that the old workaround for keybinding macros in Outlook no longer works in 2010, and I can’t find another way to do it.

That said, you can still create a Paste Unformatted macro and add it to the Quick Access bar to make it a single click. If you’re interested, here’s the code. (You’ll still need to add a UserForm for this to work.)

Sub PasteUnformatted()

Dim DObj As MSForms.DataObject
Dim ToPaste As String
Set DObj = New MSForms.DataObject
DObj.GetFromClipboard
ToPaste = DObj.GetText

Set objDoc = Application.ActiveInspector.WordEditor
Set objSel = objDoc.Windows(1).Selection
objSel.TypeText (ToPaste)

End Sub

Once you’ve saved the macro, to add it to Quick Access, open a new mail message, right-click anywhere in the ribbon, and select “Customize Quick Access Toolbar”. Then, under the Category dropdown, choose Macros.

Highlight the macro you want (should be called something like Project1.PasteUnformatted), click on Add, and press OK. Now all of your new mail messages will get this button at the top, which will paste the clipboard text unformatted.

If anyone who passes by happens to know a new workaround for adding keyboard shortcuts in Outlook 2010, I’d be interested to learn it.

You can’t bind keys in Outlook?! How bizarre.

I’m getting a compile error: user-defined type not defined (in the DObj As MSForms.DataObject line).

I went to the VBA window>insert>UserForm. It added UserForm1 to the Project pane, but the error still comes up. I’m trying to Google it, but the commonness of both the error and the line is flooding results with other issues.

Hrrm. In the VBA window, go to Tools > References, and see if the Microsoft Forms 2.0 Object Library is checked. If it isn’t, check it and press OK. If it is, I’ve got nothing, at least off the top of my head.

ETA: Did you get the Word macro working ok, or is the same thing happening there?

Nevermind.

Wasn’t there a second ago. Then I went to a Insert>userform and instead of just closing the box I added an element then closed the box. Went back to the reference library and it was checked. Macro runs great!
THANKS!

Everything is fantastic. I’m now going to bang my head on the desk for a while because of the lack of key bindings. It just seems … wrong. Did MS ever put out a rationale for it?

Not that I’ve ever seen. Every other Office application can use Application.OnKey, but this has never worked in Outlook. God knows why.

BUT! I found out you can access items on the Quick Access bar by using Alt + [number], where [number] is the position of the item on your bar. For me, the Paste macro is the sixth thing in the bar, so Alt + 6 does it. Feels weird to press that particular combo, but I imagine I’ll get used to it.

It sounds to me like what you really want is to avoid getting those empty spaces.

I’ve never used the newer versions of Word (i.e. with the ribbon) but here’s how I’d fix it in the Old Word:
– Tools
– Options
– uncheck Smart Cut And Paste

Dammit, Keeve, go away and let the programmers overcomplicate things! :wink: (In all seriousness, I’m VERY guilty of “when-all-you-have-is-a-hammer” thinking when it comes to VBA, especially in Excel. It’s just such a big, shiny hammer…)

If Smart C&P is what’s doing this to you, here’s how to kill it in Office 2010: File > Options > Under “Cut, copy, and paste,” uncheck “Use smart cut and paste”.

Why?

Primarily, it’s conditioning from the days when the Board hamsters would have random seizures. Also, a mashed keystroke can still lose a post Try writing a post, hitting back and forward to see what I’m talking about. An accidentally mashed ‘tab’ key followed by a backspace can do it. FF has gotten better at retaining text, but it still happens every once in a great while.

Writing in Word is also a bit easier (e.g. moving paragraph blocks around), and since it’s my primary workspace it’s always right there so the effort is minimal.

Add the function to the quick access toolbar, then hit Alt and whatever number shows up on the toolbar for it.

I do not use this lightly, but …

Oh. My. God.
No, really.

Wow.

Just … wow.
I cannot tell you how unfucking believably awesome this thread turned out to be.

I just remapped my paste-unformatted mouse button to Alt-7 when it uses Outlook.

Paste Unformatted foreverrrrrrr!!!

THANKYOUTHANKYOUTHANKYOU!

(Side note to anyone interested. I don’t know if they changed it in the past few years, but Logitech’s mouse control panel was somewhat limited. A program called Uberoptions adds a ton of functionality and lets you customize your mouse in an absurd number of ways–both in general and for specific programs.)

You’re welcome! :slight_smile:

I misread this the first time, and thought we were talking about keybinding macros in Word. That prompted me to remember this LifeHacker post I read yesterday, which tells how to do that: Create a Hotkey to Paste Plain Text in Microsoft Word (You only need steps 3 and 4 from that.)

That may be less useful, especially since you already figured out the Alt+ combination, but I thought it might still be helpful/interesting.