+ Reply to Thread
Results 1 to 9 of 9

Thread: Macro from Macro Recorder won't work

  1. #1
    Forum Guru
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Excel 2010
    Posts
    1,502

    Macro from Macro Recorder won't work

    I have been attempting to write a macro that will remove the hyperlinks in a document and convert the font to the same format as the rest of the document. I've accomplished the first part with this macro:
    Sub KillHyperlinksinThisDocument()
    Dim i As Long
    For i = ActiveDocument.Hyperlinks.Count To 1 Step -1
    ActiveDocument.Hyperlinks(i).Delete
    Next i
    End Sub
    For the second part I recorded my steps using the macro recorder. What I did was do a Replace. I selected ALL, opened the Replace dialogue, searched in the FONT section, for Blue Verdana underlined font, and replaced that with FONT Black TimesNewRoman, no underline. My steps worked perfectly, and the macro recorder recorded my steps, but what it recorded didn't do anything when I tried using the macro (code is below):
    Sub Hypersnew()
    '
    ' Hypersnew Macro
    ' Macro recorded 1/18/2011 by John.
    '
        Selection.WholeStory
        Selection.Fields.Unlink
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindAsk
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    End Sub
    The macro recorder doesn't appear to have captured all the Font detail that's the heart of the replacement. I'd appreciate any help I can get in getting this working.

    Thanks,
    John
    Last edited by jomili; 01-25-2011 at 08:37 AM.

  2. #2
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,648

    Re: Macro from Macro Recorder won't work

    Something like this
    Sub Remove_All_Hyperlinks()
    
    'Removes hyperlinks from the active worksheet
    'Does not delete the hyperlink text, only the link to the site
        Do Until ActiveSheet.Hyperlinks.Count = 0
            ActiveSheet.Hyperlinks(1).Delete
        Loop
    
    End Sub
    To go through the whole workbook

    Sub Remove_All_Hyperlinks()
    Dim ws as Worksheet
    'Removes hyperlinks from the workbook
    For Each ws in Thisworkbook.Worksheets
    'Does not delete the hyperlink text, only the link to the site
        Do Until ws.Hyperlinks.Count = 0
            ws.Hyperlinks(1).Delete
        Loop
    Next ws
    End Sub
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  3. #3
    Forum Guru
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Excel 2010
    Posts
    1,502

    Re: Macro from Macro Recorder won't work

    Roy,

    Thanks, but your code does the same function the first macro I posted does, namely delete the hyperlink. However, from one particular website, when I copy and paste to Word, the text is formatted as TimesNewRoman, but the Hyperlinks are formatted as Verdana. So, AFTER running the macro and removing the hyperlinks, the old hyperlink text remains as Blue and Highlighted. That's the problem my second macro in the post is supposed to fix, but doesn't. That's the one I need help with.

  4. #4
    Registered User
    Join Date
    12-18-2010
    Location
    UK
    MS-Off Ver
    2003, 2007, 2010
    Posts
    79

    Re: Macro from Macro Recorder won't work

    Hello jomille

    If you examine the styles in your document is there one that relates to the deleted links? If there is, then try setting up a macro to delete all instances of that style.
    David
    Access and Excel Developer | UK

  5. #5
    Forum Guru
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Excel 2010
    Posts
    1,502

    Re: Macro from Macro Recorder won't work

    I can see that there IS a style related to the defunct links, but I'm just starting to use Macros with Excel, so macros in Word are way over my head. I would have no idea on how to use a style in a macro. Can you help?

  6. #6
    Registered User
    Join Date
    12-18-2010
    Location
    UK
    MS-Off Ver
    2003, 2007, 2010
    Posts
    79

    Re: Macro from Macro Recorder won't work

    Perhaps you could start by manually working in Word to remove the style. Then, when you have got as far as understanding styles and applying and removing them you could record a macro to perform the removal.

    Search for ‘word styles and formatting’ on the web and you will find out plenty abut them.
    David
    Access and Excel Developer | UK

  7. #7
    Forum Guru
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Excel 2010
    Posts
    1,502

    Re: Macro from Macro Recorder won't work

    Does nobody out on the internet ever delete a specific style? I found tons of hits on deleting unused styles, but nothing for how to delete A style and return everything to Normal. So, I did it this way instead:

    Sub KillHyperlinksinThisDocument()
    Dim i As Long
    For i = ActiveDocument.Hyperlinks.Count To 1 Step -1
    ActiveDocument.Hyperlinks(i).Delete
    Next i
    ActiveDocument.Select
    With Selection.Font
    .Name = "Times New Roman"
    .Size = 12
    .Color = wdColorBlack
    .Underline = False
    End With
    The only thing I still want to add I don't know how to even phrase the question, so I've attached an example. There are two little yellow boxy parchemnt scroll things that I believe are Google ads. How would I, with VBA, select and delete those? What are they called?
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    12-18-2010
    Location
    UK
    MS-Off Ver
    2003, 2007, 2010
    Posts
    79

    Re: Macro from Macro Recorder won't work

    Hello jomili

    They are what Word refers to as 'in-line shapes'. Please test this macro as a way of deleting them:

    Sub DeleteInlineShapes()
    ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Dim rngChar As Range
    
    For Each rngChar In ActiveDocument.Characters
       rngChar.Select
       If Selection.Type = wdSelectionInlineShape Then Selection.Delete
    Next rngChar
    
    End Sub
    David
    Access and Excel Developer | UK

  9. #9
    Forum Guru
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Excel 2010
    Posts
    1,502

    Re: Macro from Macro Recorder won't work

    Oh, so that's what "in-line shapes" are. I've seen them referred to but had no idea what they were. Your macro worked beautifully. I've added it to what I previously had, so the full macro code now is this:
    Sub KillHyperlinksinThisDocument()
    Dim i As Long
    Dim rngChar As Range
    
    'Part1 - Kill the hyperlinks
    For i = ActiveDocument.Hyperlinks.Count To 1 Step -1
    ActiveDocument.Hyperlinks(i).Delete
    Next i
    
    'Part2 - set all the fonts to my standard
    ActiveDocument.Select
    With Selection.Font
    .Name = "Times New Roman"
    .Size = 12
    .Color = wdColorBlack
    .Underline = False
    End With
    
    'Part3 - get rid of those pesky in-line shapes
    For Each rngChar In ActiveDocument.Characters
       rngChar.Select
       If Selection.Type = wdSelectionInlineShape Then Selection.Delete
    Next rngChar
    
    End Sub
    Thanks for all your help on this. This macro is going to be a blessing for me, so thanks for helping me create it.

    Thanks,
    John

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0