Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.docmanagement
RPMitchal RPMitchal is offline
external usenet poster
 
Posts: 135
Default Looping Macro for Selection String Between Quotation Marks

Word 2003

I have attempted to put together the below looping macro which essentially
searches for a word or words surrounded by quotation marks in Document (2),

selects everything between and including the opening and closing quotation
marks by using the "Extend" feature (F8);

copies the selected string and pastes it at the cursor position in Document
(1);

inserts a paragraph return; and

then switches back to Document (2), advances past the highlighted selection
and repeats the same functions until reaching the end of the document.

The macro seems to work just fine until such time as I insert the "Do While"
and the "Loop" commands in an attempt to get the macro to repeat itself until
reaching the end of the document.

Obviously, I am missing at the very least €“ one step - and would very much
appreciate any assistance or insight into what I am doing incorrectly. If
the below macro is completely off the mark, I would appreciate being
furnished with the coding for a macro that actually would work and how me the
errors of my ways.

Thanks €“ Rod

Sub Definition()
'
' Definition Macro
' Macro recorded 7/31/2008 by Rod
'
Selection.Find.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
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = """"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
Selection.Copy
Windows(1).Activate
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeParagraph
Windows(2).Activate
Selection.MoveRight Unit:=wdWord, Count:=1
Loop

End Sub

  #2   Report Post  
Posted to microsoft.public.word.docmanagement
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
Default Looping Macro for Selection String Between Quotation Marks

Hi Rod,

Unfortunately, this is one of those situations where the macro recorder leads
you badly astray. Because it can only use the Selection object (corresponding to
the physical cursor in the document), it has to do the Activate to jump from
document to document. In the process, it tends to lose track of what properties
are set for the Find. Complicating that, you've put the Do While statement in
the wrong place, so the Find is only done once.

Here's a version that works. The document that receives the copies of the quoted
text is a new blank one created by the Documents.Add statement; that can be
changed if necessary.

Sub demo()
Dim Doc1 As Document, Doc2 As Document
Dim oRg1 As Range, oRg2 As Range

Set Doc1 = ActiveDocument
Set Doc2 = Documents.Add

Set oRg1 = Doc1.Content

With oRg1.Find
.Text = """"
.Forward = True
.Format = False
.Wrap = wdFindStop
Do While .Execute
' oRg1 points to starting quote
' extend it to include the ending quote
oRg1.MoveEndUntil Cset:="""", Count:=wdForward
oRg1.MoveEnd Unit:=wdCharacter, Count:=1

' point oRg2 to the end of Doc2
Set oRg2 = Doc2.Content
oRg2.Collapse Direction:=wdCollapseEnd

' "copy" without using clipboard
oRg2.FormattedText = oRg1.FormattedText

' add a paragraph mark at the end
oRg2.Collapse Direction:=wdCollapseEnd
oRg2.Text = vbCr

' prepare to find next pair of quotes
oRg1.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.

On Mon, 4 Aug 2008 13:46:43 -0700, RPMitchal
wrote:

Word 2003

I have attempted to put together the below looping macro which essentially
searches for a word or words surrounded by quotation marks in Document (2),

selects everything between and including the opening and closing quotation
marks by using the "Extend" feature (F8);

copies the selected string and pastes it at the cursor position in Document
(1);

inserts a paragraph return; and

then switches back to Document (2), advances past the highlighted selection
and repeats the same functions until reaching the end of the document.

The macro seems to work just fine until such time as I insert the "Do While"
and the "Loop" commands in an attempt to get the macro to repeat itself until
reaching the end of the document.

Obviously, I am missing at the very least – one step - and would very much
appreciate any assistance or insight into what I am doing incorrectly. If
the below macro is completely off the mark, I would appreciate being
furnished with the coding for a macro that actually would work and how me the
errors of my ways.

Thanks – Rod

Sub Definition()
'
' Definition Macro
' Macro recorded 7/31/2008 by Rod
'
Selection.Find.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
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = """"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
Selection.Copy
Windows(1).Activate
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeParagraph
Windows(2).Activate
Selection.MoveRight Unit:=wdWord, Count:=1
Loop

