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