Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA adding notes to drawing from Excel

Status
Not open for further replies.

smma

Mechanical
Jun 4, 2007
24
US
Greetings! I am new to the forum, and would like a simple question answered. I would like to add notes in from an excel spreadhseet to my drawing template. Modifying the "Set New Note Example" from the Solidworks API help file, I have made the code below:

'------------------------

Private Sub SetNotes_Click()
Dim swApp As Object ' Define variable used to hold the SldWorks object
Dim Model, DwgDoc As Object ' Define variable used to hold the ModelDoc & PartDoc objects
Dim SelMgr As Object ' Define variable used to hold the SelectionManager Object
Dim selObj As Object ' Variable for the selected object
Const swSelNOTES = 15 ' Value consistent with definitions 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("Drawing Settings").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 Drawing Name
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 a note
ret = selObj.SetText("NOTES:" & Chr(13) & Chr(10) & _
"<PARA indent=10 findent=-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.")
End If
Else ' If selected object was not a note
swApp.SendMsgToUser ("Please Select a Note for this operation.")
End If ' End if selected object was note or not
End If ' End if there is an object selected
End Sub


'-----------------

When I try to run the code, Excel highlights the nonblank cells and assigns them to the DwgNotes dim (at least I think it does) but I get an error that says "424, Object required". What is wrong with this code? I would think it would work, but I am not sure what I am missing, as I am a newbie in VBA (SWX or XL) and have been teaching myself in the process. Any insights??

THANKS!!!
 
Replies continue below

Recommended for you

I know I'm not answering your question and I realise you may have your reasons for adding notes in this way. May I suggest looking at Lenny's CommonNotes as an alternative, though? Just an idea that may or may not help. :)

Matt
CAD Engineer/ECN Analyst
Silicon Valley, CA
sw.fcsuper.com
Co-moderator of Solidworks Yahoo! Group
 
Thanks Matt, but I have already evaluated it. While it is an impressive script, it doesn't quite fit what I am looking to do. Besides, with my limited understanding of VB, I couldn't troubleshoot it properly if we ever encountered a problem with it.

Like your site & the macros you have!
 
I didn't know you could insert a range object directly in to a SW note...don't you have to read the text out of the range, put it in to a text array and populate the note from the text array??
 
Interestingly enough, you can insert the range object, although only one cell reference at a time. I have successfully used a single cell reference as in Range("C9") and it has worked that way. However, if there are ten different notes, I want the code to send the array to SWX in individual lines. I don't know how to pull the text out of the range and set it in an array as you said, engAlright. What would be the best way to do that?
 
Where did you find this information on numeric bullets?
"<PARA indent=10 findent=-10 number=on ntype=1 nformat=$$. nstartNum=0>"
I trawled the VB help files to no avail. Do you know the code for a dot bullet...And how to get a notes bullet format?

 
Hi
I think the Excel object must be Set first to access by using the code
Dim ex as object
Set ex = GetObject(, "Excel.Application")(if excel is running)
Set ex = CreateObject(, "Excel.Application")(if excel is not running)

and to access the worksheets u can use below code.
ex.Application.Worksheets("Sheet1").Range("C3").Select

I hope by adding this your code will work fine

Vinay
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top