Solidworks Macro find and replace on every drawing sheet
<p>I have some background with VBA and I hate doing the same task 100's of times. I often have to make Solidworks drawings,these drawings are templates that are mostly just tables that I populate with data. There are 3 things that need to be changed on each sheet in the file (from sheet 3 to the last sheet). Normally I go into each sheet and do 3 find and replaces to change each sheet. then move on to the next sheet and repeat.</p>

<p>My plan was to have the code count the number of sheets, prompt the user for the first find/replace, replace that text on all sheets, then repeat for the 2nd replace, and again for the 3rd. I recorded a macro and added some code but I keep getting run-time errors (in code below). Every other macro I've recorded never gave me so many errors, if you can please help </p>

<pre><code>Dim swApp As SldWorks.SldWorks
Dim swmodel As SldWorks.ModelDoc2
Dim swdraw As SldWorks.DrawingDoc
Dim Part As Object
Dim Otext As String
Dim Ntext As String
Dim Smax As Integer
Dim i As Integer
Dim swSheet As SldWorks.Sheet
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()
Set swApp = Application.SldWorks
Set swmodel = swApp.ActiveDoc
 'Set swSheet = swdraw.GetCurrentSheet

Smax = instance.GetSheetCount() - 3   ' runtime 424 error here
Set swSheet = swdraw.GetCurrentSheet  ' runtime 91 error if i skip the line above

Otext = Application.InputBox("find this text")
Ntext = Application.InputBox("find this text")

For i = 1 To Smax

Set Part = swApp.ActiveDoc
'--------------------Find and Replace Annotations--------------------
Set swUtil = swApp.GetAddInObject("Utilities.UtilitiesApp")
Set swUtilFindReplaceAnnotations = swUtil.FindReplaceAnnotations
longstatus = swUtilFindReplaceAnnotations.InitPMPage()
'--------------------Block Recording--------------------
#If 0 Then
#End If
'--------------------UnBlock Recording------------------
swUtilFindReplaceAnnotations.FindText = Otext
swUtilFindReplaceAnnotations.ReplaceText = Ntext
swUtilFindReplaceAnnotations.options = gtFraMatchCase
swUtilFindReplaceAnnotations.AnnotationFilter = gtFraAllTypes
Part.ClearSelection2 True
Part.ClearSelection2 True
Part.ClearSelection2 True
Part.ClearSelection2 True
Part.ClearSelection2 True
longstatus = swUtilFindReplaceAnnotations.ReplaceAll()
'--------------------Block Recording--------------------
#If 0 Then
#End If
'--------------------UnBlock Recording------------------
longstatus = swUtilFindReplaceAnnotations.Close()
Part.SheetNext
Part.ViewZoomtofit2

Next i

End Sub
</code></pre>