Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-24-2016, 01:21 PM
brent chadwick brent chadwick is offline Reverse Bold macro Windows 8 Reverse Bold macro Office 2013
Advanced Beginner
Reverse Bold macro
 
Join Date: Mar 2015
Posts: 86
brent chadwick is on a distinguished road
Default Reverse Bold macro

Hey guys, I have a lot text that needs to have bold reversed. I found this macro somewhere, it works but is real slow. Any suggestions to rewrite and make faster, or is this the best it gets? Thanks
Code:
Sub ReverseBold()
    Dim c
    For Each c In Selection.Characters
        c.Font.Bold = Not c.Font.Bold
    Next c
End Sub

Reply With Quote
  #2  
Old 02-24-2016, 02:01 PM
macropod's Avatar
macropod macropod is offline Reverse Bold macro Windows 7 64bit Reverse Bold macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Sub ReverseBold()
Application.ScreenUpdating = False
Dim Rng As Range, bBld As Boolean
Set Rng = Selection.Characters.First
With Rng
  bBld = .Font.Bold
  Do While .End < Selection.End
    Do While .Characters.Last.Next.Font.Bold = bBld
      If .End < Selection.End Then
        .End = .End + 1
      Else
        Exit Do
      End If
    Loop
    .Font.Bold = Not bBld
    bBld = Not bBld
    .Collapse wdCollapseEnd
  Loop
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 02-25-2016, 05:08 AM
brent chadwick brent chadwick is offline Reverse Bold macro Windows 8 Reverse Bold macro Office 2013
Advanced Beginner
Reverse Bold macro
 
Join Date: Mar 2015
Posts: 86
brent chadwick is on a distinguished road
Default

Thanks Paul. Again, your code works but it seems to take a long time. For a four line sentence it takes 20+ seconds to change the bold. Doesn't that seem like a long time??
Reply With Quote
  #4  
Old 02-25-2016, 08:26 AM
gmaxey gmaxey is offline Reverse Bold macro Windows 7 32bit Reverse Bold macro Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Kluncky but fast:

Code:
Sub ReverseBoldII()
Application.ScreenUpdating = False
Dim oRng As Range
  Set oRng = Selection.Range
  With oRng.Find
    .Text = "(*)"
    .Font.Bold = False
    .MatchWildcards = True
    With .Replacement
      .Text = "^&"
      .Font.Bold = True
      .Font.DoubleStrikeThrough = True 'any unused font attribute
    End With
    .Execute Replace:=wdReplaceAll
   End With
   Set oRng = Selection.Range
   With oRng.Find
    .Text = "(*)"
    .Font.Bold = True
    .Font.DoubleStrikeThrough = False
    .MatchWildcards = True
    With .Replacement
      .Text = "^&"
      .Font.Bold = False
    End With
    .Execute Replace:=wdReplaceAll
   End With
   Set oRng = Selection.Range
   With oRng.Find
    .Text = "(*)"
    .Font.Bold = True
    .Font.DoubleStrikeThrough = True
    .MatchWildcards = True
    With .Replacement
      .Text = "^&"
      .Font.DoubleStrikeThrough = False
    End With
    .Execute Replace:=wdReplaceAll
   End With
Application.ScreenUpdating = True
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 02-25-2016, 03:00 PM
macropod's Avatar
macropod macropod is offline Reverse Bold macro Windows 7 64bit Reverse Bold macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by brent chadwick View Post
Again, your code works but it seems to take a long time. For a four line sentence it takes 20+ seconds to change the bold. Doesn't that seem like a long time??
On my system, processing a selection with 4440 words spanning 288 lines over 7 pages, of which 456 separate words were bold, took just 2 seconds, as did reversing the process. That seems fast enough for me.

While Greg's code will probably do what you want, my approach avoids any potential interference with whatever other formatting your content might already have (including double-strikethrough).
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 02-25-2016, 04:24 PM
brent chadwick brent chadwick is offline Reverse Bold macro Windows 8 Reverse Bold macro Office 2013
Advanced Beginner
Reverse Bold macro
 
Join Date: Mar 2015
Posts: 86
brent chadwick is on a distinguished road
Default

Would it make a difference if there are a lot fields in these docs? I timed using it a couple times with either of the macro I found and the one that Paul wrote, and for a four sentence paragraph it actually took 20-25 seconds. Suggestions?
Reply With Quote
  #7  
Old 02-25-2016, 05:54 PM
gmaxey gmaxey is offline Reverse Bold macro Windows 7 32bit Reverse Bold macro Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

You have already had one suggesting which apparently you ignored. Paul's code seems fast to me. The only possible issue with it is that it pukes if your selection happens to include the final paragraph mark.

You can try:

Code:
Sub ReverseBoldII()
Dim oRng As Range
Application.ScreenUpdating = False
Set oRng = Selection.Characters.First
  Do
    oRng.Font.Bold = Not oRng.Font.Bold
    Set oRng = oRng.Characters.Last.Next
  Loop Until oRng.Start = Selection.Characters.Last.Start
  Application.ScreenUpdating = True
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 02-25-2016, 06:28 PM
macropod's Avatar
macropod macropod is offline Reverse Bold macro Windows 7 64bit Reverse Bold macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by gmaxey View Post
Paul's code seems fast to me. The only possible issue with it is that it pukes if your selection happens to include the final paragraph mark.
Thanks for the heads-up. That could be remedied by replacing:
Set Rng = Selection.Characters.First
with:
Code:
With Selection
  Set Rng = .Characters.First
  If .End = ActiveDocument.Range.End Then
    .Characters.Last.Font.Bold = Not .Characters.Last.Font.Bold
    .End = .End - 1
  End If
