Help: Wrapping quotes around selected text

P

Poster

Dudes,

I would like to have a Word macro that can wrap quotes around each line of
selected text, followed by a comma at the end of each line. Preferably it
won't append a comma to the last line, but I'll settle for a solution
without this extra feature.

eg.

Dog
Cat
Fish
Bird

becomes

'Dog',
'Cat',
'Fish',
'Bird'


The reason I need this is to allow me to save a lot of time where I have to
manually create an SQL query that includes a long list of search criteria.

Thanks. The first to provide a solution goes to the top of the class!

Poster
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Poster > écrivait :
In this message, < Poster > wrote:

|| Dudes,
||
|| I would like to have a Word macro that can wrap quotes around each line
of
|| selected text, followed by a comma at the end of each line. Preferably it
|| won't append a comma to the last line, but I'll settle for a solution
|| without this extra feature.
||
|| eg.
||
|| Dog
|| Cat
|| Fish
|| Bird
||
|| becomes
||
|| 'Dog',
|| 'Cat',
|| 'Fish',
|| 'Bird'
||

Dealing with lines in Word can be tricky as they do not really exists as
objects in the VBA model (Neither do pages).

Are these one-word line as in your examples? Are you talking about
paragraphs with wrapped lines? Was "Enter" hit to end each line?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jezebel

Assuming that each of your lines is a single-word paragraph, you can simply
use Find and Replace, manually or via VBA. With 'Use wildcards' checked,
search for

<([!^13]*)^13

and replace with

"\1",^p

You'll have to delete the final comma by hand.
 
J

Jay Freedman

Assuming the words are separated by paragraph marks (¶) and not manual
line breaks, this macro will do the job.

Sub AddQuoteComma()
Dim oRg As Range
Set oRg = ActiveDocument.Range

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(<[!^13]{1,})^13"
.Replacement.Text = "'\1',^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With

'remove final comma and paragraph mark
Set oRg = ActiveDocument.Range
With oRg
.Collapse wdCollapseEnd
.MoveStartUntil cset:=",", Count:=wdBackward
.MoveStart unit:=wdCharacter, Count:=-1
.Delete
End With

Set oRg = Nothing
End Sub
 
P

Poster

Jay Freedman wrote in message ...
Assuming the words are separated by paragraph marks (¶) and not manual
line breaks, this macro will do the job.

Sub AddQuoteComma()
Dim oRg As Range
Set oRg = ActiveDocument.Range

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(<[!^13]{1,})^13"
.Replacement.Text = "'\1',^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With

'remove final comma and paragraph mark
Set oRg = ActiveDocument.Range
With oRg
.Collapse wdCollapseEnd
.MoveStartUntil cset:=",", Count:=wdBackward
.MoveStart unit:=wdCharacter, Count:=-1
.Delete
End With

Set oRg = Nothing
End Sub

Wow, very impressive. Thank you.
 
P

Poster

The only remaining problem is that when the resulting text is pasted into a
Query Analyzer session, Query Analyzer doesn't recognise the apostrophes and
so an error occurs when trying to run an SQL query. It seems that an
apostrophe in Word is different to an apostrophe in Query Analyzer. Would
any of you Microsoft guys ever have encountered this problem before?


Jay Freedman wrote in message ...
Assuming the words are separated by paragraph marks (¶) and not manual
line breaks, this macro will do the job.

Sub AddQuoteComma()
Dim oRg As Range
Set oRg = ActiveDocument.Range

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(<[!^13]{1,})^13"
.Replacement.Text = "'\1',^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With

'remove final comma and paragraph mark
Set oRg = ActiveDocument.Range
With oRg
.Collapse wdCollapseEnd
.MoveStartUntil cset:=",", Count:=wdBackward
.MoveStart unit:=wdCharacter, Count:=-1
.Delete
End With

Set oRg = Nothing
End Sub


Poster said:
Dudes,

I would like to have a Word macro that can wrap quotes around each line of
selected text, followed by a comma at the end of each line. Preferably it
won't append a comma to the last line, but I'll settle for a solution
without this extra feature.

eg.

Dog
Cat
Fish
Bird

becomes

'Dog',
'Cat',
'Fish',
'Bird'


The reason I need this is to allow me to save a lot of time where I have to
manually create an SQL query that includes a long list of search criteria.

Thanks. The first to provide a solution goes to the top of the class!

Poster
 
J

Jay Freedman

Hi Poster,

Do your queries contain "curly quotes" that curve in opposite directions at
the start and end of each word? If so, that's why Query Analyzer doesn't
recognize them -- they're actually different characters than the "straight
quote"/apostrophe.

To fix it up, first go to Tools > AutoCorrect > AutoFormat As You Type.
Clear the option to replace "straight quotes" with "smart quotes". (Only MS
thinks they're smart!)

Now open the Replace dialog. Type one apostrophe in both the Find What box
and the Replace With box, and click the Replace All button. All the curly
quotes of both varieties will be replaced with straight quotes. Try the
result in Query Analyzer.

--
Regards,
Jay Freedman
Microsoft Word MVP
The only remaining problem is that when the resulting text is pasted
into a Query Analyzer session, Query Analyzer doesn't recognise the
apostrophes and so an error occurs when trying to run an SQL query.
It seems that an apostrophe in Word is different to an apostrophe in
Query Analyzer. Would any of you Microsoft guys ever have encountered
this problem before?


Jay Freedman wrote in message ...
Assuming the words are separated by paragraph marks (¶) and not
manual line breaks, this macro will do the job.

Sub AddQuoteComma()
Dim oRg As Range
Set oRg = ActiveDocument.Range

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(<[!^13]{1,})^13"
.Replacement.Text = "'\1',^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With

'remove final comma and paragraph mark
Set oRg = ActiveDocument.Range
With oRg
.Collapse wdCollapseEnd
.MoveStartUntil cset:=",", Count:=wdBackward
.MoveStart unit:=wdCharacter, Count:=-1
.Delete
End With

Set oRg = Nothing
End Sub


Poster said:
Dudes,

I would like to have a Word macro that can wrap quotes around each
line of selected text, followed by a comma at the end of each line.
Preferably it won't append a comma to the last line, but I'll
settle for a solution without this extra feature.

eg.

Dog
Cat
Fish
Bird

becomes

'Dog',
'Cat',
'Fish',
'Bird'


The reason I need this is to allow me to save a lot of time where I
have to manually create an SQL query that includes a long list of
search criteria.

Thanks. The first to provide a solution goes to the top of the
class!

Poster
 

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