Sub ReplaceQuotes()
Dim vFindText As Variant
Dim vReplText As Variant
Dim sFormat As Boolean
Dim sQuotes As String
Dim i As Long
'Ask the user whether to format with smart or straight quotes
sQuotes = MsgBox("Click 'Yes' to convert smart quotes to straight quotes." & vbCr & _
"Click 'No' to convert straight quotes to smart quotes.", _
vbYesNo, "Convert quotes")
'Record the current setting of the autoformat option to replace straight quotes with smart quotes
sFormat = Options.AutoFormatAsYouTypeReplaceQuotes
If sQuotes = vbYes Then 'The user has clicked 'Yes'
'Define the lists of smart quotes and their replacements
vFindText = Array(Chr(147), Chr(148), Chr(145), Chr(146))
vReplText = Array(Chr(34), Chr(34), Chr(39), Chr(39))
'Set the autoformat option to replace straight quotes with smart quotes to off
Options.AutoFormatAsYouTypeReplaceQuotes = False
'Start from the top of the document
Selection.HomeKey wdStory
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.MatchCase = True
'replace each item from the first array with the corresponding item in the second array
For i = LBound(vFindText) To UBound(vFindText)
.Text = vFindText(i)
.Replacement.Text = vReplText(i)
.Execute Replace:=wdReplaceAll
Next i
End With
Else 'User clicked 'No'
'Use autoformat to replace straight quotes with smart quotes
Options.AutoFormatReplaceQuotes = True
Selection.Range.AutoFormat
End If
'Finally reset the autoformat setting to its start configuration
Options.AutoFormatAsYouTypeReplaceQuotes = sFormat
End Sub |
No comments:
Post a Comment