?
Solved

VBA Maco for MS Word, page # problem, Please Help?

Posted on 2003-02-27
4
Medium Priority
?
195 Views
Last Modified: 2010-04-07
I need to develop a macro that will loop through a document and find and format each word "folder". Any ideas? I tryed counting the lines and looping through the count but it seems to lock up. I have over 500 pages of text. Word runs through the count loop slower and slower as the # of lines in the document increase. Lines 1 - 100 take 3 sec, ;but lines 4000 - 4100 take 15 sec, and by the 9000 line this takes forever and never finishes. Here is what I am starting with.


Sub Macro1()
Dim strFind As String
   
   strFind = "Folder"      'case is not important
 
   Application.ScreenUpdating = False
   Dim currentSentence As Word.Range
   Dim counter As Integer
   counter = 1
     
   'number of sentences in the document
   Dim sentenceCount As Integer

   With ActiveDocument
       sentenceCount = .Sentences.Count
       If sentenceCount = 0 Then Exit Sub
   End With
         
   'loop through all sentences
   For counter = 1 To sentenceCount
   
       'do one sentence at a time
       Set currentSentence = ActiveDocument.Sentences(counter)
   
       With currentSentence.Find
         
           'check sentence for the target word
           If .Execute(FindText:=strFind) Then
         
             ' target word was found; bold the entire current sentence
               ActiveDocument.Sentences(counter).Font.Bold = True
               ActiveDocument.Sentences(counter).InsertParagraphBefore
                               
           End If
       
       End With
       
   Next counter
End Sub
0
Comment
Question by:BigBee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Expert Comment

by:da_munches
ID: 8038583
I'm not sure if this will help, but try adding the following before "next counter":

  set currentSentence = Nothing

My thinking is that each time you set currentSentence, Word may not be releasing the memory correctly.  
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 100 total points
ID: 8040212
Perhaps I'll get an A grade in this copy of the question?

Sub FindReplace()

ActiveDocument.Select
With Selection.Find
   .ClearFormatting
   .MatchCase = False
   .MatchWholeWord = True
                 
   Do While .Execute(FindText:="Folder") = True
       Selection.HomeKey Unit:=wdLine
       Selection.EndKey Unit:=wdLine, Extend:=wdExtend
       Selection.Font.Bold = True
       Selection.InsertBefore (vbCrLf)
       Selection.MoveDown (wdLine)
       Selection.MoveStart (wdLine)
       Selection.MoveEnd (wdStory)
   Loop
End With


Steve
0
 

Author Comment

by:BigBee
ID: 8043060
Ok you got it. LOL...
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 8043118
OK cheers, bit cheeky but what the hell :-)

Steve
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question