Replacing special quote marks

M

Mika

Hi all,

I'm using Word 97. When I type quote marks ("") they will be converted
to Word's special ones as wanted.

Anyway, here are my problems and questions:

'This works OK
MyText = ReplaceString(MyText, Chr$(133), "...")

'This doesn't work, why not?
MyText = ReplaceString(MyText, Chr$(147), Chr$(34))

'This doesn't work either, why not?
MyText = ReplaceString(MyText, AscW(Chr$(147)), Chr$(34))

Public Function ReplaceString(MyText As Range, OriginalText, NewText As
String)
With MyText.Find
.Text = OriginalText
.Replacement.Text = NewText
End With

MyText.Find.Execute Replace:=wdReplaceAll

ReplaceString = MyText
End Function

Thanks a lot,
Mika
 
J

Jay Freedman

Hi Mika,

The problem is that you have Tools > AutoCorrect > AutoFormat As You
Type set to replace "straight quotes" with "smart quotes". As soon as
your macro replaces any occurrence of Chr$(147) with Chr$(34), the
AutoFormat changes it back again. It happens so fast that it appears
as if nothing happened. The sample code below shows how to turn off
the setting temporarily while the replacement is done, and then return
it to the user's preferred value.

This shouldn't have any effect on the correctness of the code, but I'd
suggest some changes in the design. For one thing, it makes no sense
to pass back as the function's value the same range that you pass in
as an argument. If you think you might need to know whether the
function actually made any replacements, you can pass back the value
of MyText.Find.Found. Also, in the argument list, each argument must
have an As <datatype> clause; otherwise it defaults to Variant type,
which is a waste of space and time.

Sub test()
Dim oRg As Range
Dim bOldSmartQuotes As Boolean
Dim bFound As Boolean

bOldSmartQuotes = Options.AutoFormatAsYouTypeReplaceQuotes
Options.AutoFormatAsYouTypeReplaceQuotes = False

Set oRg = Selection.Range
bFound = ReplaceString(oRg, Chr$(147), Chr$(34))

Options.AutoFormatAsYouTypeReplaceQuotes = bOldSmartQuotes

If Not bFound Then
MsgBox "Didn't find any."
End If
End Sub

Public Function ReplaceString(MyText As Range, _
OriginalText As String, NewText As String) _
As Boolean

With MyText.Find
.Text = OriginalText
.Replacement.Text = NewText
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
ReplaceString = .Found
End With

End Function
 

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