The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

MS Word macro help please.

DrezDrez Registered User regular
edited May 2007 in Help / Advice Forum
I posted this in G&T awhile back and while we got close to a solution, it never clicked.

I'm in Word 2007. I want a simple macro that will let me take any selected text and wrap it in <i> </i> tags. In other words, it will take SELECTED TEXT and replace it with <i>SELECTED TEXT</i>.

Any ideas?

Switch: SW-7690-2320-9238Steam/PSN/Xbox: Drezdar
Drez on

Posts

  • PirateJonPirateJon Registered User regular
    edited May 2007
    Should be simple. What's the varible in Word 2007 for selected text?

    PirateJon on
    all perfectionists are mediocre in their own eyes
  • DrezDrez Registered User regular
    edited May 2007
    I don't know...I'm clueless when it comes to Macros or VB.

    Drez on
    Switch: SW-7690-2320-9238Steam/PSN/Xbox: Drezdar
  • NatsusNatsus Registered User regular
    edited May 2007
    Here's a simple macro. This is for Word 2003 but I imagine it's the same for 2007

    Goto tools -> macros -> visual basic editor

    paste this code in:

    Sub macro1()
    Selection = " <i>" & Trim(Selection) & "</i> "
    End Sub


    Then just select text and run macro. (if you want to rename macro, rename macro1)

    Natsus on
  • CZroeCZroe Registered User regular
    edited May 2007
    Umm, the keystrok for activating the macro couldn't be easier than simply pressing CTRL + I could it? Unless this is part of a macro that will also identify and make the selection. :)

    CZroe on
  • embrikembrik Registered User regular
    edited May 2007
    CZroe wrote: »
    Umm, the keystrok for activating the macro couldn't be easier than simply pressing CTRL + I could it? Unless this is part of a macro that will also identify and make the selection. :)

    The OP is looking for help to add HTML tags to his document, not just use Word formatting to change it to italics (unless I misread).

    Regardless, Natsus' macro works fine in Word 2007. I tested it, once I found the damn macros menu!

    embrik on
    "Damn you and your Daily Doubles, you brigand!"

    I don't believe it - I'm on my THIRD PS3, and my FIRST XBOX360. What the heck?
  • NatsusNatsus Registered User regular
    edited May 2007
    embrik wrote: »
    CZroe wrote: »
    Umm, the keystrok for activating the macro couldn't be easier than simply pressing CTRL + I could it? Unless this is part of a macro that will also identify and make the selection. :)

    The OP is looking for help to add HTML tags to his document, not just use Word formatting to change it to italics (unless I misread).

    Regardless, Natsus' macro works fine in Word 2007. I tested it, once I found the damn macros menu!

    Yeah I haven't installed Office 2007, so I wasn't sure if the menus were the same or not. Hope this is what you were looking for Drez.

    Natsus on
  • DrezDrez Registered User regular
    edited May 2007
    LOL, is that really all there is to it? I'm going to run to the deli and try adding that.

    Drez on
    Switch: SW-7690-2320-9238Steam/PSN/Xbox: Drezdar
  • DrezDrez Registered User regular
    edited May 2007
    Thanks, that works well! Only problem is that it seems to put a carriage return before the closing </i> tag. Any idea why/how to fix it?

    Drez on
    Switch: SW-7690-2320-9238Steam/PSN/Xbox: Drezdar
  • Mr_GrinchMr_Grinch Registered User regular
    edited May 2007
    Try:

    Sub sexyprogram()
    If Right(Selection, 1) = " " Or Right(Selection, 1) = Chr(13) Then
    Selection = " <i>" & Left$((Selection), (Len(Selection) - 1)) & "</i> "
    Else
    Selection = " <i>" & Selection & "</i> "
    End If
    End Sub

    There's probably a better way but when you reach the end of the line it seems to place a carriage return marker when you select the text, this will check if the selection has a carriage return in the last position, if it does it'll remove it.

    Only tested quickly :)

    Mr_Grinch on
    Steam: Sir_Grinch
    PSN: SirGrinchX
    Oculus Rift: Sir_Grinch
  • iTunesIsEviliTunesIsEvil Cornfield? Cornfield.Registered User regular
    edited May 2007
    I can't get it to add the carriage return you're seeing... Youre sure it's not just in a point in your doc where it feels the need to wrap around to the next line? Stupid question, I know.

    Also, for the love of God, please give your macro a better entry point name than "macro1." The programmers of the world will love you just that much more. :)

    iTunesIsEvil on
  • Mr_GrinchMr_Grinch Registered User regular
    edited May 2007
    It appears to do it in the version of Word we're running (2003 I think) at work. To see if it does it on your version, write out some text, then go to the last character and hit delete, make sure nothing follows it. Now try.

    On this version of word it picks up a carriage return at the end of the selection. Stupid, but true!

    Mr_Grinch on
    Steam: Sir_Grinch
    PSN: SirGrinchX
    Oculus Rift: Sir_Grinch
  • PheezerPheezer Registered User, ClubPA regular
    edited May 2007
    If the selection is at the end of the text, ie, the last three words in the doc, you'll get this.

    Pheezer on
    IT'S GOT ME REACHING IN MY POCKET IT'S GOT ME FORKING OVER CASH
    CUZ THERE'S SOMETHING IN THE MIDDLE AND IT'S GIVING ME A RASH
  • Mr_GrinchMr_Grinch Registered User regular
    edited May 2007
    Any idea why? Adding in the little if statement above seemed to fix it (the chr(13) one, the " " could probably be taken out, I used it when I was testing stuff but it should keep things tidy) but I have no idea why it does it.

    Mr_Grinch on
    Steam: Sir_Grinch
    PSN: SirGrinchX
    Oculus Rift: Sir_Grinch
  • PheezerPheezer Registered User, ClubPA regular
    edited May 2007
    Mr_Grinch wrote: »
    Any idea why? Adding in the little if statement above seemed to fix it (the chr(13) one, the " " could probably be taken out, I used it when I was testing stuff but it should keep things tidy) but I have no idea why it does it.

    Nah, it's just dumb that way. Probably a bizarre compatibility thing, to give it an honest guess.

    Pheezer on
    IT'S GOT ME REACHING IN MY POCKET IT'S GOT ME FORKING OVER CASH
    CUZ THERE'S SOMETHING IN THE MIDDLE AND IT'S GIVING ME A RASH
  • NatsusNatsus Registered User regular
    edited May 2007
    Also, for the love of God, please give your macro a better entry point name than "macro1." The programmers of the world will love you just that much more. :)

    I actually left it macro1 for the user to rename it to whatever he wants, but you're right I probably should of named it, "yourprogramname" or something like that 8-)

    Anyway Mr_Grinch's code is good. It gets rid of the carriage return problem. I didn't realize it was happening in a different versions of Word =P

    Now what would be nice is if you could select multiple text and then run the macro to add the tags at the end of all of them, but I'm not sure if that's even possible.

    Natsus on
Sign In or Register to comment.