Limit of Search String Reached

C

Carlos Chalhoub

Hi listmates,

I am using an application called "Robohelp for Word" to generate a Webhelp
system. During the generation, Robohelp transforms Word documents into HTML
files. The problem is that the HTML code generated is unconventional, and we
need to provide a clean code to our clients. So I was asked to write a
search and replace macro that will scan more than 3000 files and make the
appropriate adjustments.

Problem:
Sometimes the search string reaches beyond the 255 characters limit and I am
not sure how to make it accept more. I read somewhere that I need to use the
"InStr" function, but I don't know how.
Thanks for any help.
Carlos

-------------- START OF CODE-----------------------
Sub FixHTMLCode()

' Macro created by Carlos Chalhoub
' This macro fixes HTML code in all files in a directory

Dim strFolderName As String

' Specify the location of the folder
strFolderName = BrowseFolder("Specify the path to the folder containing the
files.")
If Right(strFolderName, 1) <> "\" Then
strFolderName = strFolderName & "\"
End If

With Application.FileSearch
.FileName = "*.htm"
.LookIn = strFolderName
.SearchSubFolders = False
.FileType = msoFileTypeAllFiles

If .Execute <> 0 Then 'start search, and if any files found then
proceed...
For i = 1 To .FoundFiles.Count 'loop through all files in folder
Documents.Open FileName:=.FoundFiles(i), ConfirmConversions:=False,
Format:=wdOpenFormatText
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = _
"(\<P class=""List"" \>\<FONT style=""font-family:'Symbol';
font-size:10pt; "" \>·\</FONT\>\<span style=""font-size:8pt;
font-family='Times New Roman'; letter-spacing=0pt;
font-weight=normal;""\>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</sp
an\>)(*)(\</p\>)"
.Replacement.Text = _
"<ul { list-style: outside; margin-bottom:3.00pt }
<li>\2</li></ul>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
ActiveDocument.Close SaveChanges:=wdSaveChanges
Next i
Else: Exit Sub
MsgBox "There were no files found."
End If
End With
End Sub
-----------------------------END OF CODE-------------------------------
 
J

Jay Freedman

Hi, Carlos,

There is no way to get Word to search for a string longer than 255
characters, and InStr has nothing to do with it.

My first recommendation would be to use a different tool, such as Perl or
PHP, that doesn't have Word's limitations. In the end, I think you would
spend less total time learning enough Perl to write the needed script than
you would spend trying to force Word to do the job.

If you must use Word, the strategy you'll have to use goes something like
this. I assume you have a list of the codes you want to replace. For each
code, first find its length by using the Len() function. If that's less than
or equal to 255 characters, do an ordinary find/replace. If it's longer than
255, split it into two strings -- one holding the first 255, the other
holding the rest. Do a find (not a replace; that is, don't assign a value to
..Replacement.Text) for the first string. If it's found, then set a Range
object whose .Start is at the end of the found text, and whose length is the
length of the second string. Compare the contents of that range to the the
contents of the second string. If they're equal, extend the range back 255
characters (so it covers the entire original code) and set its .Text to the
replacement string.

I also strongly advise not using the Selection.Find object. Instead, declare
a Range object, set it to ActiveDocument.Range, and use that object's .Find
to do the search. This avoids moving the Selection through the document,
which means the screen doesn't need to be scrolled or redrawn, and can
vastly speed up execution. When you're searching 3000+ documents, that's
important.
 

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