better search mode

M

Martin

Dear experts,

is it possible to search a string in a file WITHOUT activating it, for
example using the fso-facility
Dim RefList As document

Set RefList = Documents.Open(FileName = FileName, Format:=wdOpenFormatText)
Set ContentRange = RefList.Content
StatusBar = "Searching " & sSignature
With ContentRange.Find

etc. (The example above does not work.) It would ease the operating process
of a program, if it had not to switch between the windows constantly.

Thank you
Martin
 
J

Jean-Guy Marcil

Martin was telling us:
Martin nous racontait que :
Dear experts,

is it possible to search a string in a file WITHOUT activating it, for
example using the fso-facility
Dim RefList As document

Set RefList = Documents.Open(FileName = FileName,
Format:=wdOpenFormatText) Set ContentRange = RefList.Content
StatusBar = "Searching " & sSignature
With ContentRange.Find

etc. (The example above does not work.) It would ease the operating

It would be helpful if you told us how it "does not work"
Sometimes, our expectations are too high!
process of a program, if it had not to switch between the windows
constantly.

This seems to work:

Dim myFileName As String
Dim RefList As Document
Dim ContentRange As Range
Dim sSignature As String

myFileName = "C:\Test Documents\test.doc"
sSignature = "New Name"

Set RefList = Documents.Open(FileName:=myFileName, Format:=wdOpenFormatText)
Set ContentRange = RefList.Content
StatusBar = "Searching " & sSignature
With ContentRange.Find
etc.


I see that you had
Open(FileName = FileName

two problems (the second one is really minor):
1) It should be: Open(FileName:= FileName
(notice the extra ":")
2) It is not a good idea to name a variable with an expression that is
already used tin the Word Object library...

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

Martin

I have simply written a reduced version of my program as follows:

Sub TestSearchMode()

Dim myFileName As String
Dim RefList As Document
Dim ContentRange As Range
Dim sSignature As String

myFileName = "H:\M.Mueller\vol III35D4\NewPrintVersion\05_File_tagged.doc"
sSignature = "93Ito"
Set RefList = Documents.Open(FileName:=myFileName,
Format:=wdOpenFormatText)
Set ContentRange = RefList.Content
StatusBar = "Searching " & sSignature
With ContentRange.Find
.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.text = sSignature
If .Found Then
MsgBox ContentRange.text
End If
End With
RefList.Close
End Sub

I want to figure out, if I can develop a module, that performs a search 'in
the background', i.e. without opening or activating the respective document;
I thought, that the 'file system objects' might help, but I do not know how
to handle them.

The program above opens the file and closes it without error messages, but
it does not find the 'sSignature', although it is present in it.

Best regards
Martin
 
J

Jean-Guy Marcil

Martin was telling us:
Martin nous racontait que :
I have simply written a reduced version of my program as follows:

Sub TestSearchMode()

Dim myFileName As String
Dim RefList As Document
Dim ContentRange As Range
Dim sSignature As String

myFileName = "H:\M.Mueller\vol
III35D4\NewPrintVersion\05_File_tagged.doc" sSignature = "93Ito"
Set RefList = Documents.Open(FileName:=myFileName,
Format:=wdOpenFormatText)
Set ContentRange = RefList.Content
StatusBar = "Searching " & sSignature
With ContentRange.Find
.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.text = sSignature
If .Found Then
MsgBox ContentRange.text
End If
End With
RefList.Close
End Sub

I want to figure out, if I can develop a module, that performs a
search 'in the background', i.e. without opening or activating the
respective document; I thought, that the 'file system objects' might
help, but I do not know how to handle them.

You must open it, but with later version of Office, you can open the
document Invisible (one of the Open parameters)
The program above opens the file and closes it without error
messages, but it does not find the 'sSignature', although it is
present in it.


You forgot an Execute statement in your search code:
.text = sSignature
.Execute
If .Found Then
MsgBox ContentRange.text
End If

Why do you open the file in text format?


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

Martin

In the following test-example, where I use the invisible opening of a file, I
do not understand, why the subsequent function 'TaggedStringBehind' does not
work, although it does work, when I open the file visibly.
MsgBox TextRange.Text always deliveres the right output.
The public function 'TaggedStringBehind' returnes a formatted string as a
tagged string, i.e. subscripts/superscripts are replaced by <sub>char</sub>,
<sup>char</sup> etc.
Best regards
Martin

Sub Test()

Dim TextRange As Range
Dim StructureList As Document
Const DataFileName As String = "E:\vba-workspace\Test2.doc"

Set StructureList = Documents.Open(FileName:=DataFileName, Visible:=False)
Set TextRange = StructureList.Tables(1).Rows(1).Cells(1).Range
TextRange.SetRange Start:=TextRange.Start, End:=TextRange.End - 1
MsgBox TextRange.text
MsgBox TaggedStringBehind(TextRange)
StructureList.Close
End Sub
 
J

Jean-Guy Marcil

Martin was telling us:
Martin nous racontait que :
In the following test-example, where I use the invisible opening of a
file, I do not understand, why the subsequent function
'TaggedStringBehind' does not work, although it does work, when I
open the file visibly.
MsgBox TextRange.Text always deliveres the right output.
The public function 'TaggedStringBehind' returnes a formatted string
as a tagged string, i.e. subscripts/superscripts are replaced by
<sub>char</sub>, <sup>char</sup> etc.
Best regards
Martin


Not sure, but, maybe that
MsgBox TextRange.text
works because you are explicitly using the string property of the range
object,
but, in
MsgBox TaggedStringBehind(TextRange)
you are manipulating a string derived from a range object, but you are not
explicitly telling the compiler to use the string which is "invisible", so
it cannot extrapolate for you the string you want.
Have you tried with
MsgBox TaggedStringBehind(TextRange.text)
???

Flash "Déjà vu":

When you write:
file, I do not understand, why the subsequent function
'TaggedStringBehind' does not work
what do you mean by "does not work"?? What is actually happening? Error
message? Undesired results? No results, but no error either? etc.

You have to be more explicit...
;-)


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

Martin

Thanks for the answer.
By the 'MsgBox' commands I try make visible, what is in the variable.
When I open the Test-file visibly, the commands deliver the right results,
as I said.
When I open the Test-file invisibly, the first command delivers the right
result, the second one a wrong one, that means, it delivers the result (i.e.
tagged string) of the function of the PRESENTLY ACTIVATED file, not the
invisibly opened one.

Using TextRange.text delivers an error, because the function expects a range
as a parameter, not a string. But I have to use a range as parameter, because
passing a string to a subroutine means the immediate removal of all
formattings of that string, in particular the sub/superscript font, which is
just what I want to preserve.

Martin
 
J

Jean-Guy Marcil

Martin was telling us:
Martin nous racontait que :
Thanks for the answer.
By the 'MsgBox' commands I try make visible, what is in the variable.
When I open the Test-file visibly, the commands deliver the right
results, as I said.
When I open the Test-file invisibly, the first command delivers the
right result, the second one a wrong one, that means, it delivers the
result (i.e. tagged string) of the function of the PRESENTLY
ACTIVATED file, not the invisibly opened one.

This probably means that there is something not quite right with your
function.

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

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