Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Tom Tom is offline
external usenet poster
 
Posts: 61
Default adding quotation marks around a selection (can you help me tweak this code?)

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(wdRefTypeHea ding)

'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

  #2   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Doug Robbins - Word MVP Doug Robbins - Word MVP is offline
external usenet poster
 
Posts: 8,832
Default adding quotation marks around a selection (can you help me tweak this code?)

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

"Tom" wrote in message
ups.com...
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(wdRefTypeHea ding)

'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



  #3   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Tom Tom is offline
external usenet poster
 
Posts: 61
Default adding quotation marks around a selection (can you help me tweak this code?)

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?

  #4   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Tom Tom is offline
external usenet poster
 
Posts: 61
Default adding quotation marks around a selection (can you help me tweak this code?)

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/arch...g-pasted-code/.
Is that how you do a find and replace with character codes?

Thanks for your help.

Tom

  #5   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Tom Tom is offline
external usenet poster
 
Posts: 61
Default adding quotation marks around a selection (can you help me tweak this code?)

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?



  #6   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Doug Robbins - Word MVP Doug Robbins - Word MVP is offline
external usenet poster
 
Posts: 8,832
Default adding quotation marks around a selection (can you help me tweak this code?)

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

"Tom" wrote in message
ups.com...
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?


  #7   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Tom Tom is offline
external usenet poster
 
Posts: 61
Default adding quotation marks around a selection (can you help me tweak this code?)

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.

Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Quotation marks Quotation marks Microsoft Word Help 1 June 20th 06 07:32 PM
How do I include quotation marks in a TOC field code? Lilli D Microsoft Word Help 17 April 13th 06 11:59 PM
How do I change formatting from guillemets to quotation marks? amyrebelyell Microsoft Word Help 1 March 29th 06 09:57 AM
Need Quotation marks to appear in TOC - TK Microsoft Word Help 2 September 9th 05 12:14 AM
adding the post code Louise Mailmerge 3 May 4th 05 06:27 PM


All times are GMT +1. The time now is 05:52 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"