adding quotation marks around a selection (can you help me tweak this code?)

T

Tom

This is a long shot here, but I figured I'd try this forum for help.

I have a macro that takes a hyperlink that says, for example,
Performing Advanced Searches and it converts this to Performing
Advanced Searches on page 7. (In other words, it adds a page number to
the cross reference).

The only problem is that our style guide would have us write it with
quotation marks, like this: See "Performing Advanced Searches" on page
7.

The macro is kind of long. I was hoping someone on this forum might
advise me how to tweak the code so that it includes the quotation marks
around the selection. Thanks.

Sub CrossReferences()
' This bit added to set Smart Cut and Paste off while macro runs
' Thanks to JScher at Woody's Lounge, www.wopr.com

Dim blnSmartCutAndPaste As Boolean
blnSmartCutAndPaste = Options.SmartCutPaste
Options.SmartCutPaste = False

' This macro written by Tannis Turnbull. RH Forum

Dim myHeadings() As String
'Dim myPageNumbers() As String
Dim i As Integer

'store all heading information
myHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)

'move the cursor to section 3
Selection.HomeKey Unit:=wdStory
Selection.Move wdSection, 2

'search for text with hyperlink style
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Hyperlink")

With Selection.Find
..Text = ""
..Replacement.Text = ""
..Forward = True
..Wrap = wdFindContinue
..Format = True
..MatchCase = False
..MatchWholeWord = False
..MatchWildcards = False
..MatchSoundsLike = False
..MatchAllWordForms = False

..Execute FindText:="", Format:=True
While .Found = True
'loop around refs to find the right one and update link
For i = 1 To UBound(myHeadings)
If Selection = Trim(myHeadings(i)) Then
'update the style with a real link and page info
Selection.Delete
Selection.InsertCrossReference ReferenceType:="Heading",
ReferenceKind:= _
wdContentText, ReferenceItem:=CStr(i), InsertAsHyperlink:=True, _
IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
' in Word XP amend the line above to IncludePosition:=False
Selection.TypeText Text:=" on page "
Selection.InsertCrossReference ReferenceType:="Heading",
ReferenceKind:= _
wdPageNumber, ReferenceItem:=CStr(i), InsertAsHyperlink:=True
'IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
' in Word XP amend the line above to IncludePosition:=False
End If
Next i
..Execute
Wend
End With
' Next line added as part of JScher's change, restores setting to what
user had before.
Options.SmartCutPaste = blnSmartCutAndPaste
End Sub
 
D

Doug Robbins - Word MVP

Use Chr(34) in code to insert "

Replacing the following line of your code

Selection.TypeText Text:=" on page "

with

Selection.TypeText Text:= Chr(34) & " on page "

will insert the closing quotation mark. You will need to work out where the
other needs to go in your code.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
T

Tom

Thanks Doug. That works to put the closing quotation mark there. I ran
into just one problem. When I convert my straight quotes to curly
quotes, it puts the closing quotation mark backwards. Do you know why
it might be doing that?
 
T

Tom

Maybe I could put some other character code in there as a dummy marker,
and then at the end, replace it with chr(148). This character code has
the quote marks going the right way. However, I'm not sure how to do
that (or if there's a better way). Maybe the macro would be something
like ...

Find chr(216)
Replace with chr(148)

I saw some other code at
http://www.dailydoseofexcel.com/archives/2006/10/17/cleaning-pasted-code/.
Is that how you do a find and replace with character codes?

Thanks for your help.

Tom
 
T

Tom

All right. I've been toying with this for the past hour and found a
circus-like workaround.

To insert the quotation mark in the front of the selection, I just
added Selection.TypeText Text:=Chr(147) before the macro inserts the
reference. That's fine.

But I was still getting a backwards quotation mark when I inserted the
later reference. So I added a yen character chr(165) before the quote
to make it point the right way. Then later I delete the yen character.

Here's a selection from where I altered the macro:

Selection.TypeText Text:=Chr(147)
Selection.InsertCrossReference ReferenceType:="Heading",
ReferenceKind:= _
wdContentText, ReferenceItem:=CStr(i), InsertAsHyperlink:=True, _
IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
'Selection.TypeText Text:=" on page "
Selection.TypeText Text:=Chr(165) & Chr(148) & " on page "
Selection.InsertCrossReference ReferenceType:="Heading",
ReferenceKind:= _
wdPageNumber, ReferenceItem:=CStr(i), InsertAsHyperlink:=True


I guess the trouble starts when I update my apostrophes to change them
all from straight to curly. Doing so always reverses the direction of
that closing quotation mark.

I wish there was a way to append the line Selection.TypeText
Text:=Chr(148) into the section where it inserts the reference. It just
needs something before the quotation mark to get it going the right
way.

I later delete the insertion of the yen character with this macro (run
after the straight-to-curly update):

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "¥"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

It works, but I feel stupid running it.

(I didn't realize that pressing alt+0165 inserted characters in the
Find field of the Find and Replace dialog box.)

Does anyone have a better solution than this yen character insertion?
 
D

Doug Robbins - Word MVP

I think that the yen character is unnecessary and that

Selection.TypeText Text:=Chr(148) & " on page "

alone should give you the " (the closing curly quote in case it does not
come out in your newsreader)


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

All right. I've been toying with this for the past hour and found a
circus-like workaround.

To insert the quotation mark in the front of the selection, I just
added Selection.TypeText Text:=Chr(147) before the macro inserts the
reference. That's fine.

But I was still getting a backwards quotation mark when I inserted the
later reference. So I added a yen character chr(165) before the quote
to make it point the right way. Then later I delete the yen character.

Here's a selection from where I altered the macro:

Selection.TypeText Text:=Chr(147)
Selection.InsertCrossReference ReferenceType:="Heading",
ReferenceKind:= _
wdContentText, ReferenceItem:=CStr(i), InsertAsHyperlink:=True, _
IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
'Selection.TypeText Text:=" on page "
Selection.TypeText Text:=Chr(165) & Chr(148) & " on page "
Selection.InsertCrossReference ReferenceType:="Heading",
ReferenceKind:= _
wdPageNumber, ReferenceItem:=CStr(i), InsertAsHyperlink:=True


I guess the trouble starts when I update my apostrophes to change them
all from straight to curly. Doing so always reverses the direction of
that closing quotation mark.

I wish there was a way to append the line Selection.TypeText
Text:=Chr(148) into the section where it inserts the reference. It just
needs something before the quotation mark to get it going the right
way.

I later delete the insertion of the yen character with this macro (run
after the straight-to-curly update):

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "¥"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

It works, but I feel stupid running it.

(I didn't realize that pressing alt+0165 inserted characters in the
Find field of the Find and Replace dialog box.)

Does anyone have a better solution than this yen character insertion?
 
T

Tom

Oh, you're right. I was running a different macro after this one that
replaced all straight quotes with curly ones. Running that second macro
changed the direction of the closing quote character in the
crossreferences macro to be the wrong way. I just switched the order of
the macros and it works fine. Thanks for your help with this one.
 

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