Find a paragraph that is also in autotext

L

Lesley Regan

After searching, browsing, finding out all kinds of things about autotext, I
cannot tell if I can do what I'm about to ask about.

If somebody could help with syntax or tell me it can't be done, I'll either
start coding or stop trying to find out!

I am running XP.

I have a long paragraph that is supposed to be included in every letter we
create. I'd like to program a search for the paragraph (in documents being
reused) and then either insert the paragraph if it's not found, or not insert
it, if it's already in the document.

It makes sense to have a paragraph available in a globally accessible
template as Autotext with a unique name of 2 or 3 words, like
autoLetterInsert.

OK, now, I found code that allows me to INSERT autotext, and code that lets
me set the value of an autotext entry, and similar code, but can I put
together Find with Autotext.Value? (pseudocode):

Something like:
clearformatting....
..
..

FindText = AutoTextEntries(autoLetterInsert).Value


If somebody could help I'd appreciate it.

Thank you, merci, gratiam tibi,
Regards, Lesley Regan
 
J

Jean-Guy Marcil

Lesley Regan was telling us:
Lesley Regan nous racontait que :
After searching, browsing, finding out all kinds of things about
autotext, I cannot tell if I can do what I'm about to ask about.

If somebody could help with syntax or tell me it can't be done, I'll
either start coding or stop trying to find out!

I am running XP.

I have a long paragraph that is supposed to be included in every
letter we create. I'd like to program a search for the paragraph (in
documents being reused) and then either insert the paragraph if it's
not found, or not insert it, if it's already in the document.

It makes sense to have a paragraph available in a globally accessible
template as Autotext with a unique name of 2 or 3 words, like
autoLetterInsert.

OK, now, I found code that allows me to INSERT autotext, and code
that lets me set the value of an autotext entry, and similar code,
but can I put together Find with Autotext.Value? (pseudocode):

Something like:
clearformatting....
.
.

FindText = AutoTextEntries(autoLetterInsert).Value


If somebody could help I'd appreciate it.

Thank you, merci, gratiam tibi,
Regards, Lesley Regan

If the documents are not too long, you could dump the document text in a
string variable, dump the paragraph text in another string variable, finally
check if variable 2 is in variable 1 with InStr.

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

Helmut Weber

Hi Lesley,

won't work anyway, as the length of the search string is limited to
255 characters anyway, whereby I assume, that your long paragraphs are
longer than that.

For searching for longer strings, see:

http://gregmaxey.mvps.org/Find_Long_String.htm

If the docs are rather small, you may rather use
something like (untested):
dim oPrg as paragraph
for each prg in activedocument.range.paragraphs
if oprg.range.text = AutoTextEntries(autoLetterInsert).Value then
next


Greetings from Bavaria, Germany

Helmut Weber, MVP, WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
L

Lesley Regan

--
Regards, Lesley



Helmut Weber said:
Hi Lesley,

won't work anyway, as the length of the search string is limited to
255 characters anyway, whereby I assume, that your long paragraphs are
longer than that.

For searching for longer strings, see:

http://gregmaxey.mvps.org/Find_Long_String.htm

If the docs are rather small, you may rather use
something like (untested):
dim oPrg as paragraph
for each prg in activedocument.range.paragraphs
if oprg.range.text = AutoTextEntries(autoLetterInsert).Value then
next


Greetings from Bavaria, Germany

Helmut Weber, MVP, WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Ah, Helmut, I like this idea. I shall try it. (Yes, I did know the
limitation of the search string, but somehow hoped it didn't apply to
autotext.)

Thank you for your idea and explanation,

Lesley
 
L

Lesley Regan

--
Regards, Lesley



Jean-Guy Marcil said:
Lesley Regan was telling us:
Lesley Regan nous racontait que :


If the documents are not too long, you could dump the document text in a
string variable, dump the paragraph text in another string variable, finally
check if variable 2 is in variable 1 with InStr.

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

Bonjour, Jean-Guy,

Thank you for your suggested solution. I haven't done Word programming
long enough to think of a string comparison in this case, but Instr seems to
be a good compromise. I'm comfortable with strings. I can't imagine that
these letters are long enough to be classified as "long documents", but one
can never be sure. I'll do some timing tests.

Cheers!

Lesley
 
J

Jean-Guy Marcil

Lesley Regan was telling us:
Lesley Regan nous racontait que :

Bonjour, Jean-Guy,

Thank you for your suggested solution. I haven't done Word
programming long enough to think of a string comparison in this case,
but Instr seems to be a good compromise. I'm comfortable with
strings. I can't imagine that these letters are long enough to be
classified as "long documents", but one can never be sure. I'll do
some timing tests.

I think that any string comparison will be way faster than most collection
iteration. (As in: For each para in ActiveDocument... )

'_______________________________________
Dim docContentStr As String
Dim paraStr As String

paraStr = "The quick brown fox jumps over the lazy dog. " _
& "The quick brown fox jumps over the lazy dog. " _
& "The quick brown fox jumps over the lazy dog. " _
& "The quick brown fox jumps over the lazy dog. " _
& "The quick brown fox jumps over the lazy dog. " _
& "The quick brown fox jumps over the lazy dog. " _
& "The quick brown fox jumps over the lazy dog. " _
& "The quick brown fox jumps over the lazy dog. " _
& "The quick brown fox jumps over the lazy dog. " _
& "The quick brown fox jumps over the lazy dog."
'Or
'paraStr = NormalTemplate.AutoTextEntries("autoLetterInsert").Value
'If the autotext is in Normal.dot

docContentStr = ActiveDocument.Range.Text

If InStr(1, docContentStr, paraStr) > 0 Then
MsgBox "String found!"
Else
MsgBox "String not found!"
End If
'_______________________________________

I tried the above with a 173-page document containing over 24,000 characters
and over 7,500 paragraphs. The result was nearly instantaneous.

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

Lesley Regan

Jean-Guy Marcil said:
Lesley Regan was telling us:
Lesley Regan nous racontait que :



I think that any string comparison will be way faster than most collection
iteration. (As in: For each para in ActiveDocument... )

'_______________________________________
Dim docContentStr As String
Dim paraStr As String

paraStr = "The quick brown fox jumps over the lazy dog. " _
& "The quick brown fox jumps over the lazy dog. " _
& "The quick brown fox jumps over the lazy dog. " _
& "The quick brown fox jumps over the lazy dog. " _
& "The quick brown fox jumps over the lazy dog. " _
& "The quick brown fox jumps over the lazy dog. " _
& "The quick brown fox jumps over the lazy dog. " _
& "The quick brown fox jumps over the lazy dog. " _
& "The quick brown fox jumps over the lazy dog. " _
& "The quick brown fox jumps over the lazy dog."
'Or
'paraStr = NormalTemplate.AutoTextEntries("autoLetterInsert").Value
'If the autotext is in Normal.dot

docContentStr = ActiveDocument.Range.Text

If InStr(1, docContentStr, paraStr) > 0 Then
MsgBox "String found!"
Else
MsgBox "String not found!"
End If
'_______________________________________

I tried the above with a 173-page document containing over 24,000 characters
and over 7,500 paragraphs. The result was nearly instantaneous.

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

I am so impressed, and so grateful. Thank you very much!

lr
 
L

Lesley Regan

Lesley Regan said:
I am so impressed, and so grateful. Thank you very much!

lr

P.S. I think I need to practice my "for each" loops though, so I'm going to
try that way as an exercise.

keep cool, everybody,

lesley
 

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