Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-14-2025, 05:54 AM
Shelley Lou Shelley Lou is offline VBA format text before colon issue with square bracket Windows 10 VBA format text before colon issue with square bracket Office 2016
Expert
VBA format text before colon issue with square bracket
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA format text before colon issue with square bracket

Hi, I've been working with the below code which G Maxy helped me with some time ago. The code works well except if there is an opening square bracket at the beginning of the text when the code is looking for a tab or a colon. I've tried adding ([\[] at the beginning but nothing seems to be working. Can anyone advise where I'm going wrong. Thanks

Before code has run
Before Image.JPG

After code has run


After Image.JPG

Code:
With oRng.Find
    .ClearFormatting
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchWildcards = True
    .Replacement.ClearFormatting
    .text = "([a-zA-Z0-9][!^13]@)([^t:])"
    'Find text before first tab or colon and format bold and remove colon
    While .Execute
       If oRng.Characters(1).Start = oRng.Paragraphs(1).Range.Characters(1).Start Then
         oRng.Font.Bold = True
         If Not oRng.Characters.Last.Next = vbTab Then
           oRng.text = Replace(oRng.text, ":", vbTab)
         Else
           oRng.Characters.Last.Delete
         End If
       End If
    Wend
  End With
Reply With Quote
  #2  
Old 05-14-2025, 07:55 AM
vivka vivka is offline VBA format text before colon issue with square bracket Windows 7 64bit VBA format text before colon issue with square bracket Office 2016
Expert
 
Join Date: Jul 2023
Posts: 302
vivka is on a distinguished road
Default

Hi, Shelley Lou! Although I didn't check it (because I don't have a text sample, only its picture), try this line:
Code:
If oRng.Characters(1).Start = oRng.Paragraphs(1).Range.Characters(1).Start Or oRng.Characters.First.Previous = "[" Then
This will be helpful if the string to find is not inside the paragraph.
Besides, I would insert

Code:
On Error Resume Next
at the beginning of the code to prevent error 91 popping up in case the range to find begins the active doc, because in that case the chr before the rng's 1st chr does not exist.
Reply With Quote
  #3  
Old 05-14-2025, 08:04 AM
Shelley Lou Shelley Lou is offline VBA format text before colon issue with square bracket Windows 10 VBA format text before colon issue with square bracket Office 2016
Expert
VBA format text before colon issue with square bracket
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA format text before colon issue with square bracket

Thank you so much Vivka, that has worked perfectly
Reply With Quote
  #4  
Old 05-14-2025, 08:29 AM
vivka vivka is offline VBA format text before colon issue with square bracket Windows 7 64bit VBA format text before colon issue with square bracket Office 2016
Expert
 
Join Date: Jul 2023
Posts: 302
vivka is on a distinguished road
Default

You are most welcome! I hope, Greg Maxey wouldn't mind my interfering in his code.
Reply With Quote
  #5  
Old 05-14-2025, 08:49 AM
gmaxey gmaxey is offline VBA format text before colon issue with square bracket Windows 10 VBA format text before colon issue with square bracket Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

No, I don't mind.


If I understand the problem correctly, this should work as well without need for the other change or On Error Resume Next line:


.Text = "([a-zA-Z0-9/[][!^13]@)([^t:])"
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/

Last edited by gmaxey; 05-15-2025 at 05:49 AM.
Reply With Quote
  #6  
Old 05-15-2025, 02:18 AM
Shelley Lou Shelley Lou is offline VBA format text before colon issue with square bracket Windows 10 VBA format text before colon issue with square bracket Office 2016
Expert
VBA format text before colon issue with square bracket
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA format text before colon issue with square bracket

Thank you so much
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Format text before colon bold not including square bracket/bracket Shelley Lou Word VBA 3 09-26-2024 09:12 AM
VBA format text before colon issue with square bracket VBA quote mark to insert after square bracket Shelley Lou Word VBA 13 06-25-2021 12:41 AM
VBA format text before colon issue with square bracket Delete blank paragraph below bold, end square bracket Dave T Word VBA 2 04-28-2019 11:00 PM
VBA format text before colon issue with square bracket Remove repeated number after square bracket jeffreybrown Word VBA 8 12-04-2018 06:01 PM
Word 2010 - Remove square-bracket encased string in large document IntestinalWorm Word 1 06-20-2017 01:14 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:53 AM.


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