×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

VBA adding notes to drawing from Excel

VBA adding notes to drawing from Excel

VBA adding notes to drawing from Excel

(OP)
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!!!

RE: VBA adding notes to drawing from Excel

(OP)
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!

RE: VBA adding notes to drawing from Excel

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??

RE: VBA adding notes to drawing from Excel

(OP)
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?

RE: VBA adding notes to drawing from Excel

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?

RE: VBA adding notes to drawing from Excel

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources