Debug works fine - run doesn't!

M

Mark

I am using Word 97.

I have some code which was kindly provided by Harold Staff which I have
manipulated a little.

When I debug it line by line it works fine and provides the document
produced is as it should be.

When I run it, the document produced is wrong.

Does anyone know what the problem might be?
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Mark > écrivait :
In this message, < Mark > wrote:

|| I am using Word 97.
||
|| I have some code which was kindly provided by Harold Staff which I have
|| manipulated a little.
||
|| When I debug it line by line it works fine and provides the document
|| produced is as it should be.
||
|| When I run it, the document produced is wrong.
||
|| Does anyone know what the problem might be?
||

Without seeing the code, it is hard to say anything...

But, I would guess that you are using the Selection object. Sometimes, when
you run step by step the code runs slower and the document has time to
"catch up" with the code. But in real time, the documents gets behind the
code and the cursor is not where it should be. Using the Range object often
eliminates this problem.

Also, sometimes it helps to sandwich your code between:
Application.ScreenUpdating = False
'your code
Application.ScreenRefresh
Application.ScreenUpdating = True

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

Mark

Hello Jean-Guy,

Here is the code :-

Sub CleanUpRR1()
Dim MyFileName, DateName, SearchText As String
Dim iPgs As Integer ' number of pages in doc-001
Dim Doc1 As Document ' document doc-001
Dim Doc2 As Document ' document doc-002
Dim Rng1 As Range ' selection.range in doc 1
Dim Rng2 As Range ' all of doc 2

Application.ScreenUpdating = False

ChangeFileOpenDirectory "\\wyp_gen_1\ea_div$\Press\"

Application.Dialogs(wdDialogFileOpen).Show

DateName = ActiveDocument.Name

MyFileName = ActiveDocument.Name

ActiveDocument.SaveAs "\\wyp_gen_1\ea_div$\Press\Converted\" &
MyFileName & ".doc"
MyFileName = ActiveDocument.Name

Selection.HomeKey Unit:=wdStory

Set Doc1 = Documents(MyFileName)

Documents.Add

If ActiveWindow.View.SplitSpecial = wdPaneNone Then
ActiveWindow.ActivePane.View.Type = wdPageView
Else
ActiveWindow.View.Type = wdPageView
End If

ActiveDocument.SaveAs "\\wyp_gen_1\ea_div$\Press\Priority Crime\Priority
Crime - " & MyFileName

Set Doc2 = Documents("Priority Crime - " & MyFileName)

Doc2.PageSetup.RightMargin = CentimetersToPoints(1)
Doc2.PageSetup.LeftMargin = CentimetersToPoints(1)
Doc2.PageSetup.TopMargin = CentimetersToPoints(2)


' FINDS "BURGLARY DWELLING"
Doc1.Activate
Selection.ExtendMode = False
Selection.HomeKey Unit:=wdStory
SearchText = "BURGLARY DWELLING"
With Doc1.BuiltInDocumentProperties
Count = 0
For iPgs = 1 To .Item("Number of pages")
Selection.GoTo _
What:=wdGoToPage, _
Which:=wdGoToNext, _
Name:=CStr(iPgs)
Doc1.Bookmarks("\page").Select ' select the page
If InStr(Selection.Range.Text, SearchText) Then
Set Rng1 = Selection.Range.FormattedText
Set Rng2 = Doc2.Range.FormattedText
Rng2.InsertAfter Rng1
Count = Count + 1
End If
Next

Doc2.Activate
Selection.HomeKey Unit:=wdStory
Selection.InsertBreak Type:=wdPageBreak
Selection.HomeKey Unit:=wdStory
Selection.TypeParagraph
Selection.TypeText Text:=SearchText & " was found " & Count & " times"

End With

Doc2.Save
Doc1.Saved = True

Doc1.Close

Application.ScreenRefresh
Application.ScreenUpdating = True

End Sub
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Mark > écrivait :
In this message, < Mark > wrote:

