Preview | SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
TFTom Fosler04/06/2007
Greetings! I would like a
simplequestion answered. I would like to add notes in from an
excelspreadhseet to my drawing template. Modifying the "Set NewNote
Example" from the Solidworks API help file, I have madethe code
below:
'------------------------
Private Sub SetNotes_Click()
Dim swApp As Object ' Define variableused to hold the SldWorks object
Dim Model, DwgDoc As Object ' Definevariable used to hold the ModelDoc & PartDoc objects
Dim SelMgr As Object ' Define variable usedto hold the SelectionManager Object
Dim selObj As Object ' Variable for theselected object
Const swSelNOTES = 15 ' Value consistent withdefinitions in swconst.bas
Set swApp =CreateObject("SldWorks.Application") 'Attach to or open SolidWorks session
Set Model =swApp.ActiveDoc 'Grab the current document
Set DwgDoc =Model 'PartDoc & ModelDoc are same in VB
DwgDoc.EditTemplate 'Edit the current drawing template
Set View =DwgDoc.GetFirstView 'This is the template
Worksheets("DrawingSettings").Range("C9").Select
Dim DwgNotes As Range
Set DwgNotes = Range(Selection, Selection.End(xlDown)).Copy
Model.SelectByID "DwgNotes@Sheet Format1","NOTE", 0, 0, 0 ' Select the note for the DrawingName
Set SelMgr =Model.SelectionManager() 'Get the selection manager class
If (SelMgr.GetSelectedObjectCount <>0)Then 'If user has selected something
Set selObj =SelMgr.GetSelectedObject2(1) 'Get the first item in the selection list
If(SelMgr.GetSelectedObjectType(1) = swSelNOTES)Then ' If selected object is anote
ret= selObj.SetText("NOTES:" & Chr(13) & Chr(10)& _
"<PARA indent=10findent=-10 number=on ntype=1 nformat=$$. nstartNum=0>"& _
DwgNotes) 'Change the text in the note
If(ret = True)Then 'If change is successful
DwgDoc.EditRebuild 'Rebuild to see the change
Else 'If name change failed
swApp.SendMsgToUser("Error changing note text.")
EndIf
Else 'If selected object was not a note
swApp.SendMsgToUser("Please Select a Note for this operation.")
EndIf 'End if selected object was note or not
EndIf 'End if there is an object selected
End Sub
'-----------------
When I try to run the code, Excel highlights the nonblank cells andassigns them to the DwgNotes dim (at least I think it does) but Iget an error that says "424, Object required". What iswrong with this code? I would think it would work, but I am notsure what I am missing, as I am a newbie in VBA (SWX or XL) andhave been teaching myself in the process. Any insights??
THANKS!!!
'------------------------
Private Sub SetNotes_Click()
Dim swApp As Object ' Define variableused to hold the SldWorks object
Dim Model, DwgDoc As Object ' Definevariable used to hold the ModelDoc & PartDoc objects
Dim SelMgr As Object ' Define variable usedto hold the SelectionManager Object
Dim selObj As Object ' Variable for theselected object
Const swSelNOTES = 15 ' Value consistent withdefinitions in swconst.bas
Set swApp =CreateObject("SldWorks.Application") 'Attach to or open SolidWorks session
Set Model =swApp.ActiveDoc 'Grab the current document
Set DwgDoc =Model 'PartDoc & ModelDoc are same in VB
DwgDoc.EditTemplate 'Edit the current drawing template
Set View =DwgDoc.GetFirstView 'This is the template
Worksheets("DrawingSettings").Range("C9").Select
Dim DwgNotes As Range
Set DwgNotes = Range(Selection, Selection.End(xlDown)).Copy
Model.SelectByID "DwgNotes@Sheet Format1","NOTE", 0, 0, 0 ' Select the note for the DrawingName
Set SelMgr =Model.SelectionManager() 'Get the selection manager class
If (SelMgr.GetSelectedObjectCount <>0)Then 'If user has selected something
Set selObj =SelMgr.GetSelectedObject2(1) 'Get the first item in the selection list
If(SelMgr.GetSelectedObjectType(1) = swSelNOTES)Then ' If selected object is anote
ret= selObj.SetText("NOTES:" & Chr(13) & Chr(10)& _
"<PARA indent=10findent=-10 number=on ntype=1 nformat=$$. nstartNum=0>"& _
DwgNotes) 'Change the text in the note
If(ret = True)Then 'If change is successful
DwgDoc.EditRebuild 'Rebuild to see the change
Else 'If name change failed
swApp.SendMsgToUser("Error changing note text.")
EndIf
Else 'If selected object was not a note
swApp.SendMsgToUser("Please Select a Note for this operation.")
EndIf 'End if selected object was note or not
EndIf 'End if there is an object selected
End Sub
'-----------------
When I try to run the code, Excel highlights the nonblank cells andassigns them to the DwgNotes dim (at least I think it does) but Iget an error that says "424, Object required". What iswrong with this code? I would think it would work, but I am notsure what I am missing, as I am a newbie in VBA (SWX or XL) andhave been teaching myself in the process. Any insights??
THANKS!!!