Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-24-2024, 01:48 AM
Shelley Lou Shelley Lou is offline VBA Format text in brackets to Italics but only after cross references Windows 10 VBA Format text in brackets to Italics but only after cross references Office 2016
Expert
VBA Format text in brackets to Italics but only after cross references
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Format text in brackets to Italics but only after cross references

I have some code that will format text within brackets to italics. I would like to modify the code to look for text in brackets only if they appear after a cross reference field or a cross reference manual number after clause, paragraph, part or schedule and format with italics. What do I need to add to the code to achieve this? Any advice would be very much appreciated.



Before Image.JPG

After Image.JPG

Cross Ref in Brackets Test.docx

Code:
Sub DPU_BracketText_Italics()
Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.text = "\(*\)"
.Font.Bold = False
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Format = True
Do While .Execute
With oRng
.MoveEnd Unit:=wdCharacter, count:=-1
.MoveStart Unit:=wdCharacter, count:=1
.Font.Italic = True
.Font.Bold = False
.Collapse wdCollapseEnd
End With
Loop
End With
End Sub
Reply With Quote
  #2  
Old 12-24-2024, 03:26 AM
macropod's Avatar
macropod macropod is offline VBA Format text in brackets to Italics but only after cross references Windows 10 VBA Format text in brackets to Italics but only after cross references Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,384
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

For example:
Code:
Sub DPU_BracketText_Italics()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Text = " \(*\)"
    .Forward = True
    .Wrap = wdFindStop
    .MatchWildcards = True
  End With
  Do While .Find.Execute
    If .Characters.First.Previous.Fields.Count > 0 Then
      .Start = .Start + 2
      .End = .End - 1
      .Font.Italic = True
    End If
    .Collapse wdCollapseEnd
  Loop
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 12-24-2024, 04:16 AM
Shelley Lou Shelley Lou is offline VBA Format text in brackets to Italics but only after cross references Windows 10 VBA Format text in brackets to Italics but only after cross references Office 2016
Expert
VBA Format text in brackets to Italics but only after cross references
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Format text in brackets to Italics but only after cross references

Hi Macropod, thank you so much for this it is very much appreciated - only tested on one document so far but seems to be working as it should.
Reply With Quote
  #4  
Old 01-07-2025, 08:21 AM
Shelley Lou Shelley Lou is offline VBA Format text in brackets to Italics but only after cross references Windows 10 VBA Format text in brackets to Italics but only after cross references Office 2016
Expert
VBA Format text in brackets to Italics but only after cross references
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Format text in brackets to Italics but only after cross references

I marked this thread as solved but on further testing, the code isn't working as expected. I ran it on a document containing text within brackets after cross reference fields and general text in brackets. The code formats all text within brackets to italics and not just the text in brackets after a cross reference field. Any advice what I can add to the code to prevent the code from formatting all text within brackets to italics?

Capture.JPG
Reply With Quote
  #5  
Old 01-07-2025, 01:13 PM
macropod's Avatar
macropod macropod is offline VBA Format text in brackets to Italics but only after cross references Windows 10 VBA Format text in brackets to Italics but only after cross references Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,384
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

Hi Shelley,

The only way the macro will italicize the text between a pair of brackets is if they're preceded by a field. I can only conclude that the problem text was either:
• preceded by a field when the macro was run and the field was deleted/moved afterwards; or
• italicized by some other process.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Format text in brackets to Italics but only after cross references Cross-references between text boxes - no link janiceward Word 3 05-06-2016 02:51 PM
VBA Format text in brackets to Italics but only after cross references Formatting text after cross references spunkygirl1983 Word 1 03-18-2016 03:42 PM
Convert manual cross references in footnotes to other footnotes to automatic cross references ghumdinger Word VBA 7 11-20-2014 11:47 PM
VBA Format text in brackets to Italics but only after cross references Globally changing formatting of in-text cross references estube Word 1 07-08-2014 03:21 AM
VBA Format text in brackets to Italics but only after cross references Changing text in cross-references Richard B Word 3 09-11-2013 07:18 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:36 PM.


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