Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-07-2023, 11:00 AM
Shelley Lou Shelley Lou is offline VBA help non breaking spaces for array of words not working correctly Windows 10 VBA help non breaking spaces for array of words not working correctly Office 2016
Competent Performer
VBA help non breaking spaces for array of words not working correctly
 
Join Date: Dec 2020
Posts: 171
Shelley Lou is on a distinguished road
Default VBA help non breaking spaces for array of words not working correctly

I would be really grateful for some pointers on where I'm going wrong with the below code. I have an array of words I want to change the space to a non breaking space between the word and digit(s) and their plural e.g looks for clause and clauses etc.



At the moment it is putting the non breaking space before the word e.g. clause. I've tried \1^s but that puts it at the end of the digit. I've tried \1^s\2 and other variations but they all just give me an error - I'm just not sure where I'm going wrong for something that should be quite straightforward.

Code:
ArrFndB = Array("[Aa]ppendix", "[Cc]lause", "[Pp]aragraph", "[Pp]art", "[Ss]chedule")
Code:
For i = 0 To UBound(ArrFndB)
   .text = "(" & ArrFndB(i) & "[s ]@[0-9.]{1,})"
   .Replacement.text = "^s\1"
   .Execute Replace:=wdReplaceAll
   Next
Reply With Quote
  #2  
Old 03-07-2023, 03:56 PM
Guessed's Avatar
Guessed Guessed is offline VBA help non breaking spaces for array of words not working correctly Windows 10 VBA help non breaking spaces for array of words not working correctly Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,991
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

The plural of appendix is not just adding an s to the end. It is normally appendixes or appendices. The problem you are having is that you are enclosing the find text in a single set of (brackets) when you need two separate sets if you want to replace something inside there.

This would all be a lot more straightforward if you simply expand the array to include all acceptable plurals.
Code:
  Dim arr() As String, i As Integer, sFind As String
  sFind = "[Aa]ppendix [Aa]ppendixes [Aa]ppendices [Cc]lause [Cc]lauses [Pp]aragraph [Pp]aragraphs [Pp]art [Pp]arts [Ss]chedule [Ss]chedules"
  arr = Split(sFind, " ")
  With ActiveDocument.Range.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .MatchWildcards = True
    For i = 0 To UBound(arr)
      .Text = "(" & arr(i) & ") ([0-9.]{1,})"
      .Replacement.Text = "\1^s\2"
      .Execute Replace:=wdReplaceAll
    Next
  End With
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 03-08-2023, 03:15 AM
Shelley Lou Shelley Lou is offline VBA help non breaking spaces for array of words not working correctly Windows 10 VBA help non breaking spaces for array of words not working correctly Office 2016
Competent Performer
VBA help non breaking spaces for array of words not working correctly
 
Join Date: Dec 2020
Posts: 171
Shelley Lou is on a distinguished road
Default VBA help non breaking spaces for array of words not working correctly

Hi, thank you so much, I can see where I was going wrong now, I've managed to replicate the code but in the opposite way i.e. find number then array for a different set of words so many thanks for pointing out where I was going wrong, much appreciated.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Globally Remove Non-breaking Spaces PSSMargaret PowerPoint 3 11-15-2021 01:06 PM
Adding non-breaking spaces to cross references seanspotatobusiness Word 3 07-22-2018 07:48 AM
VBA help non breaking spaces for array of words not working correctly Spaces between words, help! cheffie Word 2 10-02-2013 01:59 PM
An array of words from a document knowing the font style Kreol2013 Word VBA 0 07-08-2013 01:29 AM
VBA help non breaking spaces for array of words not working correctly Non-breaking spaces in random emails Tri Outlook 3 02-04-2013 06:35 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:45 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