VBA search for text string in non-Word doc?

E

Ed

I am using TextStream to create documents. I also need to grab several text
strings from the documents. At the moment, I create and save the doc with
TextStream, then reopen it with Word and do my searches. This adds a
monumental amount of time to running this program. Is it possible to search
it with Word VBA as a non-Word document as it's created and while it's still
open?

Code to create doc:
Set docTIR = fs.CreateTextFile(strNewDoc)
docTIR.Write (strNewTIR)
docTIR.Close

Ed
 
J

Jay Freedman

Ed said:
I am using TextStream to create documents. I also need to grab
several text strings from the documents. At the moment, I create and
save the doc with TextStream, then reopen it with Word and do my
searches. This adds a monumental amount of time to running this
program. Is it possible to search it with Word VBA as a non-Word
document as it's created and while it's still open?

Code to create doc:
Set docTIR = fs.CreateTextFile(strNewDoc)
docTIR.Write (strNewTIR)
docTIR.Close

Ed

Hi Ed,

You don't say where you're getting the content of strNewTIR that you're
writing into the file, but you should be able to use the InStr function on
strNewTIR to find substrings. Can you describe more explicitly what you're
trying to do?
 
E

Ed

Thanks for replying, Jay. Sorry I wasn't more specific - I should know by
now!

Right now, I'm scanning through each created document for certain text
strings as "labels" for information to populate an Excel file as a document
index. For instance, I will search each doc for "3. TITLE: "; when found,
the range can then be collapsed and reset either left or right to encompass
the doc title, which is captured as a string and written to the Excel file.

It works, but only if I close each TextStream doc and reopen as a Word
doc, search, and close. That eats up a lot of time, and I'd like to make it
faster. I do hundreds, often thousands, of docs like this.

Ed
 
H

Helmut Weber

Hi Ed,
as far as I understand this, you don't process word files
at all, but text-files. All you need is to make use of the string
functions availabe in any programming language. You got
delimiters, such as chr(13), which split the text-string up
into discrete parts. If such a part contains "3. TITLE: ",
then you may put that part into a database or an Excel-Sheet,
or process it further beforehand. I think you have to read
character by character, store what's coming in into a string,
until a delimiter is found, process the result, and do with it
whatever you like, depending on the processing.
Certainly easier said than done.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
E

Ed

Hi, Helmut. The process actually involves taking a very long text file,
finding a certain portion and saving it to a string, then opening a new
file and writing the string to it. From this, I need to extract some
internal
text as strings to populate and Excel file which is used as an index. I'm
trying to get around closing and reopening the file.

Ed
 
H

Helmut Weber

Hi Ed,
in principle it could work like this:
Public Sub Test601()
Dim sInp As String
Dim sTmp As String
Open "c:\test\textfile.txt" For Input As #1
'---
While Not EOF(1)
sInp = Input(1, #1) ' 1 character
If sInp <> Chr(13) Then
sTmp = sTmp & sInp
Else
If InStr(sTmp, ". TITLE: ") Then
MsgBox "got you : " & sTmp ' !!!
End If
sTmp = ""
End If
Wend
'---
Close #1
End Sub
But a lot depends on the structure of the textfile.
It could be, theoretically, that there is no chr(13)
in it. Or no other character other than a-z, 0-9 etc.
Then it would not have a structure at all, or at
least a structure, that would be difficult to accesss.
If the file is structured by lines, then you may
read line by line and not character by character,
as I did here, which is by far the slowest way.
 

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