Any idea why this macro would run on 2018, but not on 2016 or 2017?
in 2016 and 2017 I get:
Run-time error '91':
Object variable or With block variable not set.
All it should need is a drawing open with a note on a layer named DEL ME.
'--------------------------------------------------
' How to delete all notes on a specified layer.
'
' Preconditions:
' 1. Open ball_valve.slddrw.
' 2. Double-click the note DELETE ME NOTE
' to open the Note PropertyManager page.
' 3. Scroll to the bottom of the PropertyManager page
' to verify that the note DELETE ME NOTE
' is on the DEL ME layer.
' 4. Click a blank area in the drawing sheet to close
' the Note PropertyManager page.
' 5. Run the macro.
'
' Postconditions: The "DELETE ME NOTE" is deleted. The DO NOT
' DELETE ME NOTE remains intact because it does not reside
' on the DEL ME layer.
'
' NOTE: Do not save any changes when closing the drawing document.
'---------------------------------------------------
Option Explicit
' Name of layer from which to delete DELETE ME NOTE note
Const DeleteLayer As String = "DEL ME"
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swDraw As SldWorks.DrawingDoc
Dim swSelMgr As SldWorks.SelectionMgr
Dim swView As SldWorks.View
Dim swAnn As SldWorks.Annotation
Dim swSelData As SldWorks.SelectData
Dim NumShts As Long
Dim bRet As Boolean
Dim i As Long
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension
Set swDraw = swModel
Set swSelMgr = swModel.SelectionManager
Set swSelData = swSelMgr.CreateSelectData
NumShts = swDraw.GetSheetCount
For i = 1 To NumShts
swDraw.SheetPrevious
Next i
For i = 1 To NumShts
' Clear the selection set for the sheet
swModel.ClearSelection2 True
Set swView = swDraw.GetFirstView
While Not swView Is Nothing
Set swAnn = swView.GetFirstAnnotation3
While Not swAnn Is Nothing
' If the annotation is a note, then determine
' if the layer is DEL ME
If swNote = swAnn.GetType Then
If DeleteLayer = swAnn.Layer Then
' If the layer is the DEL ME layer
' then select the notes residing
' on that layer
bRet = swAnn.Select3(True, swSelData)
End If
End If
Set swAnn = swAnn.GetNext3
Wend
Set swView = swView.GetNextView
Wend
' Delete the selected notes on the DEL ME layer
bRet = swModelDocExt.DeleteSelection2(swDelete_Absorbed)
swDraw.SheetNext
Next i
End Sub
replace this line
Set swApp = CreateObject("SldWorks.Application")
with
Set swApp = Application.SldWorks
Are you running this macro on computer with more than one SW version installed? Check in task manager how many solidworks processes are started.