Find Specific Text

S

singeredel

I need to write programming to find a specific block of text in a document
consisting of multiple paragraphs (apprx. 5) starting with the heading
"Notice" (which is formatted with a style) and ending with "Signed in County
on -_____________________-." After finding the entire block of text, I want
to delete the text temporarily so that I can run a Word Add-In line-counting
program to count lines without this specific text included and then undelete
the text or not save it again after deleting the text. Any help as to how to
go about this would be appreciated!

Thanks...
 
S

singeredel

For reasons too complicated to go into in this forum, I cannot insert an
autotext entry. Briefly, the files are already complete and saved before any
line counting takes place because certain information has to be inserted
within the "Notice" paragraphs and saved in the file. Somehow I need to be
able to search for the beginning text and ending text and not be concerned
with what appears within those boundaries, because some of this text can be
different within each document.
 
J

Jean-Guy Marcil

singeredel was telling us:
singeredel nous racontait que :
I need to write programming to find a specific block of text in a
document consisting of multiple paragraphs (apprx. 5) starting with
the heading "Notice" (which is formatted with a style) and ending
with "Signed in County on -_____________________-." After finding
the entire block of text, I want to delete the text temporarily so
that I can run a Word Add-In line-counting program to count lines
without this specific text included and then undelete the text or not
save it again after deleting the text. Any help as to how to go about
this would be appreciated!

Try this to get you started:

'_______________________________________
Sub Main()

Dim curRge As Range
Dim startRgeLg As Long
Dim endRgeLg As Long
Dim blockRge As Range

'Save initial selection
Set curRge = Selection.Range
Selection.HomeKey wdStory

'Find beginning of range
ClearFindAndReplaceParameters

With Selection.Find
.Text = "Notice"
.Style = ActiveDocument.Styles("Notice Style")
.Format = True
.Execute
End With

startRgeLg = Selection.Range.Start

'Find end of range
ClearFindAndReplaceParameters

With Selection.Find
.Text = "Signed in * on"
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute
End With

Selection.Paragraphs(1).Range.Select
endRgeLg = Selection.Range.End

Set blockRge = ActiveDocument.Range(startRgeLg, endRgeLg)
blockRge.Delete

'Call your add-in

'Make sure the add-in did not add action to the undo list,
'if it did, see if that number is consitent,
'if not, another approach will be necessary.

ActiveDocument.Undo 1

'Reset everything the way it was
ClearFindAndReplaceParameters
curRge.Select

End Sub
'_______________________________________

'_______________________________________
Sub ClearFindAndReplaceParameters()

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

End Sub
'_______________________________________

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

singeredel

Thank you so much for your valuable input. All of your code worked perfectly.
The only problem I have is with calling the Add-In program. I used the
following code in the spot indicated in your instructions:

Application.Run MacroName:="Abacus.Counter.AbacusCountLines"

When I run this code, it brings up the dialogue box for running the word
counting program, but the calling program finishes executing after the
dialogue box appears to run the counting program (before I can do the line
counting), and therefore the deleted text has already been put back into the
document.

