I have the following code, which works the way I want it to when I click the button the first time. However, if I close out the excel and click the button again, it errors at the line "ActiveWorkbook.Worksheets("Ref").UnProtect", with the message "Run-time error '91', Object variable or With block variable not set". I can not figure this out, why in the world does it work the first time, but not the second???
Private Sub cmdReport_Click()
Dim path As String
Dim XL As Object
Dim oBook As Excel.Workbook
Dim pic As Excel.Shape
Set XL = CreateObject("Excel.Application")
If IsNull(Me.RawReport) Or Me.RawReport = "" Then
MsgBox "There currently are no Raw Data Forms for this project", , "No File"
Else
path = HyperlinkPart(Me.RawReport, acAddress)
With XL.Application
.Visible = True
.AskToUpdateLinks = False
.Workbooks.Open path
.AskToUpdateLinks = True
ActiveWorkbook.Worksheets("Ref").Unprotect **IT ERRORS HERE**
For Each pic In ActiveWorkbook.Worksheets("Ref").Shapes
If pic.Name = Forms!Main!txtCurrentUser Then
pic.Copy
Worksheets("TermsSig").Range("E12").Select
Worksheets("TermsSig").Paste Destination:=Worksheets("TermsSig").Range("E12")
With Worksheets("TermsSig").Range("E12")
Selection.ShapeRange.ScaleHeight 1.5020895209, _
msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.ScaleHeight 1.5020911212, _
msoFalse, msoScaleFromBottomRight
Selection.ShapeRange.IncrementTop -20.5
Selection.ShapeRange.IncrementLeft 7.75
End With
End If
Next pic
End With
End If
Set XL = Nothing
End Sub
Private Sub cmdReport_Click()
Dim path As String
Dim XL As Object
Dim oBook As Excel.Workbook
Dim pic As Excel.Shape
Set XL = CreateObject("Excel.Application")
If IsNull(Me.RawReport) Or Me.RawReport = "" Then
MsgBox "There currently are no Raw Data Forms for this project", , "No File"
Else
path = HyperlinkPart(Me.RawReport, acAddress)
With XL.Application
.Visible = True
.AskToUpdateLinks = False
.Workbooks.Open path
.AskToUpdateLinks = True
ActiveWorkbook.Worksheets("Ref").Unprotect **IT ERRORS HERE**
For Each pic In ActiveWorkbook.Worksheets("Ref").Shapes
If pic.Name = Forms!Main!txtCurrentUser Then
pic.Copy
Worksheets("TermsSig").Range("E12").Select
Worksheets("TermsSig").Paste Destination:=Worksheets("TermsSig").Range("E12")
With Worksheets("TermsSig").Range("E12")
Selection.ShapeRange.ScaleHeight 1.5020895209, _
msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.ScaleHeight 1.5020911212, _
msoFalse, msoScaleFromBottomRight
Selection.ShapeRange.IncrementTop -20.5
Selection.ShapeRange.IncrementLeft 7.75
End With
End If
Next pic
End With
End If
Set XL = Nothing
End Sub