0

I need a Word Macro to convert selection text from this: This is a =selection in a word =document. To this: This is a s________ in a word d_______. Can anyone help me out?

I've tried

Sub conv_1()
    Dim Sentence    As String
    Dim i           As Integer
    Sentence = Selection.Text
    For i = 1 To Len(Sentence)
        If Mid(Sentence, i, 1) Like "[a-zA-Z]" Then
            Mid(Sentence, i, 1) = "_"
        End If
    Next i
    Selection.Text = Sentence
End Sub

but it converts all the selected words to Dash-Dash-Dash.

I've also tried

Sub conv_2()
    
    'Declare variables
    Dim intCount    As Integer
    Dim intLength   As Integer
    Dim strInput    As String
    Dim strOutput   As String
    Dim strTemp     As String
    
    'Get the selected text
    strInput = Selection
    
    'If nothing is selected, exit
    If strInput = "" Then
        Exit Sub
    End If
    
    'Split the string into an array
    Dim arrWords
    arrWords = Split(strInput)
    
    'Loop through each word in the array
    For intCount = LBound(arrWords) To UBound(arrWords)
        
        'Get the length of the word
        intLength = Len(arrWords(intCount))
        
        'If the word is longer than 1 character, convert the word
        If intLength > 1 Then
            strTemp = arrWords(intCount)
            strOutput = strOutput & Left(strTemp, 1) & String(intLength - 2, "_") & _
                        Right(strTemp, 1) & " "
        Else
            'If the word is 1 character, add it to the output string
            strOutput = strOutput & arrWords(intCount) & " "
        End If
        
    Next
    
    'Trim the trailing space
    strOutput = Trim(strOutput)
    
    'Replace the original selection with the converted text
    Selection.TypeText Text:=strOutput
    
End Sub

but it converts all words into A-Dash-A.

I don't know how to make it so that only the words that have an equal sign next to them get turned into A-Dash-Dash. I would appreciate any help on this!

Tim Li
  • 7
  • 3
  • On your second code you could try to implement a evaluation of the first character of a word if it is a _=_. e.g. `if Left(strTemp, 1) = "=" then` – Shrotter Nov 12 '22 at 09:15

1 Answers1

0

If all you want is two dashes, you don't even need VBA - it can all be done with a wildcard Find/Replace, where:

Find = =<[A-Za-z]@>
Replace= ––

If you want the text to be replaced with the same number of dashes as there are characters in the word, you could use a macro like:

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, StrTxt As String
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "=<[A-Za-z]@>"
    .Replacement.Text = ""
    .Forward = True
    .Format = False
    .Wrap = wdFindStop
    .MatchWildcards = True
  End With
  Do While .Find.Execute
    StrTxt = ""
    For i = 1 To Len(.Text) - 1
      StrTxt = StrTxt & "–"
    Next
    .Text = StrTxt
    .Collapse wdCollapseEnd
  Loop
End With
Application.ScreenUpdating = True
End Sub
macropod
  • 12,757
  • 2
  • 9
  • 21