Macros to delete em dashs

D

Designingsally

i m trying to deleted em dashes ( Ctrl plus -) when it is prefixed with the
word "pre" in a document.

For example, Pre-amble. The macros ll correct it as Preamble.
Sub UMPre()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng
With .FInd
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute("Pre(" & ChrW(8212) & ")[a-z]{1,}>",
MatchWildcards:=True)
oRng.Select 'Word processing
Select Case Msgbox(Chr(34) & "Pre-" & Chr(34) & " on page " &
Selection.Information(wdActiveEndPageNumber) & " should not be" & Chr(34) &
"hyphenated." & Chr(34), vbYesNoCancel, "Colon")
Case vbCancel
Exit Sub
Case vbYes
oRng = Replace(oRng.Text, "(" & ChrW(8212) & ")", "")
If oRng.Words.Last.Characters.First.Case <> wdUpperCase Then
oRng.Font.Bold = False
oRng.Words.First.Bold = True
oRng.Words.Last.Characters.First.Case = wdUpperCase
End If
End Select
oRng.Collapse wdCollapseEnd
Loop
End With
End With
End Sub
I wrote the macro code. But it is not identifying em dashes at all what
could be the reason for this. Can someone explain?
Pls take a look the code given below.
Thanks for the help in advance.
 
P

Pesach Shelnitz

Hi Sally,

Your macro finds em dashes for me. I think that your problem may be that you
are confusing en dashes with em dashes. Your code looks for a string
containing ChrW(8212), which is an em dash, but in your explanation you
mentioned the shortcut key Ctrl+-. Pressing Ctrl together with - on the
number pad produces an en dash. The decimal code for an en dash is 8211, not
8212.

BTW, when you use a Range object instead of the Selection object, there is
no need to call ClearFormatting.

Also, you can replace Chr(34) & "Pre-" & Chr(34) & by """Pre-""" & . In my
opinion, the latter is more readable.
 
D

Designingsally

HI Pesach

yes -the macros are able to recognize en dash but it is not replacing it
with a space.
for example
Pre(en dash)amble
Preamble

I have placed the code below. Thanks again for looking through.

Sub UMPre()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng
With .FInd
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute("Pre(" & ChrW(8211) & ")[a-z]{1,}>",
MatchWildcards:=True)
oRng.Select 'Word processing
Select Case Msgbox(Chr(34) & "Pre-" & Chr(34) & " on page " &
Selection.Information(wdActiveEndPageNumber) & " should not be" & Chr(34) &
"hyphenated." & Chr(34), vbYesNoCancel, "Colon")
Case vbCancel
Exit Sub
Case vbYes
oRng = Replace(oRng.Text, "(ChrW(8211))", "")
If oRng.Words.Last.Characters.First.Case <> wdUpperCase Then
oRng.Font.Bold = False
oRng.Words.First.Bold = True
oRng.Words.Last.Characters.First.Case = wdUpperCase
End If
End Select
oRng.Collapse wdCollapseEnd
Loop
End With
End With
End Sub



--
I believe in Hope.

DesigningSally


Pesach Shelnitz said:
Hi Sally,

Your macro finds em dashes for me. I think that your problem may be that you
are confusing en dashes with em dashes. Your code looks for a string
containing ChrW(8212), which is an em dash, but in your explanation you
mentioned the shortcut key Ctrl+-. Pressing Ctrl together with - on the
number pad produces an en dash. The decimal code for an en dash is 8211, not
8212.

BTW, when you use a Range object instead of the Selection object, there is
no need to call ClearFormatting.

Also, you can replace Chr(34) & "Pre-" & Chr(34) & by """Pre-""" & . In my
opinion, the latter is more readable.

--
Hope this helps,
Pesach Shelnitz


Designingsally said:
i m trying to deleted em dashes ( Ctrl plus -) when it is prefixed with the
word "pre" in a document.

