I am having some difficulty writing some code to cycle through files of a certain extension, open them, then cycle through the sheets within the file and perform an operation on each sheet. The difficulty I am having is cycling through the sheets properly. I actually have some code to cycle through the files in a directory, and then some code to cycle through the sheets. Each of these codes work fine by themselves. I just cannot seem to combine the code into one macro. Unfortunately I am not well versed in vba so I am sure my error is something very simple. Here is the code:
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim sDirectory As String
Dim workDrive As String
Dim workDir As String
Dim swDocType As String
Dim Response As String
Dim Doc As Object
Dim longstatus As Long
Dim longwarnings As Long
Dim vSheetName As Variant
Dim i As Integer
Dim DwgView As Object
Dim Note As Object
Dim Note_Text As String
Dim rval As Boolean
Dim Silent As Boolean
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swDraw = swModel
'sDirectory = InputBox(Prompt:="What directory do you want to process?", Title:="Process Directory",Default:="C:\Users\swright\Desktop\Macro Project")
sDirectory = "C:\Users\swright\Desktop\Macro Project"
workDrive = Left(sDirectory, 1)
workDir = sDirectory
ChDrive (workDrive)
ChDir (workDir)
swDocType = ".SLDDRW"
Response = Dir("")
Do Until Response = ""
If Right(UCase\$(Response), 7) = swDocType Then
Set Doc = swApp.OpenDoc6(Response, 3, 0, "", longstatus, longwarnings)
Set Doc = swApp.ActiveDoc
' Get all the sheet names
vSheetName = swDraw.GetSheetNames
' Start with the first sheet, iterate through all the sheets
For i = 0 To UBound(vSheetName)
swDraw.ActivateSheet (vSheetName)
Set DwgView = Doc.GetFirstView()
' Starting with the sheet, iterate through all views in drawing
While Not (DwgView Is Nothing)
' Get the first note in this view
Set Note = DwgView.GetFirstNote()
' Repeat until there are no more notes to get
While Not (Note Is Nothing)
Note_Text = UCase(Note.GetText)
rval = Note.SetText(Note_Text)
Set Note = Note.GetNext()
Wend
' Get the next view. Or not.
Set DwgView = DwgView.GetNextView()
Wend
Doc.ViewZoomtofit2
Next i
' Save the drawing
Doc.Save2 Silent
' Close the drawing
swApp.CloseDoc (Response)
End If
Response = Dir()
Loop
End Sub
If anyone can help provide some input to correct the code I would really appreciate it.
Also, if you have any tips to improve any other parts of the code then please by all means let me know. Thank you.
SolidworksApi macros