End Sub

  #3   Report Post  
Posted to microsoft.public.word.docmanagement
RPMitchal RPMitchal is offline
external usenet poster
 
Posts: 135
Default Looping Macro for Selection String Between Quotation Marks

Hello Jay:

As always, thanks so much for your assistance in this situation. I've been
at the point where I don't quite know what I would do without you and your
fellow gurus who monitor and contribute to this Forum.

I am attempting so very desperately to make heads or tails of VBA and all
that it entails and I have to say that it's one of the most difficult and/or
challenging experiences of my working career. However, with assistance from
you and the above-mentioned "gurus" I continue to hope for an epipany. :-)

Thanks so very much - Rod

"Jay Freedman" wrote:

Hi Rod,

Unfortunately, this is one of those situations where the macro recorder leads
you badly astray. Because it can only use the Selection object (corresponding to
the physical cursor in the document), it has to do the Activate to jump from
document to document. In the process, it tends to lose track of what properties
are set for the Find. Complicating that, you've put the Do While statement in
the wrong place, so the Find is only done once.

Here's a version that works. The document that receives the copies of the quoted
text is a new blank one created by the Documents.Add statement; that can be
changed if necessary.

Sub demo()
Dim Doc1 As Document, Doc2 As Document
Dim oRg1 As Range, oRg2 As Range

Set Doc1 = ActiveDocument
Set Doc2 = Documents.Add

Set oRg1 = Doc1.Content

With oRg1.Find
.Text = """"
.Forward = True
.Format = False
.Wrap = wdFindStop
Do While .Execute
' oRg1 points to starting quote
' extend it to include the ending quote
oRg1.MoveEndUntil Cset:="""", Count:=wdForward
oRg1.MoveEnd Unit:=wdCharacter, Count:=1

' point oRg2 to the end of Doc2
Set oRg2 = Doc2.Content
oRg2.Collapse Direction:=wdCollapseEnd

' "copy" without using clipboard
oRg2.FormattedText = oRg1.FormattedText

' add a paragraph mark at the end
oRg2.Collapse Direction:=wdCollapseEnd
oRg2.Text = vbCr

' prepare to find next pair of quotes
oRg1.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.

On Mon, 4 Aug 2008 13:46:43 -0700, RPMitchal
wrote:

Word 2003

I have attempted to put together the below looping macro which essentially
searches for a word or words surrounded by quotation marks in Document (2),

selects everything between and including the opening and closing quotation
marks by using the "Extend" feature (F8);

copies the selected string and pastes it at the cursor position in Document
(1);

inserts a paragraph return; and

then switches back to Document (2), advances past the highlighted selection
and repeats the same functions until reaching the end of the document.

The macro seems to work just fine until such time as I insert the "Do While"
and the "Loop" commands in an attempt to get the macro to repeat itself until
reaching the end of the document.

Obviously, I am missing at the very least €“ one step - and would very much
appreciate any assistance or insight into what I am doing incorrectly. If
the below macro is completely off the mark, I would appreciate being
furnished with the coding for a macro that actually would work and how me the
errors of my ways.

Thanks €“ Rod

