ActiveWorkbook.ReadOnly Object Variable error

calebfulks

New Member
Joined
Dec 9, 2011
Messages
3
I have enough experience with VBA and macros to get into trouble, and this is my first post here.
I have written a macro to be usde from Solidworks 2011 that opens an excel file, finds a part number(ModName) and then updates some values that are associated with that part number. After getting that portion to work, I added an If then statement using the activeworkbook.readonly to ensure that the user has aces to the excel file. If I step through the program with F8 inside visual basic, the program works as planned every time, but when I run the macro in Solidworks it gives a an error every-other time: run-time error '91', Object Variable or With block not set. Any help would be very appreciated.
There is a second set of code below what I have shown here, hence all of the extra declarations.



Code:
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Revision As String
Dim Proptype As String
Dim Partnumber As String
Dim MyPath, ModName, NewName1, NewName2, OldRevPath As String
Dim MB1 As Boolean
Dim MB2 As Boolean
Dim Errs As Long
Dim Warnings As Long
Dim swFrame As SldWorks.Frame
Dim FSO As Object
Dim OldRev As Object
 
Sub main()
Set swApp = Application.SldWorks
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set Model = swApp.ActiveDoc
Set swFrame = swApp.Frame
Revision = Part.GetCustomInfoValue("", "Revision")
ModName = Left(Model.GetTitle, InStrRev(Model.GetTitle, " Sheet") - 3)
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
 
If GetAttr("X:\Book1.xlsx") And vbReadOnly Then
      MsgBox "You Do NOT have write acces to the Number Log"
        Exit Sub
   End If
objExcel.Workbooks.open ("x:\Book1.xlsx")
If activeworkbook.ReadOnly Then
      MsgBox "You Do NOT have write acces to the Number Log"
      objExcel.activeworkbook.Close
        Exit Sub
 
   End If
Cells.Find(What:=ModName, After:=ActiveCell, LookIn:=xlFormulas, _
               LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
               MatchCase:=False, SearchFormat:=False).Select
ActiveCell.Offset(0, 2).Select
objExcel.ActiveCell.Value = Revision
ActiveCell.Offset(0, -3).Select
objExcel.ActiveCell.Value = "Released"
objExcel.activeworkbook.Save
objExcel.activeworkbook.Close
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I get the error when it trys to do the ActiveWorkbook.ReadOnly

Code:
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Revision As String
Dim Proptype As String
Dim Partnumber As String
Dim MyPath, ModName, NewName1, NewName2, OldRevPath As String
Dim MB1 As Boolean
Dim MB2 As Boolean
Dim Errs As Long
Dim Warnings As Long
Dim swFrame As SldWorks.Frame
Dim FSO As Object
Dim OldRev As Object
 
Sub main()
Set swApp = Application.SldWorks
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set Model = swApp.ActiveDoc
Set swFrame = swApp.Frame
Revision = Part.GetCustomInfoValue("", "Revision")
ModName = Left(Model.GetTitle, InStrRev(Model.GetTitle, " Sheet") - 3)
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True

If GetAttr("X:\Book1.xlsx") And vbReadOnly Then
      MsgBox "You Do NOT have write acces to the Number Log"
        Exit Sub
   End If
objExcel.Workbooks.open ("x:\Book1.xlsx")
If activeworkbook.ReadOnly Then 'THE ERROR OCCURS HERE!
      MsgBox "You Do NOT have write acces to the Number Log"
      objExcel.activeworkbook.Close
        Exit Sub
        
   End If
Cells.Find(What:=ModName, After:=ActiveCell, LookIn:=xlFormulas, _
               LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
               MatchCase:=False, SearchFormat:=False).Select
ActiveCell.Offset(0, 2).Select
objExcel.ActiveCell.Value = Revision
ActiveCell.Offset(0, -3).Select
objExcel.ActiveCell.Value = "Released"
objExcel.activeworkbook.Save
objExcel.activeworkbook.Close
 
Last edited:
Upvote 0
Are you sure that the previous line is succesfully opening the workbook?
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top