VBA to rename sheets?

Discussion in 'SolidWorks' started by Brian Mears, Jan 13, 2004.

  1. Brian Mears

    Brian Mears Guest

    I'm new to SolidWorks. I was able to write simple custom VBA programs
    in my previous CAD program (Mechanical Desktop). It's been a while,
    and I'm pretty unfamiliar with SolidWorks and VBA, so I'm wondering if
    anybody would be willing to get me started on a simple program.

    When the user runs the macro, it will rename each sheet in a
    multiple-sheet drawing to the name of the part on it. For example,
    imagine a drawing which contains Sheet1, Sheet2, and Sheet3. When the
    VBA program is run, the sheets would be renamed to Top Panel, Brace,
    and Side Panel.

    Very basic format of VBA program would be something like this:

    For Each 'sheet' in 'drawing file'
    set 'sheet name' = 'drawing view name'
    Next

    Again, I'm not looking for anybody to write this for me, but it should
    be pretty simple, so if you can/will do the whole thing, knock
    yourself out. Thanks!

    Brian
     
    Brian Mears, Jan 13, 2004
    #1
  2. Brian Mears

    Heikki Leivo Guest

    Very basic format of VBA program would be something like this:
    To encourage you to solve the problem by yourself, instead of writing a
    ready macro I will give you some hints. Your macro should do the following
    tasks :

    1. Get the active drawing document by calling SldWorks::ActiveDoc method
    2. Get the list of sheet names in the drawing by calling
    DrawingDoc::GetSheetNames
    3. Loop through each sheet by calling DrawingDoc::ActivateSheet and passing
    the sheet name as parameter
    4. Get the active sheet handle by calling DrawingDoc::GetCurrentSheet

    Inside the loop your macro should do following tasks for each sheet:

    5. Get the first view of the drawing by calling DrawingDoc::GetFirstView,
    which will actually return the view associated to the drawing sheet.
    6. Get the first actual model view by calling View::GetNextView
    7. Get the model name inside the view by calling
    View::GetReferencedModelName
    8. Do whatever with the model name and then call Sheet::SetName for the
    active sheet, which was returned by GetCurrentSheet method in step 4.

    Of course you should, to avoid errors, verify that you are not trying to
    rename multiple sheets to same name.

    There is _lots_ of information available in the API help, I wrote this just
    by browsing the API help back and forth for a while. For more detailed help
    search for the keywords mentioned abowe. Don't hesitate to ask if you need
    more specific information!

    Hope this helps!

    Kind regards,
    Heikki
     
    Heikki Leivo, Jan 13, 2004
    #2
  3. Brian Mears

    Brian Mears Guest

    Heikki-

    I did it last night, and it worked. The code is very sloppy, but it
    worked. Just wanted to say thanks for your help!

    Brian
     
    Brian Mears, Jan 15, 2004
    #3
  4. Brian Mears

    Heikki Leivo Guest

    Heikki-
    Gee.. you're welcome! It ain't so hard, is it? :)

    -h-
     
    Heikki Leivo, Jan 16, 2004
    #4
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.