Spaces after Em dash (Longer dash)

S

Sahana

I m trying to make the macros to indicate there should be no space
before and after an em dash. But i m unable to get the execution
right. Below is the code. let me know whats wrong.

Sub Spacesemdash()
Dim oRng As Range
Selection.HomeKey Unit:=wdStory
With Selection.FInd
.ClearFormatting
.Wrap = wdFindStop
''' .Text = "^="
.Text = "ChrW(8212)"
Do While .Execute
Set oRng = Selection.Range
With oRng
.MoveEnd Unit:=wdCharacter, Count:=1
.MoveStart Unit:=wdCharacter, Count:=-1
With .Characters
If .Last = " " Or .First = " " Then
ActiveDocument.comments.Add _
Range:=oRng, _
Text:="There should be no spaces before or
after an em dash."
End If
End With
End With
Loop
End With
End Sub
 
G

Graham Mayor

Remove the quotes from around ChrW(8212) or Word will search for the literal
text.
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
P

Pesach Shelnitz

Hi,

I can get your macro to work properly by making the following changes.
1) Remove the quotation marks around ChrW(8212), or change "ChrW(8212)" to
"^+".
2) Add the following line before the end of the loop.

Selection.Collapse Direction:=wdCollapseEnd

Also, you should add the following line at the end of your macro to promote
garbage collection.

Set oRng = Nothing
 
S

Sahana

Remove the quotes from around ChrW(8212) or Word will search for the literal
text.
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>





- Show quoted text -

Hi I tried this... but does not work :-(

Sub Spacesemdash()
Dim oRng As Range
Selection.HomeKey Unit:=wdStory
With Selection.FInd
.ClearFormatting
.Wrap = wdFindStop
.Text = ChrW(8212)
Do While .Execute
Set oRng = Selection.Range
With oRng
.MoveEnd Unit:=wdCharacter, Count:=1
.MoveStart Unit:=wdCharacter, Count:=-1
With .Characters
If .Last = " " Or .First = " " Then
ActiveDocument.comments.Add _
Range:=oRng, _
Text:="There should be no spaces before or
after an em dash."
End If
End With
End With
Loop
End With
End Sub
 
S

Sahana

Hi I tried this... but does not work :-(

Sub Spacesemdash()
Dim oRng As Range
Selection.HomeKey Unit:=wdStory
With Selection.FInd
    .ClearFormatting
    .Wrap = wdFindStop
    .Text = ChrW(8212)
    Do While .Execute
        Set oRng = Selection.Range
        With oRng
            .MoveEnd Unit:=wdCharacter, Count:=1
            .MoveStart Unit:=wdCharacter, Count:=-1
            With .Characters
                If .Last = " " Or .First = " " Then
                    ActiveDocument.comments.Add _
                        Range:=oRng, _
                        Text:="There should be no spaces before or
after an em dash."
                End If
            End With
        End With
    Loop
End With
End Sub- Hide quoted text -

- Show quoted text -

I also added Selection.Collapse Direction:=wdCollapseEnd before loop
as suggested by Pesach. Didnt work yet:-(
 
G

Graham Mayor

I also added Selection.Collapse Direction:=wdCollapseEnd before loop
as suggested by Pesach. Didnt work yet:-(

It works as suggested without the need to collapse. Try using
..Text = Chr(151)
instead. Otherwise are you sure that the searched string actually exists in
your document?
Are you sure that the Em dashes in your document are not En dashes
(Chr(150)?
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Sahana

It works as suggested without the need to collapse. Try using
.Text = Chr(151)
instead. Otherwise are you sure that the searched string actually exists in
your document?
Are you sure that the Em dashes in your document are not En dashes
(Chr(150)?
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Hi

It still didnt work I m sure I m going wrong somewhere.I thought i ll
post the code with example. So that you can excatly point to me where
I m going wrong.
My example:

MS Word- is great.
MS Word– is great.


My code
Sub Spacesemdash()
Dim oRng As Range
Selection.HomeKey Unit:=wdStory
With Selection.FInd
.ClearFormatting
.Wrap = wdFindStop
.Text = Chr(151)
Do While .Execute
Set oRng = Selection.Range
With oRng
.MoveEnd Unit:=wdCharacter, Count:=1
.MoveStart Unit:=wdCharacter, Count:=-1
With .Characters
If .Last = " " Or .First = " " Then
ActiveDocument.comments.Add _
Range:=oRng, _
Text:="There should be no spaces before or
after an em dash."
End If
End With
End With

End With
End Sub

Can you post your code? SO that I can compare. The macro should check
the second half of the example. Or maybe I m giving wrong example
also....
 
G

Graham Mayor

The dash in your posted example is an En dash Chr(150)
Use the following macro to identify the character at the cursor

Sub AnsiValue()
S1$ = "Because the selected text contains"
S2$ = " characters, not all of the ANSI values will be displayed."
S3$ = "ANSI Value ("
S4$ = " characters in selection)"
S5$ = " character in selection)"
S6$ = "Text must be selected before this macro is run."
S7$ = "ANSI Value"
Dim strSel, strNums, LastFourChar As String
Dim iPos As Integer
strSel = Selection.Text
If Len(strSel) > 0 Then
For i = 1 To Len(strSel)
strNums = strNums + Str(Asc(Mid(strSel, i)))
Next i
strNums = LTrim(strNums)
If Len(strNums) > 255 Then
LastFourChar = Mid(strNums, 252, 4)
strNums = Left(strNums, 251) + Left(LastFourChar, _
4 - InStr(" ", LastFourChar))
MsgBox S1$ + Str(Len(strSel)) + S2$
End If
If Len(strSel) = 1 Then S4$ = S5$
MsgBox strNums, 0, S3$ + LTrim(Str(Len(strSel))) + S4$
Else
MsgBox S6$, 0, S7$
End If
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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

Similar Threads

Check Double Spaces 1
Highlight that word in a sentence 2
Error in simple macro 3
Macros to delete em dashs 3
Facing Error with List 1
List 0
HOW TO KEEP A RANGE INSIDE QUOTES? 2
Macro duplicates last comment 2

Top