|| Hello Jean-Guy,
||
|| Here is the code :-
||
|| Sub CleanUpRR1()
|| Dim MyFileName, DateName, SearchText As String
|| Dim iPgs As Integer ' number of pages in doc-001
|| Dim Doc1 As Document ' document doc-001
|| Dim Doc2 As Document ' document doc-002
|| Dim Rng1 As Range ' selection.range in doc 1
|| Dim Rng2 As Range ' all of doc 2
||
|| Application.ScreenUpdating = False
||
|| ChangeFileOpenDirectory "\\wyp_gen_1\ea_div$\Press\"
||
|| Application.Dialogs(wdDialogFileOpen).Show
||
|| DateName = ActiveDocument.Name
||
|| MyFileName = ActiveDocument.Name
||
|| ActiveDocument.SaveAs "\\wyp_gen_1\ea_div$\Press\Converted\" &
|| MyFileName & ".doc"
|| MyFileName = ActiveDocument.Name
||
|| Selection.HomeKey Unit:=wdStory
||
|| Set Doc1 = Documents(MyFileName)
||
|| Documents.Add
||
|| If ActiveWindow.View.SplitSpecial = wdPaneNone Then
|| ActiveWindow.ActivePane.View.Type = wdPageView
|| Else
|| ActiveWindow.View.Type = wdPageView
|| End If
||
|| ActiveDocument.SaveAs "\\wyp_gen_1\ea_div$\Press\Priority Crime\Priority
|| Crime - " & MyFileName
||
|| Set Doc2 = Documents("Priority Crime - " & MyFileName)
||
|| Doc2.PageSetup.RightMargin = CentimetersToPoints(1)
|| Doc2.PageSetup.LeftMargin = CentimetersToPoints(1)
|| Doc2.PageSetup.TopMargin = CentimetersToPoints(2)
||
||
|| ' FINDS "BURGLARY DWELLING"
|| Doc1.Activate
|| Selection.ExtendMode = False
|| Selection.HomeKey Unit:=wdStory
|| SearchText = "BURGLARY DWELLING"
|| With Doc1.BuiltInDocumentProperties
|| Count = 0
|| For iPgs = 1 To .Item("Number of pages")
|| Selection.GoTo _
|| What:=wdGoToPage, _
|| Which:=wdGoToNext, _
|| Name:=CStr(iPgs)
|| Doc1.Bookmarks("\page").Select ' select the page
|| If InStr(Selection.Range.Text, SearchText) Then
|| Set Rng1 = Selection.Range.FormattedText
|| Set Rng2 = Doc2.Range.FormattedText
|| Rng2.InsertAfter Rng1
|| Count = Count + 1
|| End If
|| Next
||
|| Doc2.Activate
|| Selection.HomeKey Unit:=wdStory
|| Selection.InsertBreak Type:=wdPageBreak
|| Selection.HomeKey Unit:=wdStory
|| Selection.TypeParagraph
|| Selection.TypeText Text:=SearchText & " was found " & Count & "
times"
||
|| End With
||
|| Doc2.Save
|| Doc1.Saved = True
||
|| Doc1.Close
||
|| Application.ScreenRefresh
|| Application.ScreenUpdating = True
||
|| End Sub
||

OK, that's a lot of code to debug in a few minutes....
Just a few quick comments:

Dim MyFileName, DateName, SearchText As String

This, I believe, declares SearchText As String, but the type for MyFileName,
DateName are not declared.
If you want all three to be strings, I think you have to write:

Dim MyFileName As String, DateName As String, SearchText As String

But I could be wrong, maybe the VBA compiler understands shortcuts.

DateName = ActiveDocument.Name

MyFileName = ActiveDocument.Name

Why assign the name to two variables? I did not see DateTime anywhere else
in the code. (I might have missed it, I did have just a cursory glance).

ActiveDocument.SaveAs "\\wyp_gen_1\ea_div$\Press\Converted\" &
MyFileName & ".doc"

Won't this generate a funny filename, such as "MyFile.doc.doc" or
"MyFile.txt.doc"?

Before I look at it in more details, can you answer a few questions?

I see that your code has the Application.ScreenUpdating bits that I had
suggested... Was that already there, or did you add it after reading my
post. If the latter, did you try running it with ScreenUpdating set to
false? Did it make any difference?

If not, can you at least tell us which part of your code misfires. You wrote
that the result was wrong.
What do you mean? How wrong is it? All the expected content is there, but
jumbled up? Or mis-formatted? Some of the expected content is missing?

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

Alex Ivanov

As Jean_Guy said, try to avoid the Selection object if possible.
Are you sure you need the page-by-page search? From your code it seems to me
that you really
need only a total number of matches, am I correct? In this case the
procedure like this one should be more accurate and much faster.

HTH

Sub FindMatches()
'
' FindMatches Macro
' Macro created 10/01/04 by AleksI
'
Dim doc1 As Document
Dim doc2 As Document

Dim rng As Range
Dim cnt As Integer
Dim SearchText As String

Set doc1 = ActiveDocument 'or open your doc
SearchText = "BURGLARY DWELLING"

Set rng = doc1.StoryRanges(wdMainTextStory)
'or simply this
'Set rng = doc1.Range
cnt = 0
With rng.Find
.Text = SearchText
While .Execute(Forward:=True)
Debug.Print .Text
cnt = cnt + 1
Wend
End With
Set doc2 = New Document 'or open existing one
doc2.Characters.Last.Text = "Matches Found: " & cnt
'or you can specify the presize location of the inserted text by a bookmark
'doc2.Bookmarks!Name_Of_Existing_Bookmark_In_Doc2.Range.Text = "Matches
Found: " & cnt

'lastly you may activate the doc2
doc2.Activate
End Sub
 
M

Mark

Jean-Guy,

Sorry for delay away from work for the weekend!

I had the screenupdating bits in but not the screen refresh.

I have declared the otgher two strings now, it seemed to work fine without
doing so, I always thought that that's how they were declared, a friend
taught me :)

How it misfires,

The summary reports at the begining of the document are incorrect and the
content copied is incorrect. Sometimes the document is 1500 pages in length
rather than 30 pages!


Mark
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Mark > écrivait :
In this message said:
Jean-Guy,

Sorry for delay away from work for the weekend!

I had the screenupdating bits in but not the screen refresh.

I have declared the otgher two strings now, it seemed to work fine without
doing so, I always thought that that's how they were declared, a friend
taught me :)

How it misfires,

The summary reports at the begining of the document are incorrect and the

I did not see in the code where these summary pages were created...
How are they incorrect?
content copied is incorrect. Sometimes the document is 1500 pages in length
rather than 30 pages!

Have you studied this problem? From 30 to 1,500... Is it that each page is
reproduced 50 times ("page 1" 50 times, "page 2" 50 times, et., or "pages
1-30," "pages 1-30," "pages 1-30," etc.)? Or the last page is pasted 1,471
times? Or each page is spread out to occupy 50 pages (whereas in the source
document it fitted on one page)?
This would help pinpoint the part of the code to debug.

--
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