Change quotes into curly quotes with VBA?

N

Nomey

Dear all,

Is it possible to use VBA to change all quotes into curly quotes (not smart quotes). Now I have Chr0145, Chr0146, Chr0147 and Chr0148 in my document. I've tried to do this with Search & Replace unsuccessfully, in various fonts.

Best regards,
Shirley Nomey
 
H

Helmut Weber

Hi Shirley,

often autocorrect issues seem to prevent a replacement.
In fact, they don't, it's only that the replacement
is corrected immediately afterwards.
You may prevent autocorrection by
setting the found range to a new character,
like that:

Sub Test6777()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "[^0145-^0148]"
.MatchWildcards = True
While .Execute
rDcm.Select ' for testing using F8
' rdcm.Text = what character?
rDcm.Collapse Direction:=wdCollapseEnd ' maybe not required
Wend
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
G

Graham Mayor

Smart quotes *are* curly quotes? And they are those indicated by the
characters you have. If you want to change them back to straight quotes then
something like .....

Sub ReplaceSmartQuotes()
Dim vFindText As Variant
Dim vReplText As Variant
Dim i As Long

vFindText = Array("[^0145^0146]", "[^0147^0148]")
vReplText = Array("^039", "^034")
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.MatchWildcards = True

For i = LBound(vFindText) To UBound(vFindText)
.Text = vFindText(i)
.Replacement.Text = vReplText(i)
.Execute Replace:=wdReplaceAll
Next i
End With
End Sub

should do the trick

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
N

Nomey

Thanks, Helmut. I'll try that. I'll have to study Graham's solution too. Maybe it's a font problem rather than a character problem, since the right characters are already in the text, according to Graham. Someone edited my text and changed the font to Courier New. When I change it back to Times New Roman, my curly quotes seem to have disappeared.

Cheers,
S.
 
S

Shauna Kelly

Hi Shirley

If that's all the problem is, then use Format > AutoFormat. If you need
to do it in VBA, use something like this:

Sub MakeQuotesCurly()

Dim oDoc As Word.Document
Dim bOldAutoFormatQuotes As Boolean

Set oDoc = ActiveDocument

'Store the user's old settings
bOldAutoFormatQuotes =
Word.Application.Options.AutoFormatReplaceQuotes

'Set up Word to AutoFormat what we want
Word.Application.Options.AutoFormatReplaceQuotes = True

'Try Word's built-in autoformatting
On Error Resume Next
oDoc.Range.AutoFormat

'If Word's Autoformat didn't work, tell the user
If Err.Number > 0 Then
MsgBox "Error: Could not change straight quotes to curly quotes"
End If

'Restore the user's old settings
Word.Application.Options.AutoFormatReplaceQuotes =
bOldAutoFormatQuotes


End Sub

Note that AutoFormat won't work in footnotes.

If it helps, my standard test sentence for such things is:

Fred Smith, who was staying at his sister's home in 's Gravenhage, said
"I'll meet you at 8 o'clock 'n' we'll have dinner at "George's". I'll
ask the maître d' to recommend a wine."


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word



Nomey said:
Thanks, Helmut. I'll try that. I'll have to study Graham's solution
too. Maybe it's a font problem rather than a character problem, since
the right characters are already in the text, according to Graham.
Someone edited my text and changed the font to Courier New. When I
change it back to Times New Roman, my curly quotes seem to have
disappeared.

Cheers,
S.

Helmut said:
Hi Shirley,

often autocorrect issues seem to prevent a replacement.
In fact, they don't, it's only that the replacement
is corrected immediately afterwards.
You may prevent autocorrection by
setting the found range to a new character,
like that:

Sub Test6777()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "[^0145-^0148]"
.MatchWildcards = True
While .Execute
rDcm.Select ' for testing using F8
' rdcm.Text = what character?
rDcm.Collapse Direction:=wdCollapseEnd ' maybe not required
Wend
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
G

Graham Mayor

You can check the ANSI code of an inserted character with the following
macro

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

The smart quote characters are present in both Times New Roman and Courier
New (though obviously have a different appearance from one another). One
ploy may be to run the macro I posted earlier to change them all to straight
quotes then run autoformat with the straight quotes to smart quotes option
set.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Thanks, Helmut. I'll try that. I'll have to study Graham's solution
too. Maybe it's a font problem rather than a character problem, since
the right characters are already in the text, according to Graham.
Someone edited my text and changed the font to Courier New. When I
change it back to Times New Roman, my curly quotes seem to have
disappeared.
Cheers,
S.

Helmut said:
Hi Shirley,

often autocorrect issues seem to prevent a replacement.
In fact, they don't, it's only that the replacement
is corrected immediately afterwards.
You may prevent autocorrection by
setting the found range to a new character,
like that:

Sub Test6777()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "[^0145-^0148]"
.MatchWildcards = True
While .Execute
rDcm.Select ' for testing using F8
' rdcm.Text = what character?
rDcm.Collapse Direction:=wdCollapseEnd ' maybe not required
Wend
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

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