Thread: Error problems
View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson[_5_] Dave Peterson[_5_] is offline
external usenet poster
 
Posts: 1,758
Default Error problems

Maybe you can just check in line and continue depending on what you find:

Option Explicit
Sub testme()

Dim myChartObject As ChartObject
Dim ChartIdentifierNum As Long
Dim ChartIdentifier As Variant
Dim ChartReference As Variant
Dim wdApp As Word.Application
Dim myRange As Word.Range

Set wdApp = New Word.Application
wdApp.Visible = True

wdApp.Documents.Add DocumentType:=wdNewBlankDocument

For ChartIdentifierNum = 1 To 40
ChartIdentifier = Sheets("Report Export Ref").Cells(3 _
+ ChartIdentifierNum, 5).Value
ChartReference = Sheets("Report Export Ref").Cells(3 _
+ ChartIdentifierNum, 6).Value

If ChartIdentifier < "" Then
If ChartReference < "" Then

Set myChartObject = Nothing
On Error Resume Next
Set myChartObject = Worksheets("Chart Sheet") _
.ChartObjects(ChartReference).Select
On Error GoTo 0

If myChartObject Is Nothing Then
'do nothing
Else
myChartObject.Copy
With wdApp
Set myRange = .ActiveDocument.Content
myRange.Find.Execute FindText:=ChartIdentifier, _
Forward:=True
If myRange.Find.Found = True Then
myRange.Select
.Selection.PasteSpecial Link:=False, _
DataType:=wdPasteMetafilePicture, _
Placement:=wdInLine, DisplayAsIcon:=False
End If
End With
End If
End If
End If
Next ChartIdentifierNum
End Sub

(I fudged the MSWord stuff.)


Nick Shinkins wrote:

Hiya I have this section of code which takes up to forty charts, identified
by references and pastes them into a word document.

I have an error handling statement which if the chart doesn't exist should
enable the procedure to recover and go on to the next chart.

I am testing the code by giving a reference to a chart that doesn't exist.
Unfortunately the code still crashes with an 1004 error, which I thought
would be trapped.

I do have another error handling code previously in the code but this should
be switched off when it reaches the code shown below.

Can anyone help? I can give more info if needed. Here is the code:

For ChartIdentifierNum = 1 To 40

ChartIdentifier = Sheets("Report Export Ref").Cells(3 +
ChartIdentifierNum, 5).Value
ChartReference = Sheets("Report Export Ref").Cells(3 +
ChartIdentifierNum, 6).Value

If ChartIdentifier < "" Then

If ChartReference < "" Then

Worksheets("Chart Sheet").Activate

On Error GoTo SkipChart
ActiveSheet.ChartObjects(ChartReference).Select

Selection.Copy

With wdApp
Set MyRange = .ActiveDocument.Content

MyRange.Find.Execute FindText:=ChartIdentifier, Forward:=True

If MyRange.Find.Found = True Then

MyRange.Select

.Selection.PasteSpecial Link:=False,
DataType:=wdPasteMetafilePicture, Placement:=wdInLine, DisplayAsIcon:=False

End If
End With

SkipChart:

On Error GoTo 0

End If

End If

Next ChartIdentifierNum

TIA!

A confused,

Nick Shinkins


--

Dave Peterson