Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2008

Tip: Looking for answers? Try searching our database.

Change quotes into curly quotes with VBA?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nomey - 12 Jan 2008 14:09 GMT
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
Helmut Weber - 12 Jan 2008 14:30 GMT
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
Nomey - 13 Jan 2008 11:33 GMT
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.

> Hi Shirley,
>
[quoted text clipped - 26 lines]
>
> Vista Small Business, Office XP
Shauna Kelly - 13 Jan 2008 13:12 GMT
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

> 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
[quoted text clipped - 36 lines]
>>
>> Vista Small Business, Office XP
Graham Mayor - 13 Jan 2008 15:19 GMT
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.

Signature

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> 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
[quoted text clipped - 35 lines]
>>
>> Vista Small Business, Office XP
Graham Mayor - 12 Jan 2008 15:01 GMT
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

Signature

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Dear all,
>
[quoted text clipped - 4 lines]
> Best regards,
> Shirley Nomey
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.