Sub Definition()
'
' Definition Macro
' Macro recorded 7/31/2008 by Rod
'
Selection.Find.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
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = """"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
Selection.Copy
Windows(1).Activate
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeParagraph
Windows(2).Activate
Selection.MoveRight Unit:=wdWord, Count:=1
Loop

End Sub


  #4   Report Post  
Posted to microsoft.public.word.docmanagement
RPMitchal RPMitchal is offline
external usenet poster
 
Posts: 135
Default Looping Macro for Selection String Between Quotation Marks

Jay:

Just a quick further question in this regard. What is meant by or what is
the purpose of using the "o" within the coding that you've supplied? For
example:

Dim "o"Rg1 As Range, "o"Rg2 as Range

I've seen it before but have been unable to find any reference to it's use
among the several manuals that I have reviewed.

Thanks Again - Rod


"Jay Freedman" wrote:

Hi Rod,

Unfortunately, this is one of those situations where the macro recorder leads
you badly astray. Because it can only use the Selection object (corresponding to
the physical cursor in the document), it has to do the Activate to jump from
document to document. In the process, it tends to lose track of what properties
are set for the Find. Complicating that, you've put the Do While statement in
the wrong place, so the Find is only done once.

Here's a version that works. The document that receives the copies of the quoted
text is a new blank one created by the Documents.Add statement; that can be
changed if necessary.

Sub demo()
Dim Doc1 As Document, Doc2 As Document
Dim oRg1 As Range, oRg2 As Range

Set Doc1 = ActiveDocument
Set Doc2 = Documents.Add

Set oRg1 = Doc1.Content

With oRg1.Find
.Text = """"
.Forward = True
.Format = False
.Wrap = wdFindStop
Do While .Execute
' oRg1 points to starting quote
' extend it to include the ending quote
oRg1.MoveEndUntil Cset:="""", Count:=wdForward
oRg1.MoveEnd Unit:=wdCharacter, Count:=1

' point oRg2 to the end of Doc2
Set oRg2 = Doc2.Content
oRg2.Collapse Direction:=wdCollapseEnd

' "copy" without using clipboard
oRg2.FormattedText = oRg1.FormattedText

' add a paragraph mark at the end
oRg2.Collapse Direction:=wdCollapseEnd
oRg2.Text = vbCr

' prepare to find next pair of quotes
oRg1.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.

On Mon, 4 Aug 2008 13:46:43 -0700, RPMitchal
wrote:

Word 2003

I have attempted to put together the below looping macro which essentially
searches for a word or words surrounded by quotation marks in Document (2),

selects everything between and including the opening and closing quotation
marks by using the "Extend" feature (F8);

copies the selected string and pastes it at the cursor position in Document
(1);

inserts a paragraph return; and

then switches back to Document (2), advances past the highlighted selection
and repeats the same functions until reaching the end of the document.

The macro seems to work just fine until such time as I insert the "Do While"
and the "Loop" commands in an attempt to get the macro to repeat itself until
reaching the end of the document.

Obviously, I am missing at the very least €“ one step - and would very much
appreciate any assistance or insight into what I am doing incorrectly. If
the below macro is completely off the mark, I would appreciate being
furnished with the coding for a macro that actually would work and how me the
errors of my ways.

Thanks €“ Rod

Sub Definition()
'
' Definition Macro
' Macro recorded 7/31/2008 by Rod
'
Selection.Find.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
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = """"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
Selection.Copy
Windows(1).Activate
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeParagraph
Windows(2).Activate
Selection.MoveRight Unit:=wdWord, Count:=1
Loop

End Sub


  #5   Report Post  
Posted to microsoft.public.word.docmanagement
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
Default Looping Macro for Selection String Between Quotation Marks

Hi Rod,

It's a convention (that is, something useful but not required). A Range is
an "object" (a programming structure that contains many values, as opposed
to a simple variable with a single value). VBA requires the use of the Set
keyword when you assign any object, as in "Set oRg1 = Doc1.Content". I find
it helpful to use the "o" prefix for objects as a reminder to include the
Set. But I don't always use it consistently; in this case I didn't name the
documents oDoc1 and oDoc2 even though they're objects too.