For example, Pre-amble. The macros ll correct it as Preamble.
Sub UMPre()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng
With .FInd
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute("Pre(" & ChrW(8212) & ")[a-z]{1,}>",
MatchWildcards:=True)
oRng.Select 'Word processing
Select Case Msgbox(Chr(34) & "Pre-" & Chr(34) & " on page " &
Selection.Information(wdActiveEndPageNumber) & " should not be" & Chr(34) &
"hyphenated." & Chr(34), vbYesNoCancel, "Colon")
Case vbCancel
Exit Sub
Case vbYes
oRng = Replace(oRng.Text, "(" & ChrW(8212) & ")", "")
If oRng.Words.Last.Characters.First.Case <> wdUpperCase Then
oRng.Font.Bold = False
oRng.Words.First.Bold = True
oRng.Words.Last.Characters.First.Case = wdUpperCase
End If
End Select
oRng.Collapse wdCollapseEnd
Loop
End With
End With
End Sub
I wrote the macro code. But it is not identifying em dashes at all what
could be the reason for this. Can someone explain?
Pls take a look the code given below.
Thanks for the help in advance.
 
P

Pesach Shelnitz

Hi Sally,

Change the line

oRng = Replace(oRng.Text, "(ChrW(8211))", "")
to

oRng = Replace(oRng.Text, ChrW(8211), "")

--
Hope this helps,
Pesach Shelnitz


Designingsally said:
HI Pesach

yes -the macros are able to recognize en dash but it is not replacing it
with a space.
for example
Pre(en dash)amble
Preamble

I have placed the code below. Thanks again for looking through.

Sub UMPre()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng
With .FInd
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute("Pre(" & ChrW(8211) & ")[a-z]{1,}>",
MatchWildcards:=True)
oRng.Select 'Word processing
Select Case Msgbox(Chr(34) & "Pre-" & Chr(34) & " on page " &
Selection.Information(wdActiveEndPageNumber) & " should not be" & Chr(34) &
"hyphenated." & Chr(34), vbYesNoCancel, "Colon")
Case vbCancel
Exit Sub
Case vbYes
oRng = Replace(oRng.Text, "(ChrW(8211))", "")
If oRng.Words.Last.Characters.First.Case <> wdUpperCase Then
oRng.Font.Bold = False
oRng.Words.First.Bold = True
oRng.Words.Last.Characters.First.Case = wdUpperCase
End If
End Select
oRng.Collapse wdCollapseEnd
Loop
End With
End With
End Sub



--
I believe in Hope.

DesigningSally


Pesach Shelnitz said:
Hi Sally,

Your macro finds em dashes for me. I think that your problem may be that you
are confusing en dashes with em dashes. Your code looks for a string
containing ChrW(8212), which is an em dash, but in your explanation you
mentioned the shortcut key Ctrl+-. Pressing Ctrl together with - on the
number pad produces an en dash. The decimal code for an en dash is 8211, not
8212.

BTW, when you use a Range object instead of the Selection object, there is
no need to call ClearFormatting.

Also, you can replace Chr(34) & "Pre-" & Chr(34) & by """Pre-""" & . In my
opinion, the latter is more readable.

--
Hope this helps,
Pesach Shelnitz


Designingsally said:
i m trying to deleted em dashes ( Ctrl plus -) when it is prefixed with the
word "pre" in a document.

For example, Pre-amble. The macros ll correct it as Preamble.
Sub UMPre()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng
With .FInd
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute("Pre(" & ChrW(8212) & ")[a-z]{1,}>",
MatchWildcards:=True)
oRng.Select 'Word processing
Select Case Msgbox(Chr(34) & "Pre-" & Chr(34) & " on page " &
Selection.Information(wdActiveEndPageNumber) & " should not be" & Chr(34) &
"hyphenated." & Chr(34), vbYesNoCancel, "Colon")
Case vbCancel
Exit Sub
Case vbYes
oRng = Replace(oRng.Text, "(" & ChrW(8212) & ")", "")
If oRng.Words.Last.Characters.First.Case <> wdUpperCase Then
oRng.Font.Bold = False
oRng.Words.First.Bold = True
oRng.Words.Last.Characters.First.Case = wdUpperCase
End If
End Select
oRng.Collapse wdCollapseEnd
Loop
End With
End With
End Sub
I wrote the macro code. But it is not identifying em dashes at all what
could be the reason for this. Can someone explain?
Pls take a look the code given below.
Thanks for the help in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top