SW API - How do I use GetNext?
SW API - How do I use GetNext?
(OP)
Hey Gurus,
I have a form with two TextBoxes (txtTitle and txtType), plus one Button (btnNext).
What I am trying to do is...
1. Form popsup, give info on the active opened Doc.
2. User presses Next Button and the Next document that is open becomes active and it's info repopulates the Textboxes.
What is my code missing? Please help.
Robert
Private Sub btnNext_Click()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swApp = Application.SldWorks
Set swModel = swApp.GetFirstDocument
While Not swModel Is Nothing
Set swModel = swModel.GetNext
swDocUpdate txtTitle, txtType
Wend
End Sub
'--------------------
Private Sub UserForm_initialize()
swDocUpdate txtTitle, txtType
End Sub
'--------------------
Function swDocUpdate(strTxtTitle As Object, strTxtType As Object)
Dim swApp As SldWorks.SldWorks
Dim swModelDoc As SldWorks.ModelDoc2
Dim swDocTitle As String
Dim swDocType As String
Dim swDocGetNext As String
Dim strTypePRT As String
Dim strTypeASM As String
Dim strTypeDRW As String
Set swApp = Application.SldWorks
Set swModelDoc = swApp.ActiveDoc
strTypePRT = "Part (SLDPRT)" 'Part = 1
strTypeASM = "Assembly (SLDASM)" 'Assembly = 2
strTypeDRW = "Drawing (SLDDRW)" 'Drawing = 3
'Get Component Information
swDocTitle = swModelDoc.GetTitle
swDocType = swModelDoc.GetType
If swDocType = 1 Then
strTxtType.Text = strTypePRT
ElseIf swDocType = 2 Then
strTxtType.Text = strTypeASM
Else
strTxtType.Text = strTypeDRW
End If
strTxtTitle.Text = swDocTitle
End Function
I have a form with two TextBoxes (txtTitle and txtType), plus one Button (btnNext).
What I am trying to do is...
1. Form popsup, give info on the active opened Doc.
2. User presses Next Button and the Next document that is open becomes active and it's info repopulates the Textboxes.
What is my code missing? Please help.
Robert
Private Sub btnNext_Click()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swApp = Application.SldWorks
Set swModel = swApp.GetFirstDocument
While Not swModel Is Nothing
Set swModel = swModel.GetNext
swDocUpdate txtTitle, txtType
Wend
End Sub
'--------------------
Private Sub UserForm_initialize()
swDocUpdate txtTitle, txtType
End Sub
'--------------------
Function swDocUpdate(strTxtTitle As Object, strTxtType As Object)
Dim swApp As SldWorks.SldWorks
Dim swModelDoc As SldWorks.ModelDoc2
Dim swDocTitle As String
Dim swDocType As String
Dim swDocGetNext As String
Dim strTypePRT As String
Dim strTypeASM As String
Dim strTypeDRW As String
Set swApp = Application.SldWorks
Set swModelDoc = swApp.ActiveDoc
strTypePRT = "Part (SLDPRT)" 'Part = 1
strTypeASM = "Assembly (SLDASM)" 'Assembly = 2
strTypeDRW = "Drawing (SLDDRW)" 'Drawing = 3
'Get Component Information
swDocTitle = swModelDoc.GetTitle
swDocType = swModelDoc.GetType
If swDocType = 1 Then
strTxtType.Text = strTypePRT
ElseIf swDocType = 2 Then
strTxtType.Text = strTypeASM
Else
strTxtType.Text = strTypeDRW
End If
strTxtTitle.Text = swDocTitle
End Function






RE: SW API - How do I use GetNext?
You need to use the statement SldWorks.ActivateDoc2 after the getnext to make the document active. One word of waring; the getfirstdocument and getnext work on the order in which the docuements were opened. So the first document may be duplicated again if it was not the first document opened.
SA
RE: SW API - How do I use GetNext?
Also, in your code for the "Next" button, there is no stopping point other than the final document. As written here, it will:
1. Get the first docuemnt
2. Get the next document
3. Update the text box values
4. Repeat 2 and 3 without stopping or pausing until reaching the last document. During this time, system resources will be dedicated to actions other than repainting the form so you probably won't even see the values updating. All you will see is the last document's information.
I believe what you need in this case are a couple of variables that are global to this UserForm. They will hold their value while the form is displayed but after the individual procedure that sets them is finished. Check VBA help for variable scope.
RE: SW API - How do I use GetNext?
Following handleman's advice will also allow you to eliminate your duplicate code of getting the swApp and swModel objects twice. And you can also reduce the amount of code by not setting all results to variables. As an example I re-wrote your code as follows:
CODE
Dim swModel As SldWorks.ModelDoc2
Dim strDocType(1 To 3) As String
Private Sub btnNext_Click()
Set swModel = swModel.GetNext
'check if there is a next document
If swModel Is Nothing Then
MsgBox "No more documents."
Else 'display next document
swDocUpdate
End If
End Sub
Private Sub UserForm_initialize()
'get SolidWorks
Set swApp = Application.SldWorks
'get first document
Set swModel = swApp.GetFirstDocument
'check for active doc
If swModel Is Nothing Then
MsgBox "No active documents."
'no sense to go on - end macro
End
End If
'initilize doc types
strDocType(1) = "Part (SLDPRT)"
strDocType(2) = "Assembly (SLDASM)"
strDocType(3) = "Drawing (SLDDRW)"
'display first document
swDocUpdate
End Sub
Sub swDocUpdate()
Dim ActivateErrors As Long
'make document active
swApp.ActivateDoc2 swModel.GetTitle, True, ActivateErrors
'add check of ActivateErrors here
'get doc type
txtType.Text = strDocType(swModel.GetType)
'get doc title
txtTitle.Text = swModel.GetTitle
End Sub
Please note that if you have Windows set to not display known file extensions, the macro will not work as expected if you have two different document types that have the same file name.
SA
RE: SW API - How do I use GetNext?
Thank you so much for the code SolidAir, it works great. I appreciate the comments too, that helps a lot. But I ran into a problem. I want the Next button to continue to cycling through each document. I changed this
--------------
Private Sub btnNext_Click()
Set swModel = swModel.GetNext
'check if there is a next document
If swModel Is Nothing Then
'If no more documents, start from firts document again.
Set swModel = swApp.GetFirstDocument
Else 'display next document
swDocUpdate
End If
End Sub
--------------
But for some reason it stops on the last one, I click it again and then it continues to cycle through but always skips the first doc. What is wrong?
I appreciate your help, I really want to understand this. Coding looks fun, but difficult to get started in.
Thanks again guys for your help.
RE: SW API - How do I use GetNext?
Funny you should ask that, I originally had the code written that way but I did not think you wanted that.
Change your code to:
CODE
Set swModel = swModel.GetNext
'check if there is a next document
If swModel Is Nothing Then
'If no more documents, start from firts document again.
Set swModel = swApp.GetFirstDocument
End If
swDocUpdate
End Sub
What was happening is it was not calling the swDocUpdate procedure because it was in the else part of the block If-Then statement and thus was skipped the first time through.
SA
RE: SW API - How do I use GetNext?
RE: SW API - How do I use GetNext?
CODE
This line should exist in only one place in your macro, and it should not be part of a function or sub. It should be in the general declarations.
RE: SW API - How do I use GetNext?