This is a single example of something called "Hungarian notation" (see
http://en.wikipedia.org/wiki/Hungarian_notation). There are various opinions
about how useful that notation is.

-- Jay

RPMitchal wrote:
Jay:

Just a quick further question in this regard. What is meant by or
what is the purpose of using the "o" within the coding that you've
supplied? For example:

Dim "o"Rg1 As Range, "o"Rg2 as Range

I've seen it before but have been unable to find any reference to
it's use among the several manuals that I have reviewed.

Thanks Again - Rod


"Jay Freedman" wrote:

Hi Rod,

Unfortunately, this is one of those situations where the macro
recorder leads you badly astray. Because it can only use the
Selection object (corresponding to the physical cursor in the
document), it has to do the Activate to jump from document to
document. In the process, it tends to lose track of what properties
are set for the Find. Complicating that, you've put the Do While
statement in the wrong place, so the Find is only done once.

Here's a version that works. The document that receives the copies
of the quoted text is a new blank one created by the Documents.Add
statement; that can be changed if necessary.

Sub demo()
Dim Doc1 As Document, Doc2 As Document
Dim oRg1 As Range, oRg2 As Range

Set Doc1 = ActiveDocument
Set Doc2 = Documents.Add

Set oRg1 = Doc1.Content

With oRg1.Find
.Text = """"
.Forward = True
.Format = False
.Wrap = wdFindStop
Do While .Execute
' oRg1 points to starting quote
' extend it to include the ending quote
oRg1.MoveEndUntil Cset:="""", Count:=wdForward
oRg1.MoveEnd Unit:=wdCharacter, Count:=1

' point oRg2 to the end of Doc2
Set oRg2 = Doc2.Content
oRg2.Collapse Direction:=wdCollapseEnd

' "copy" without using clipboard
oRg2.FormattedText = oRg1.FormattedText

' add a paragraph mark at the end
oRg2.Collapse Direction:=wdCollapseEnd
oRg2.Text = vbCr

' prepare to find next pair of quotes
oRg1.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Mon, 4 Aug 2008 13:46:43 -0700, RPMitchal
wrote:

Word 2003

I have attempted to put together the below looping macro which
essentially searches for a word or words surrounded by quotation
marks in Document (2),

selects everything between and including the opening and closing
quotation marks by using the "Extend" feature (F8);

copies the selected string and pastes it at the cursor position in
Document (1);

inserts a paragraph return; and

then switches back to Document (2), advances past the highlighted
selection and repeats the same functions until reaching the end of
the document.

The macro seems to work just fine until such time as I insert the
"Do While" and the "Loop" commands in an attempt to get the macro
to repeat itself until reaching the end of the document.

Obviously, I am missing at the very least - one step - and would
very much appreciate any assistance or insight into what I am doing
incorrectly. If the below macro is completely off the mark, I
would appreciate being furnished with the coding for a macro that
actually would work and how me the errors of my ways.

Thanks - Rod

Sub Definition()
'
' Definition Macro
' Macro recorded 7/31/2008 by Rod
'
Selection.Find.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
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = """"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
Selection.Copy
Windows(1).Activate
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeParagraph
Windows(2).Activate
Selection.MoveRight Unit:=wdWord, Count:=1
Loop

End Sub





  #6   Report Post  
Posted to microsoft.public.word.docmanagement
RPMitchal RPMitchal is offline
external usenet poster
 
Posts: 135
Default Looping Macro for Selection String Between Quotation Marks

Hello Again Jay:

I have, in the interim, been studying and researching the code that you
supplied. I found that up to a point it is headed in the correct direction €“
and for that I thank you.

I ran the code and the below is an example of the result.

(€œJay Freeman€)
in Doc1 - presented me with

"J¶
")¶
in Doc2

It would appear that instead of selecting the first quotation mark and
extending the selection to the next occurrence of a quotation mark, I am
being presented with the quotation mark and only the next following
character.

In *Stepping Through* the coding, it would appear (to my limited knowledge)
that the below block of coding may contain the culprit in this regard.

' oRg1 points to starting quote
' extend it to include the ending quote
oRg1.MoveEndUntil Cset:="""", Count:=wdForward
oRg1.MoveEnd Unit:=wdCharacter, Count:=1

I would certainly appreciate you taking another look at the above coding in
hopes of shedding further light on this situation.

Thanks Again €“ Rod



"Jay Freedman" wrote:

Hi Rod,

It's a convention (that is, something useful but not required). A Range is
an "object" (a programming structure that contains many values, as opposed
to a simple variable with a single value). VBA requires the use of the Set
keyword when you assign any object, as in "Set oRg1 = Doc1.Content". I find
it helpful to use the "o" prefix for objects as a reminder to include the
Set. But I don't always use it consistently; in this case I didn't name the
documents oDoc1 and oDoc2 even though they're objects too.

This is a single example of something called "Hungarian notation" (see
http://en.wikipedia.org/wiki/Hungarian_notation). There are various opinions
about how useful that notation is.

-- Jay

RPMitchal wrote:
Jay:

Just a quick further question in this regard. What is meant by or
what is the purpose of using the "o" within the coding that you've
supplied? For example:

Dim "o"Rg1 As Range, "o"Rg2 as Range

I've seen it before but have been unable to find any reference to
it's use among the several manuals that I have reviewed.

Thanks Again - Rod


"Jay Freedman" wrote:

Hi Rod,

Unfortunately, this is one of those situations where the macro
recorder leads you badly astray. Because it can only use the
Selection object (corresponding to the physical cursor in the
document), it has to do the Activate to jump from document to
document. In the process, it tends to lose track of what properties
are set for the Find. Complicating that, you've put the Do While
statement in the wrong place, so the Find is only done once.

Here's a version that works. The document that receives the copies
of the quoted text is a new blank one created by the Documents.Add
statement; that can be changed if necessary.

Sub demo()
Dim Doc1 As Document, Doc2 As Document
Dim oRg1 As Range, oRg2 As Range

Set Doc1 = ActiveDocument
Set Doc2 = Documents.Add

Set oRg1 = Doc1.Content

With oRg1.Find
.Text = """"
.Forward = True
.Format = False
.Wrap = wdFindStop
Do While .Execute
' oRg1 points to starting quote
' extend it to include the ending quote
oRg1.MoveEndUntil Cset:="""", Count:=wdForward
oRg1.MoveEnd Unit:=wdCharacter, Count:=1

' point oRg2 to the end of Doc2
Set oRg2 = Doc2.Content
oRg2.Collapse Direction:=wdCollapseEnd

' "copy" without using clipboard
oRg2.FormattedText = oRg1.FormattedText

' add a paragraph mark at the end
oRg2.Collapse Direction:=wdCollapseEnd
oRg2.Text = vbCr

' prepare to find next pair of quotes
oRg1.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Mon, 4 Aug 2008 13:46:43 -0700, RPMitchal
wrote:

Word 2003

I have attempted to put together the below looping macro which
essentially searches for a word or words surrounded by quotation
marks in Document (2),

selects everything between and including the opening and closing
quotation marks by using the "Extend" feature (F8);

copies the selected string and pastes it at the cursor position in
Document (1);

inserts a paragraph return; and

then switches back to Document (2), advances past the highlighted
selection and repeats the same functions until reaching the end of
the document.

The macro seems to work just fine until such time as I insert the
"Do While" and the "Loop" commands in an attempt to get the macro
to repeat itself until reaching the end of the document.

Obviously, I am missing at the very least - one step - and would
very much appreciate any assistance or insight into what I am doing
incorrectly. If the below macro is completely off the mark, I
would appreciate being furnished with the coding for a macro that
actually would work and how me the errors of my ways.

Thanks - Rod

Sub Definition()
'
' Definition Macro
' Macro recorded 7/31/2008 by Rod
'
Selection.Find.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
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = """"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
Selection.Copy
Windows(1).Activate
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeParagraph
Windows(2).Activate
Selection.MoveRight Unit:=wdWord, Count:=1
Loop

End Sub




  #7   Report Post  
Posted to microsoft.public.word.docmanagement
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
Default Looping Macro for Selection String Between Quotation Marks

Hi Rod,

In the line

oRg1.MoveEndUntil Cset:="""", Count:=wdForward

remove the middle two of the four double-quotes and replace them with a
closing "curly quote" (Unicode character 201D). I would give you a line to
copy and paste, but Outlook Express would just change the character as I
post.

The Find command, when asked to look for a "straight quote", will match a
straight quote or either opening or closing curly quotes. The MoveEndUntil
command is picky and will look only for the specific character in the Cset
argument. I didn't realize you were using curly quotes, so I wrote the macro
for straight quotes.

-- Jay

RPMitchal wrote:
Hello Again Jay:

I have, in the interim, been studying and researching the code that
you supplied. I found that up to a point it is headed in the correct
direction - and for that I thank you.

I ran the code and the below is an example of the result.

("Jay Freeman")
in Doc1 - presented me with

"J¶
")¶
in Doc2

It would appear that instead of selecting the first quotation mark and
extending the selection to the next occurrence of a quotation mark, I
am being presented with the quotation mark and only the next following
character.

In *Stepping Through* the coding, it would appear (to my limited
knowledge) that the below block of coding may contain the culprit in
this regard.

' oRg1 points to starting quote
' extend it to include the ending quote
oRg1.MoveEndUntil Cset:="""", Count:=wdForward
oRg1.MoveEnd Unit:=wdCharacter, Count:=1

I would certainly appreciate you taking another look at the above
coding in hopes of shedding further light on this situation.

Thanks Again - Rod



"Jay Freedman" wrote:

Hi Rod,

It's a convention (that is, something useful but not required). A
Range is an "object" (a programming structure that contains many
values, as opposed to a simple variable with a single value). VBA
requires the use of the Set keyword when you assign any object, as
in "Set oRg1 = Doc1.Content". I find it helpful to use the "o"
prefix for objects as a reminder to include the Set. But I don't
always use it consistently; in this case I didn't name the documents
oDoc1 and oDoc2 even though they're objects too.

This is a single example of something called "Hungarian notation"
(see http://en.wikipedia.org/wiki/Hungarian_notation). There are
various opinions about how useful that notation is.

-- Jay

RPMitchal wrote:
Jay:

Just a quick further question in this regard. What is meant by or
what is the purpose of using the "o" within the coding that you've
supplied? For example:

Dim "o"Rg1 As Range, "o"Rg2 as Range

I've seen it before but have been unable to find any reference to
it's use among the several manuals that I have reviewed.

Thanks Again - Rod


"Jay Freedman" wrote:

Hi Rod,

Unfortunately, this is one of those situations where the macro
recorder leads you badly astray. Because it can only use the
Selection object (corresponding to the physical cursor in the
document), it has to do the Activate to jump from document to
document. In the process, it tends to lose track of what properties
are set for the Find. Complicating that, you've put the Do While
statement in the wrong place, so the Find is only done once.

Here's a version that works. The document that receives the copies
of the quoted text is a new blank one created by the Documents.Add
statement; that can be changed if necessary.

Sub demo()
Dim Doc1 As Document, Doc2 As Document
Dim oRg1 As Range, oRg2 As Range

Set Doc1 = ActiveDocument
Set Doc2 = Documents.Add

Set oRg1 = Doc1.Content

With oRg1.Find
.Text = """"
.Forward = True
.Format = False
.Wrap = wdFindStop
Do While .Execute
' oRg1 points to starting quote
' extend it to include the ending quote
oRg1.MoveEndUntil Cset:="""", Count:=wdForward
oRg1.MoveEnd Unit:=wdCharacter, Count:=1

' point oRg2 to the end of Doc2
Set oRg2 = Doc2.Content
oRg2.Collapse Direction:=wdCollapseEnd

' "copy" without using clipboard
oRg2.FormattedText = oRg1.FormattedText

' add a paragraph mark at the end
oRg2.Collapse Direction:=wdCollapseEnd
oRg2.Text = vbCr

' prepare to find next pair of quotes
oRg1.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Mon, 4 Aug 2008 13:46:43 -0700, RPMitchal
wrote:

Word 2003

I have attempted to put together the below looping macro which
essentially searches for a word or words surrounded by quotation
marks in Document (2),

selects everything between and including the opening and closing
quotation marks by using the "Extend" feature (F8);

copies the selected string and pastes it at the cursor position in
Document (1);

inserts a paragraph return; and

then switches back to Document (2), advances past the highlighted
selection and repeats the same functions until reaching the end of
the document.

The macro seems to work just fine until such time as I insert the
"Do While" and the "Loop" commands in an attempt to get the macro
to repeat itself until reaching the end of the document.

Obviously, I am missing at the very least - one step - and would
very much appreciate any assistance or insight into what I am
doing incorrectly. If the below macro is completely off the
mark, I would appreciate being furnished with the coding for a
macro that actually would work and how me the errors of my ways.

Thanks - Rod

Sub Definition()
'
' Definition Macro
' Macro recorded 7/31/2008 by Rod
'
Selection.Find.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
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = """"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
Selection.Copy
Windows(1).Activate
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeParagraph
Windows(2).Activate
Selection.MoveRight Unit:=wdWord, Count:=1
Loop

End Sub



  #8   Report Post  
Posted to microsoft.public.word.docmanagement
RPMitchal RPMitchal is offline
external usenet poster
 
Posts: 135
Default Looping Macro for Selection String Between Quotation Marks

Jay:

What a guy!

I'm going to put into effect your latest and greatest suggestion and will
keep you posted on its success.

Thanks Again - Rod

"Jay Freedman" wrote:

Hi Rod,

In the line

oRg1.MoveEndUntil Cset:="""", Count:=wdForward

remove the middle two of the four double-quotes and replace them with a
closing "curly quote" (Unicode character 201D). I would give you a line to
copy and paste, but Outlook Express would just change the character as I
post.

The Find command, when asked to look for a "straight quote", will match a
straight quote or either opening or closing curly quotes. The MoveEndUntil
command is picky and will look only for the specific character in the Cset
argument. I didn't realize you were using curly quotes, so I wrote the macro
for straight quotes.

-- Jay

RPMitchal wrote:
Hello Again Jay:

I have, in the interim, been studying and researching the code that
you supplied. I found that up to a point it is headed in the correct
direction - and for that I thank you.

I ran the code and the below is an example of the result.

("Jay Freeman")
in Doc1 - presented me with

"J¶
")¶
in Doc2

It would appear that instead of selecting the first quotation mark and
extending the selection to the next occurrence of a quotation mark, I
am being presented with the quotation mark and only the next following
character.

In *Stepping Through* the coding, it would appear (to my limited
knowledge) that the below block of coding may contain the culprit in
this regard.

' oRg1 points to starting quote
' extend it to include the ending quote
oRg1.MoveEndUntil Cset:="""", Count:=wdForward
oRg1.MoveEnd Unit:=wdCharacter, Count:=1

I would certainly appreciate you taking another look at the above
coding in hopes of shedding further light on this situation.

Thanks Again - Rod



"Jay Freedman" wrote:

Hi Rod,

It's a convention (that is, something useful but not required). A
Range is an "object" (a programming structure that contains many
values, as opposed to a simple variable with a single value). VBA
requires the use of the Set keyword when you assign any object, as
in "Set oRg1 = Doc1.Content". I find it helpful to use the "o"
prefix for objects as a reminder to include the Set. But I don't
always use it consistently; in this case I didn't name the documents
oDoc1 and oDoc2 even though they're objects too.

This is a single example of something called "Hungarian notation"
(see http://en.wikipedia.org/wiki/Hungarian_notation). There are
various opinions about how useful that notation is.

-- Jay

RPMitchal wrote:
Jay:

Just a quick further question in this regard. What is meant by or
what is the purpose of using the "o" within the coding that you've
supplied? For example:

Dim "o"Rg1 As Range, "o"Rg2 as Range

I've seen it before but have been unable to find any reference to
it's use among the several manuals that I have reviewed.

Thanks Again - Rod


"Jay Freedman" wrote:

Hi Rod,

Unfortunately, this is one of those situations where the macro
recorder leads you badly astray. Because it can only use the
Selection object (corresponding to the physical cursor in the
document), it has to do the Activate to jump from document to
document. In the process, it tends to lose track of what properties
are set for the Find. Complicating that, you've put the Do While
statement in the wrong place, so the Find is only done once.

Here's a version that works. The document that receives the copies
of the quoted text is a new blank one created by the Documents.Add
statement; that can be changed if necessary.

Sub demo()
Dim Doc1 As Document, Doc2 As Document
Dim oRg1 As Range, oRg2 As Range

Set Doc1 = ActiveDocument
Set Doc2 = Documents.Add

Set oRg1 = Doc1.Content

With oRg1.Find
.Text = """"
.Forward = True
.Format = False
.Wrap = wdFindStop
Do While .Execute
' oRg1 points to starting quote
' extend it to include the ending quote
oRg1.MoveEndUntil Cset:="""", Count:=wdForward
oRg1.MoveEnd Unit:=wdCharacter, Count:=1

' point oRg2 to the end of Doc2
Set oRg2 = Doc2.Content
oRg2.Collapse Direction:=wdCollapseEnd

' "copy" without using clipboard
oRg2.FormattedText = oRg1.FormattedText

' add a paragraph mark at the end
oRg2.Collapse Direction:=wdCollapseEnd
oRg2.Text = vbCr

' prepare to find next pair of quotes
oRg1.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Mon, 4 Aug 2008 13:46:43 -0700, RPMitchal
wrote:

Word 2003

I have attempted to put together the below looping macro which
essentially searches for a word or words surrounded by quotation
marks in Document (2),

selects everything between and including the opening and closing
quotation marks by using the "Extend" feature (F8);

copies the selected string and pastes it at the cursor position in
Document (1);

inserts a paragraph return; and

then switches back to Document (2), advances past the highlighted
selection and repeats the same functions until reaching the end of
the document.

The macro seems to work just fine until such time as I insert the
"Do While" and the "Loop" commands in an attempt to get the macro
to repeat itself until reaching the end of the document.

Obviously, I am missing at the very least - one step - and would
very much appreciate any assistance or insight into what I am
doing incorrectly. If the below macro is completely off the
mark, I would appreciate being furnished with the coding for a
macro that actually would work and how me the errors of my ways.

Thanks - Rod

Sub Definition()
'
' Definition Macro
' Macro recorded 7/31/2008 by Rod
'
Selection.Find.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
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = """"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
Selection.Copy
Windows(1).Activate
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeParagraph
Windows(2).Activate
Selection.MoveRight Unit:=wdWord, Count:=1
Loop

End Sub




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 JulieMae Microsoft Word Help 10 September 18th 13 02:29 PM
My question marks, apostrophies and quotation marks are weird. pj Microsoft Word Help 1 January 10th 08 10:26 AM
adding quotation marks around a selection (can you help me tweak this code?) Tom Mailmerge 6 December 29th 06 02:24 PM
Why don't my quotation marks appear until I hit another key? Matter of Fact Microsoft Word Help 1 November 17th 05 06:51 PM
quotation marks Michael Microsoft Word Help 3 November 17th 05 03:26 AM


All times are GMT +1. The time now is 02:28 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"