Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-03-2024, 02:30 AM
Shelley Lou Shelley Lou is offline VBA include Character Code 002D Dash-Minus Windows 10 VBA include Character Code 002D Dash-Minus Office 2016
Expert
VBA include Character Code 002D Dash-Minus
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA include Character Code 002D Dash-Minus

Hi, in my Word documents the dashes are either character code 2013 or 002D.



The code below only looks for code 2013. I would like to add an additional line of code to include code 002D but not sure how to include this - can anybody help/advise at all, thanks.


Code:
 Case InStr(oRngEval, " - ") = 1 'highlights if cross refs separated by a dash and space either side e.g. clause 1 - 2
              oRng.Move wdCharacter, 3
              lngOffset = 3
Reply With Quote
  #2  
Old 07-03-2024, 04:00 AM
vivka vivka is offline VBA include Character Code 002D Dash-Minus Windows 7 64bit VBA include Character Code 002D Dash-Minus Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default

Hi! Your dash 2013 is actually en-dash ChrW(8211) & dash 002D is hyphen-minus ChrW(45).
Without seeing your 'Select Case ...' I have doubts if the following will help, but try using
Code:
Case InStr(oRngEval, " " & ChrW(8211) & " ") = 1, InStr(oRngEval, " " & ChrW(45) & " ") = 1

Or if there are no other Cases, I'd use:
Code:
Dim sStr As String

Select Case InStr(oRng, " " & sStr & " ") = 1
Case sStr = ChrW(45), sStr = ChrW(8211)
Reply With Quote
  #3  
Old 07-03-2024, 04:38 AM
Shelley Lou Shelley Lou is offline VBA include Character Code 002D Dash-Minus Windows 10 VBA include Character Code 002D Dash-Minus Office 2016
Expert
VBA include Character Code 002D Dash-Minus
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA include Character Code 002D Dash-Minus

Hi Vivka, thank you for the explanation of the character codes which is really helpful. Unfortunately that line of code doesn't work within my full code but it has given me something to work on so thank you (full code below).

Code:
Sub CompoundCRs(oRngPassed As Range)
Dim strTerms As String
Dim arrTerms() As String
Dim lngIndex As Long, lngOffset As Long
Dim bCompound As Boolean
Dim oRng As Range, oRngEval As Range

  strTerms = "[Aa]rticle,[Aa]ppendix,[Cc]lause,[Pp]aragraph,[Pp]art,[Ss]chedule" 'highlight numbers after these words
  arrTerms = Split(strTerms, ",")
  On Error GoTo Err_Handler
  Set oRng = oRngPassed.Duplicate
  
  With oRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindStop
    .MatchWildcards = True
    .text = "[^s]"
    .Replacement.text = " "
    .Execute Replace:=wdReplaceAll
  End With
  For lngIndex = 0 To UBound(arrTerms)
    Set oRng = oRngPassed.Duplicate
    With oRng.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Forward = True
      .Wrap = wdFindStop
      .MatchWildcards = True
      .text = arrTerms(lngIndex) & "[s ^s]@[0-9.]{1,}" 'looks for singular and plural of string words
      Do While .Execute
        oRng.MoveStart wdWord, 1
        oRng.HighlightColorIndex = wdBrightGreen
        oRng.Collapse wdCollapseEnd
        bCompound = True
        Do While bCompound
          Set oRngEval = oRngPassed.Duplicate
          oRngEval.Start = oRng.End
          Select Case True
            Case InStr(oRngEval, "-") = 1 'highlights if cross refs seperated by a dash e.g. clause 1-2
              oRng.Move wdCharacter, 1
              lngOffset = 0
            Case InStr(oRngEval, ", ") = 1 'highlights if cross refs seperated by a comma e.g. clause 1, 2
               oRng.Move wdCharacter, 2
               lngOffset = 0
            Case InStr(oRngEval, " - ") = 1 'highlights if cross refs seperated by a dash and space either side e.g. clause 1 - 2 THIS CURRENTLY DOESN'T WORK
              oRng.Move wdCharacter, 3
              lngOffset = 3
            Case InStr(oRngEval, " or ") = 1 'highlights if cross refs seperated by the word 'or' e.g. clause 1 or 2
              oRng.Move wdCharacter, 4
              lngOffset = 4
            Case InStr(oRngEval, " to ") = 1 'highlights if cross refs seperated by the word 'to' e.g. clause 1 to 2
              oRng.Move wdCharacter, 4
              lngOffset = 4
            Case InStr(oRngEval, " and ") = 1 'highlights if cross refs seperated by the word 'and' e.g. clause 1 and 2
              oRng.Move wdCharacter, 5
              lngOffset = 5
            Case InStr(oRngEval, " and/or ") = 1 'highlights if cross refs seperated by the word 'and/or' e.g. clause 1 and/or 2
              oRng.Move wdCharacter, 8
              lngOffset = 8
            Case Else
              bCompound = False
          End Select
          If bCompound Then
            oRng.MoveEnd wdCharacter, 1
            Do While IsNumeric(oRng.Characters.Last.Next) Or (oRng.Characters.Last.Next = "." And IsNumeric(oRng.Characters.Last.Next.Next))
              oRng.MoveEnd wdCharacter, 1
            Loop
            If IsNumeric(oRng.Characters.First.text) Then oRng.HighlightColorIndex = wdBrightGreen
            oRng.Collapse wdCollapseEnd
            If lngOffset > 0 Then
              oRng.Move wdCharacter, -lngOffset
            End If
          End If
        Loop
      Loop
     End With
