AnsweredAssumed Answered

How is an array with hard-coded values made in SW API/VBA/Macro?

Question asked by Hank Costner on Oct 31, 2017
Latest reply on Oct 31, 2017 by Josh Brady



I'm having trouble hard-coding an array. It's called "filenames", and it's defined about half-way through. I've tried to assign values a few different ways, and right now, I'm trying to do it dynamically.

The error the compiler is giving me is Runtime error '13': Type Mismatch.

I defined filenames dynamically as a string (I think).


I'm not familiar with the SW coding environment. I normally use C++ and MatLab.

I'm using SW 2016 SP5, and when I make a new macro, it opens a Microsoft Visual Basic for Applications Window. I thought that meant I'm using VBA, but some VBA syntax hasn't worked, like the "End While" statement. So I'm not sure if this is API or VBA.


I included my code below. I have a drawing with a lot of sheets open when I run the code.



Dim swApp As Object

Sub main()


Dim swModel As Object


Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

    If (swModel Is Nothing) Then

        MsgBox "No active drawing found in SolidWorks." & Chr(13) & Chr(13) _

            & "Please load/activate a SolidWorks drawing ", vbExclamation


    End If


    If (swModel.GetType <> swDocDRAWING) Then

        MsgBox "Current document is not a drawing." & Chr(13) & Chr(13) _

            & "Please load/activate a SolidWorks drawing ", vbExclamation


    End If



Dim numberOfSheets As Integer

Dim SalesPageToPrint(0) As Integer 'Page Array

Dim FabPagesToPrint(1) As Integer 'Page Array

Dim n As Integer 'file name placeholder

Dim fileNames() As String 'names for files

Dim pageIncrement As Integer 'used to step through Page Array

Dim i As Integer 'iterator



numberOfSheets = swModel.GetSheetCount

SalesPageToPrint(0) = 1

FabPagesToPrint(0) = 2

FabPagesToPrint(1) = 3

i = 0

n = 0


fileNames = Array("125112 - Production", "125112 - Sales", _

"125117 - Production", "125117 - Sales")



Do While i < 3


swModel.Extension.PrintOut2 SalesPageToPrint, 1, True, "", fileNames(n)

n = n + 1

swModel.Extension.PrintOut2 FabPagesToPrint, 1, True, "", fileNames(n)



SalesPageToPrint(0) = SalesPageToPrint(0) + 3

FabPagesToPrint(0) = FabPagesToPrint(0) + 3

FabPagesToPrint(1) = FabPagesToPrint(1) + 3

n = n + 1

i = i + 3






End Sub