Find String, Copy/Paste Next 255+ Characters to New .doc

R

ryguy7272

Does anyone know of a way to search for a certain string, such as
“Additional†and then copy/paste this into a new document, along with the
following 255 characters? Is 255 the limit? I found the code below on this
DG:

Sub RTM4()
Dim oDoc As Document
Dim oSrcRg As Range, oDestRg As Range

Set oSrcRg = ActiveDocument.Range
Set oDoc = Documents.Add
Set oDestRg = oDoc.Range
oDestRg.Collapse wdCollapseEnd

With oSrcRg.Find
..ClearFormatting
..Text = "Additional ?{255}"
..Forward = True
..Wrap = wdFindStop
..Format = False
..MatchCase = False
..MatchWholeWord = False
..MatchAllWordForms = False
..MatchSoundsLike = False
..MatchWildcards = True

Do While .Execute
oDestRg.FormattedText = oSrcRg.FormattedText
oDestRg.Collapse wdCollapseEnd
oDestRg.InsertParagraph
oDestRg.Collapse wdCollapseEnd
Loop
End With

Set oDestRg = oDoc.Range
oDestRg.ConvertToTable Separator:=wdSeparateByTabs, _
NumColumns:=3, NumRows:=100, _
AutoFitBehavior:=wdAutoFitFixed
With oDoc.Tables(1)
..Style = "Table Grid"
..ApplyStyleHeadingRows = True
..ApplyStyleLastRow = True
..ApplyStyleFirstColumn = True
..ApplyStyleLastColumn = True
End With

oDoc.Activate

Set oSrcRg = Nothing
Set oDestRg = Nothing
Set oDoc = Nothing
End Sub


It works great. Sometimes the string, such as “Additional†is in a table,
and the code seems to struggle with the tables sometimes. For instance, if I
have the word “Additional†in one table, I'd like to find 255 characters, or
more, up to 500 characters, in an adjacent table, just to the right on this
table. Any suggestions?

Regards,
Ryan
 
R

Russ

Ryan,
In regards to find string in a table across cells, you could temporarily
convert the table range to text, use your find your search criteria- copy
and paste loop, and change table range back to table. Since you are not
altering the table, it should survive a change to text and back to table
without incident.

In the find text box there might be a limit of 255 characters that you can
find **literally**, but if you use the find textbox while using the
wildcards function you effectively get passed finding only 255 characters at
the most. You must come up with a unique find start and end pattern.
http://www.gmayor.com/replace_using_wildcards.htm


You are being too vague on the ending delimiter of your search range. You
can find a certain pattern then use the .extend or .expand method while in
the found loop. But while using VBA code you need to come up with a unique
ending pattern or one certain character count limit or limit it to a certain
amount of words, cells, paragraphs, sections, etc. after the initial
starting found text. (Unless you allow user input while running the macro to
change the starting and ending criteria on the fly.)
 
R

ryguy7272

Thanks for the look Russ. I’m intrigued, but I don’t know how to implement
what you suggested. Do you know how to approach this task? I seriously
doubt the macro recorder will create me the code that gives me what I need.

Now I’m lost.


Any additional help would be greatly appreciated.
Ryan--
 
R

Russ

Ryan,
As I said, you need to determine unique patterns to search for.

The macro recorder can help get you started sometimes:
I.e., It can show you the VBA code used to convert a table to text and then
back from text to table.
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm

Help on using wildcards:
http://www.word.mvps.org/FAQs/General/UsingWildcards.htm
http://www.word.mvps.org/FAQs/General/FindingSpecialCharacters.htm
http://www.gmayor.com/replace_using_wildcards.htm

Example of find then do something besides standard replacement of found
text:
http://word.mvps.org/faqs/macrosvba/ReplceTextWithDoc.htm

After reading the above links and other pages they may branch off to, If you
have a specific question, then by all means, post another message. But at
least tell us what you have tried and didn't get to work.
 

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