End With
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 02-25-2016, 07:10 PM
brent chadwick brent chadwick is offline Reverse Bold macro Windows 8 Reverse Bold macro Office 2013
Advanced Beginner
Reverse Bold macro
 
Join Date: Mar 2015
Posts: 86
brent chadwick is on a distinguished road
Default

Thanks Paul for the change in the code, works well. Greg, yours worked well also but it really messed up my bookmarks. Thanks to all for the help-
Reply With Quote
  #10  
Old 02-26-2016, 04:49 AM
gmaxey gmaxey is offline Reverse Bold macro Windows 7 32bit Reverse Bold macro Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Odd that the find and replace version affects bookmarks as it does. I don't recall encountering that before. Paul any idea as to why?

The loop version doesn't seem to affect bookmarks, but if you have a solution it is mute.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #11  
Old 02-26-2016, 05:04 AM
macropod's Avatar
macropod macropod is offline Reverse Bold macro Windows 7 64bit Reverse Bold macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

ISTR encountering difficulties at times when F/R code crosses or terminates at a field brace. The gotcha is that toggling the field code display changes what might be found...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 02-26-2016, 09:25 AM
gmaxey gmaxey is offline Reverse Bold macro Windows 7 32bit Reverse Bold macro Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Paul,

Not this case. In a very simple:
This is a bold test
with "a bold" bookmarked, the code returns:
This is a bold test
and the bookmark shifts left places to:
"s a bol"

Weird
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #13  
Old 02-26-2016, 10:37 AM
brent chadwick brent chadwick is offline Reverse Bold macro Windows 8 Reverse Bold macro Office 2013
Advanced Beginner
Reverse Bold macro
 
Join Date: Mar 2015
Posts: 86
brent chadwick is on a distinguished road
Default

That's what happened to me. I agree-weird.
Reply With Quote
  #14  
Old 02-26-2016, 02:28 PM
macropod's Avatar
macropod macropod is offline Reverse Bold macro Windows 7 64bit Reverse Bold macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Ah, OK, I mis-read as a reference to fields. I can see how what you're describing might happen, though I've not encountered it before. Just another little 'gotcha' to be aware of.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #15  
Old 02-26-2016, 05:21 PM
gmaxey gmaxey is offline Reverse Bold macro Windows 7 32bit Reverse Bold macro Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Paul,

I don't' see a reason for it happening and it seems a first rate bug to me. The code is "finding" text and replacing that text with itself with a different font attribute. There is nothing it the code that I see that should cause bookmarks to shift to the left.

As I said, the code is clunky to begin with. Now it is even more clunky restoring the bookmarks :

Code:
Sub ReverseBoldIII()
Application.ScreenUpdating = False
Dim oRI As Range, oRng As Range
Dim oCol As New Collection
Dim oBM As Bookmark
Dim lngIndex As Long
Dim arrRng() As String
  For Each oBM In Selection.Range.Bookmarks
    oCol.Add oBM.Start & "|" & oBM.End
  Next
  Set oRng = Selection.Range.Duplicate
  Set oRI = Selection.Range.Duplicate
  With oRng.Find
    .Text = "*"
    .Font.Bold = False
    .MatchWildcards = True
    With .Replacement
      .Text = "^&"
      .Font.Bold = True
      .Font.DoubleStrikeThrough = True 'any unused font attribute
    End With
    .Execute Replace:=wdReplaceAll
   End With
   Set oRng = Selection.Range.Duplicate
   With oRng.Find
    .Text = "*"
    .Font.Bold = True
    .Font.DoubleStrikeThrough = False
    .MatchWildcards = True
    With .Replacement
      .Text = "^&"
      .Font.Bold = False
    End With
    .Execute Replace:=wdReplaceAll
   End With
   Set oRng = Selection.Range.Duplicate
   oRng.Select
   With oRng.Find
    .Text = "*"
    .Font.Bold = True
    .Font.DoubleStrikeThrough = True
    .MatchWildcards = True
    With .Replacement
      .Text = "^&"
      .Font.DoubleStrikeThrough = False
    End With
    .Execute Replace:=wdReplaceAll
   End With
   Set oRng = Selection.Range
   For lngIndex = 1 To oCol.Count
     Set oBM = oRI.Bookmarks(lngIndex)
     arrRng = Split(oCol.Item(lngIndex), "|")
     oBM.Start = arrRng(LBound(arrRng))
     oBM.End = arrRng(UBound(arrRng))
    Next
Application.ScreenUpdating = True
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reverse Bold macro Macro to find and replace headings in bold and underline redzan Word VBA 4 02-13-2016 12:24 PM
Creating macro in 2007 to bold within quotes on the fly marymaryt Word VBA 5 06-01-2015 06:55 AM
Macro Needed to bold specific lines and Macro to turn into CSV anewteacher Word VBA 1 05-28-2014 03:59 PM
Macro to reverse a test string Jennifer Murphy Word VBA 5 01-16-2014 03:19 PM
Format Bold in one line makes all lines bold Nitte Word 2 02-07-2013 12:34 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:57 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft