E
Ed
I have been trying to maintain a macro for a coworker for over a year,
sometimes by little tweaks, sometimes by total rewrites. This macro
processes a plain text report generated by a database. I've put it into a
template which is installed as an Add-In. It always works great on my
machine, but runs hit-or-miss on hers.
Today, after validating this on my machine, I sent her the template, she
installed and ran the macro. Several times an error message popped up -
"Code execution has been interrupted." The Continue and End buttons were
available, but Debug was greyed out. After hitting continue a half dozen
times, the macro finally ran through.
The code is below, if you can see anything right off the bat. Otherwise,
what's the best way to proceed with troubleshooting this thing as it runs?
Ed
Sub SetUpScoringReport()
Dim strFPath As String
Dim strFName As String
Dim strItem As String
Dim strSerNo As String
'Dim doc1 As Document
Dim doc2 As Document
Dim dlgFName As Dialog
Dim PgBrk As Boolean
PgBrk = False
'Set doc1 = ActiveDocument
' Get scoring report to run macro on
Set dlgFName = Dialogs(wdDialogFileOpen)
With dlgFName
If .Display <> 0 Then
strFName = WordBasic.FileNameInfo$(.Name, 1)
End If
End With
If strFName <> "" Then
Documents.Open FileName:=strFName
Set doc2 = ActiveDocument
Else
GoTo EndThis
End If
strFPath = doc2.Path
Application.ScreenUpdating = False
' Set up scoring doc and save
With doc2.PageSetup
.Orientation = wdOrientLandscape
.BottomMargin = 30
.TopMargin = 30
End With
doc2.ActiveWindow.View.Type = wdPrintView
doc2.Content.Select
Selection.Font.Name = "Courier New"
Selection.Font.Size = 8
On Error GoTo EndThis
Selection.HomeKey wdStory
With Selection.Find
.Text = "ITEM#: " ' Will be part of file name
.Forward = True
End With
Selection.Find.Execute
Selection.Collapse Direction:=wdCollapseEnd
Selection.MoveEndUntil Cset:=" ", Count:=wdForward
If Selection.Text <> "" Then
strItem = Selection.Text
End If
Selection.HomeKey wdStory
With Selection.Find
.Text = "SERIAL# " ' Will be part of file name
.Forward = True
End With
Selection.Find.Execute
Selection.Collapse Direction:=wdCollapseEnd
Selection.MoveEndUntil Cset:=" ", Count:=wdForward
If Selection.Text <> " " Then
strSerNo = Selection.Text
End If
doc2.SaveAs FileName:=strFPath & "\ScoreDoc_" & _
strItem & " " & strSerNo & " " & _
Format(Now, "mm-dd-yy") & ".doc", _
FileFormat:=wdFormatDocument
' Check for marker, replace with page break
Selection.HomeKey wdStory
With Selection.Find
.Text = "^p1^p^0000"
.Replacement.Text = "^m"
.Forward = True
End With
If Selection.Find.Execute = True Then
With Selection.Find
.Text = "^p1^p^0000"
.Replacement.Text = "^m"
.Forward = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
PgBrk = True
End If
' Find / replace "garbage" characters
Selection.HomeKey wdStory
With Selection.Find
.Text = "^0000"
.Replacement.Text = ""
.Forward = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
' If page breaks have not been inserted,
' find the start of each report and insert break
If PgBrk = False Then
With Selection.Find
.Text = "^pFILE: "
.Replacement.Text = "^mFILE: "
.Forward = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End If
If PgBrk = True Then
Selection.HomeKey wdStory
Selection.MoveRight wdCharacter, Count:=1, Extend:=wdExtend
Selection.Delete
End If
Repl_All_MTPs
Repl_All_MTPs
Repl_All_MTPs
Repl_All_MTPs
doc2.Save
Application.ScreenUpdating = True
Selection.HomeKey wdStory
MsgBox "I'm done!"
EndThis:
Application.ScreenUpdating = True
End Sub
Sub Repl_All_MTPs()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^p^p^m"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do
Selection.Find.Execute
If Selection.Find.Found = True Then
Selection.Collapse wdCollapseStart
Selection.MoveRight Unit:=wdCharacter, _
Count:=1, Extend:=wdExtend
Selection.Delete
Else
Exit Do
End If
Loop
End Sub
sometimes by little tweaks, sometimes by total rewrites. This macro
processes a plain text report generated by a database. I've put it into a
template which is installed as an Add-In. It always works great on my
machine, but runs hit-or-miss on hers.
Today, after validating this on my machine, I sent her the template, she
installed and ran the macro. Several times an error message popped up -
"Code execution has been interrupted." The Continue and End buttons were
available, but Debug was greyed out. After hitting continue a half dozen
times, the macro finally ran through.
The code is below, if you can see anything right off the bat. Otherwise,
what's the best way to proceed with troubleshooting this thing as it runs?
Ed
Sub SetUpScoringReport()
Dim strFPath As String
Dim strFName As String
Dim strItem As String
Dim strSerNo As String
'Dim doc1 As Document
Dim doc2 As Document
Dim dlgFName As Dialog
Dim PgBrk As Boolean
PgBrk = False
'Set doc1 = ActiveDocument
' Get scoring report to run macro on
Set dlgFName = Dialogs(wdDialogFileOpen)
With dlgFName
If .Display <> 0 Then
strFName = WordBasic.FileNameInfo$(.Name, 1)
End If
End With
If strFName <> "" Then
Documents.Open FileName:=strFName
Set doc2 = ActiveDocument
Else
GoTo EndThis
End If
strFPath = doc2.Path
Application.ScreenUpdating = False
' Set up scoring doc and save
With doc2.PageSetup
.Orientation = wdOrientLandscape
.BottomMargin = 30
.TopMargin = 30
End With
doc2.ActiveWindow.View.Type = wdPrintView
doc2.Content.Select
Selection.Font.Name = "Courier New"
Selection.Font.Size = 8
On Error GoTo EndThis
Selection.HomeKey wdStory
With Selection.Find
.Text = "ITEM#: " ' Will be part of file name
.Forward = True
End With
Selection.Find.Execute
Selection.Collapse Direction:=wdCollapseEnd
Selection.MoveEndUntil Cset:=" ", Count:=wdForward
If Selection.Text <> "" Then
strItem = Selection.Text
End If
Selection.HomeKey wdStory
With Selection.Find
.Text = "SERIAL# " ' Will be part of file name
.Forward = True
End With
Selection.Find.Execute
Selection.Collapse Direction:=wdCollapseEnd
Selection.MoveEndUntil Cset:=" ", Count:=wdForward
If Selection.Text <> " " Then
strSerNo = Selection.Text
End If
doc2.SaveAs FileName:=strFPath & "\ScoreDoc_" & _
strItem & " " & strSerNo & " " & _
Format(Now, "mm-dd-yy") & ".doc", _
FileFormat:=wdFormatDocument
' Check for marker, replace with page break
Selection.HomeKey wdStory
With Selection.Find
.Text = "^p1^p^0000"
.Replacement.Text = "^m"
.Forward = True
End With
If Selection.Find.Execute = True Then
With Selection.Find
.Text = "^p1^p^0000"
.Replacement.Text = "^m"
.Forward = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
PgBrk = True
End If
' Find / replace "garbage" characters
Selection.HomeKey wdStory
With Selection.Find
.Text = "^0000"
.Replacement.Text = ""
.Forward = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
' If page breaks have not been inserted,
' find the start of each report and insert break
If PgBrk = False Then
With Selection.Find
.Text = "^pFILE: "
.Replacement.Text = "^mFILE: "
.Forward = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End If
If PgBrk = True Then
Selection.HomeKey wdStory
Selection.MoveRight wdCharacter, Count:=1, Extend:=wdExtend
Selection.Delete
End If
Repl_All_MTPs
Repl_All_MTPs
Repl_All_MTPs
Repl_All_MTPs
doc2.Save
Application.ScreenUpdating = True
Selection.HomeKey wdStory
MsgBox "I'm done!"
EndThis:
Application.ScreenUpdating = True
End Sub
Sub Repl_All_MTPs()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^p^p^m"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do
Selection.Find.Execute
If Selection.Find.Found = True Then
Selection.Collapse wdCollapseStart
Selection.MoveRight Unit:=wdCharacter, _
Count:=1, Extend:=wdExtend
Selection.Delete
Else
Exit Do
End If
Loop
End Sub