Do I need to do something different in the code when I call up the word
counting program so that I can manually execute the word counting program
before the rest of the calling program completes? (By the way, the code for
deleting the text is in a different Project than the line-counting program,
which is in a separate Project of it's own, both of which do not reside in
the Normal template.

Thanks!
 
J

Jean-Guy Marcil

singeredel was telling us:
singeredel nous racontait que :
Thank you so much for your valuable input. All of your code worked
perfectly. The only problem I have is with calling the Add-In
program. I used the following code in the spot indicated in your
instructions:

Application.Run MacroName:="Abacus.Counter.AbacusCountLines"

When I run this code, it brings up the dialogue box for running the
word counting program, but the calling program finishes executing
after the dialogue box appears to run the counting program (before I
can do the line counting), and therefore the deleted text has already
been put back into the document.

Do I need to do something different in the code when I call up the
word counting program so that I can manually execute the word
counting program before the rest of the calling program completes?
(By the way, the code for deleting the text is in a different Project
than the line-counting program, which is in a separate Project of
it's own, both of which do not reside in the Normal template.

It could be because the Abacus add-in is not coded properly...Without
knowing what goes on in that code, it is hard to tell...
Is there a modeless dialog box?

Have you tried with a
DoEvents
after the
Application.Run MacroName:="Abacus.Counter.AbacusCountLines"
line?

Also, do you need an Add-in to count the lines in the document?
ActiveDocument.ComputeStatistics(wdStatisticLines)
counts the line as well... no add-in...

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

singeredel

Thanks for your reply. It is definitely not possible for me to use the
application-generated statistics for my line counting, so I do have to use
this add-in program for billing purposes.

I do not know what you mean by a "modeless dialog box" or its implications
and do not know how to write code for DoEvents (sorry, just a layperson here
trying to make things work!)
 
J

Jean-Guy Marcil

singeredel was telling us:
singeredel nous racontait que :
Thanks for your reply. It is definitely not possible for me to use the
application-generated statistics for my line counting, so I do have
to use this add-in program for billing purposes.

I do not know what you mean by a "modeless dialog box" or its

A dialog box that does not hold total control. You can click on another
window and keep the dialog box in he background. Like the Search/Replace
window in Word.
implications and do not know how to write code for DoEvents (sorry,
just a layperson here trying to make things work!)

The code is "DoEvents"
Try adding that line as I suggested, sometimes it helps.

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

singeredel

Thanks so much for your help so far!

The add-in program seems to allow me to click in the document and edit,
etc., although the dialogue box does not go to the background out of sight,
so perhaps this is modeless? If so, I do not know what this means to the
programming.

Also, I did try typing "DoEvents" after the line calling the line counting
macro, but it did not seem to make any difference. I did notice, however,
that when stepping through the program code the stepping did not continue
after the line calling the line counting macro. I have noticed this before
when trying to use the "Application.Run MacroName" code. I can never finish
stepping through the rest of the code after putting in this type of line.
Don't know if this is of significance or not or whether there is some other
way to activate the line counting program.
 
J

Jean-Guy Marcil

singeredel was telling us:
singeredel nous racontait que :
Thanks so much for your help so far!

The add-in program seems to allow me to click in the document and
edit, etc., although the dialogue box does not go to the background
out of sight, so perhaps this is modeless? If so, I do not know what
this means to the programming.

Yes, if the dialog box stays on screen and also allows you to interact with
other windows, such as the document window, then it is modeless.

When a modeless form is displayed, the rest of the code executes while the
form is displayed. That is the purpose of modeless forms.

The few times I have worked with modeless forms, I created them for a
specific purpose and I was in control of the whole thing. In this case, you
do not have access to the code behind the form itself... so, I think, that
complicates things a bit.
Also, I did try typing "DoEvents" after the line calling the line
counting macro, but it did not seem to make any difference. I did

No, in this case it would not help.

Try this:

Instead of the DoEvents line I suggested earlier, insert this code right
after the call to the Add-in macro that counts line:

'_______________________________________
Dim nrUserForms As Long
nrUserForms = UserForms.Count

Do While nrUserForms = 1
If UserForms(0).Caption = "myFormOK" Then
Pause 0.5
End If
nrUserForms = UserForms.Count
Loop
'_______________________________________

The longer the number after Pause, the longer the delay that might occur
between the time the user closes the dialog and when the rest of your code
restarts executing.
Also, change "myFormOK" for the dialog title (The title you see in the title
bar of the line courting add-in dialog box).
Finally, make sure there are no other dialog box running.... and I hope that
the people who created the line courting add-in wrote clean code, otherwise
you might have problems if you run the code a few times back to back...
<Crossing my fingers for you!>

Then add this sub at the end of your module:
'_______________________________________
Sub Pause(ByVal fSeconds As Single, _
Optional ByVal AllowEvents As Boolean = True)
' By Frank Carr
' Pause execution for specified # of seconds [fSeconds]
'
Dim fTimer As Single ' intial timer value
fTimer = Timer
Do While Timer - fTimer < fSeconds
If AllowEvents Then
DoEvents
End If
'
' if we cross midnight, back up one day
'
If Timer < fTimer Then
fTimer = fTimer - 86400 'one day in seconds 24*60*60
End If
Loop
End Sub
'_______________________________________

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

singeredel

Thanks again for responding. Unfortunately, the program code continues to run
after the dialogue box for the line-counting program is activated and before
I can actually execute the dialogue box, so the deleted paragraph reappears
in the document before I can run the line-count program.

I have tried to step through my program code but, as mentioned before, it
will not continue to step through any code after the "Application.Run
MacroName" code, although the code appears to run because the deleted text
reappears in the document.
--
singeredel


Jean-Guy Marcil said:
singeredel was telling us:
singeredel nous racontait que :
Thanks so much for your help so far!

The add-in program seems to allow me to click in the document and
edit, etc., although the dialogue box does not go to the background
out of sight, so perhaps this is modeless? If so, I do not know what
this means to the programming.

Yes, if the dialog box stays on screen and also allows you to interact with
other windows, such as the document window, then it is modeless.

When a modeless form is displayed, the rest of the code executes while the
form is displayed. That is the purpose of modeless forms.

The few times I have worked with modeless forms, I created them for a
specific purpose and I was in control of the whole thing. In this case, you
do not have access to the code behind the form itself... so, I think, that
complicates things a bit.
Also, I did try typing "DoEvents" after the line calling the line
counting macro, but it did not seem to make any difference. I did

No, in this case it would not help.

Try this:

Instead of the DoEvents line I suggested earlier, insert this code right
after the call to the Add-in macro that counts line:

'_______________________________________
Dim nrUserForms As Long
nrUserForms = UserForms.Count

Do While nrUserForms = 1
If UserForms(0).Caption = "myFormOK" Then
Pause 0.5
End If
nrUserForms = UserForms.Count
Loop
'_______________________________________

The longer the number after Pause, the longer the delay that might occur
between the time the user closes the dialog and when the rest of your code
restarts executing.
Also, change "myFormOK" for the dialog title (The title you see in the title
bar of the line courting add-in dialog box).
Finally, make sure there are no other dialog box running.... and I hope that
the people who created the line courting add-in wrote clean code, otherwise
you might have problems if you run the code a few times back to back...
<Crossing my fingers for you!>

Then add this sub at the end of your module:
'_______________________________________
Sub Pause(ByVal fSeconds As Single, _
Optional ByVal AllowEvents As Boolean = True)
' By Frank Carr
' Pause execution for specified # of seconds [fSeconds]
'
Dim fTimer As Single ' intial timer value
fTimer = Timer
Do While Timer - fTimer < fSeconds
If AllowEvents Then
DoEvents
End If
'
' if we cross midnight, back up one day
'
If Timer < fTimer Then
fTimer = fTimer - 86400 'one day in seconds 24*60*60
End If
Loop
End Sub
'_______________________________________

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

Jean-Guy Marcil

singeredel was telling us:
singeredel nous racontait que :
Thanks again for responding. Unfortunately, the program code
continues to run after the dialogue box for the line-counting program
is activated and before I can actually execute the dialogue box, so
the deleted paragraph reappears in the document before I can run the
line-count program.

I have tried to step through my program code but, as mentioned
before, it will not continue to step through any code after the
"Application.Run MacroName" code, although the code appears to run
because the deleted text reappears in the document.

Sorry, if you have followed my instructions and used the code exactly as I
have suggested then this is as far as I can go. All the tests I have run
with the code I posted worked on my machine. The modeless forms appears and
any code after the call to the Sub containing the creation of the modeless
form is paused until the said form is closed an unloaded.

As I wrote in an earlier reply, not having access to the code in the line
courting add-in does not help and many of these commercial add-ins are badly
coded. One common example is Acrobat. Their add-in are infamous for screwing
up the way Word works...

Maybe someone else as a different approach.

You could start a new thread in the userform group mentioning this new facet
of the problem: pausing code execution when calling an add-in sub that
contains a modeless userform.

Good luck.

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

singeredel

Thank you so much for all your follow-up. At least for now I can easily
delete the text, thanks to your programming, and then run the line-counting
program separately, which will help greatly. In the meantime, I will take
your advice and post another message with the new problem.

Again, your help was much appreciated!
 

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