Next_For:
  Next
lbl_Exit:
  Exit Sub
Err_Handler:
  MsgBox Err.Number & " - " & Err.Description
  Resume Next_For
End Sub
Reply With Quote
  #4  
Old 07-03-2024, 05:40 AM
vivka vivka is offline VBA include Character Code 002D Dash-Minus Windows 7 64bit VBA include Character Code 002D Dash-Minus Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default

Shelley Lou, I have two ideas.
Idea 1: try replacing the appropriate part of your code with this:
Code:
Dim sStr As String
          
          Select Case InStr(oRngEval, sStr) = 1
            Case sStr = "-" 'highlights if cross refs seperated by a dash e.g. clause 1-2
              oRng.Move wdCharacter, 1
              lngOffset = 0
            Case sStr = ", " 'highlights if cross refs seperated by a comma e.g. clause 1, 2
               oRng.Move wdCharacter, 2
               lngOffset = 0
            Case sStr = " - " 'highlights if cross refs seperated by a dash and space either side e.g. clause 1 - 2 THIS CURRENTLY DOESN'T WORK
              oRng.Move wdCharacter, 3
              lngOffset = 3
            Case sStr = " or " 'highlights if cross refs seperated by the word 'or' e.g. clause 1 or 2
              oRng.Move wdCharacter, 4
              lngOffset = 4
            Case sStr = " to " 'highlights if cross refs seperated by the word 'to' e.g. clause 1 to 2
              oRng.Move wdCharacter, 4
              lngOffset = 4
            Case sStr = " and " 'highlights if cross refs seperated by the word 'and' e.g. clause 1 and 2
              oRng.Move wdCharacter, 5
              lngOffset = 5
            Case sStr = " and/or " 'highlights if cross refs seperated by the word 'and/or' e.g. clause 1 and/or 2
              oRng.Move wdCharacter, 8
              lngOffset = 8
            Case Else
              bCompound = False
          End Select
Idea 2: Probably, the dash in the text and the dash the code is looking for are actually different dashes (they have different codes), though they look identical.
To find out the dashes' codes, select them and then use AscW(selection).
Reply With Quote
  #5  
Old 07-03-2024, 06:46 AM
Shelley Lou Shelley Lou is offline VBA include Character Code 002D Dash-Minus Windows 10 VBA include Character Code 002D Dash-Minus Office 2016
Expert
VBA include Character Code 002D Dash-Minus
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA include Character Code 002D Dash-Minus

Hi Vivka, yes you are correct, the code as written will only highlight instances of ChrW(8211) but my word documents can have both ChrW(8211) and ChrW(45) within them as they are often converted from pdf to word - maybe it would be easier to do a find ChrW(45) and replace with ChrW(8211) first before running the code maybe. Thanks for responding, much appreciated.
Reply With Quote
  #6  
Old 07-03-2024, 06:57 AM
vivka vivka is offline VBA include Character Code 002D Dash-Minus Windows 7 64bit VBA include Character Code 002D Dash-Minus Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default

I've tested the codes below and they seem to work correctly:
Code:
Case InStr(oRngEval, " " & ChrW(45) & " ") = 1, InStr(oRngEval, " " & ChrW(8211) & " ") = 1
Code:
Case InStr(oRngEval, " " & ChrW(45) & " ") = 1 Or InStr(oRngEval, " " & ChrW(8211) & " ") = 1
Try putting either of them instead of the 'misbehaving' line.
Reply With Quote
  #7  
Old 07-03-2024, 07:55 AM
Shelley Lou Shelley Lou is offline VBA include Character Code 002D Dash-Minus Windows 10 VBA include Character Code 002D Dash-Minus Office 2016
Expert
VBA include Character Code 002D Dash-Minus
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA include Character Code 002D Dash-Minus

Hi Vivka, fantastic, the second line of code works perfectly - thank you so much
Reply With Quote
  #8  
Old 07-03-2024, 08:18 AM
vivka vivka is offline VBA include Character Code 002D Dash-Minus Windows 7 64bit VBA include Character Code 002D Dash-Minus Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default

Yes, we did it! It calls for drinking
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA include Character Code 002D Dash-Minus List of ascii code of Character Symbols or code for all symbols used in Word SamDsouza Word VBA 3 03-12-2020 04:49 AM
VBA include Character Code 002D Dash-Minus How can you use the " character for inches in a field code? steveflooks Word 1 07-19-2018 02:52 PM
VBA include Character Code 002D Dash-Minus Help to include header and footer in string search code which i already have. branko Word VBA 1 03-28-2017 11:50 PM
Is it possible to include a newline character in the replace text desgordon Word 2 09-14-2012 02:28 AM
VBA include Character Code 002D Dash-Minus Need Word to change -- (dash,dash) into one long dash. Bobosmite Word 2 05-06-2011 04:21 AM

Other Forums: Access Forums

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