VBA to rename sheets?

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

Reply to
Brian Mears
Loading thread data ...

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:

  1. Get the first view of the drawing by calling DrawingDoc::GetFirstView, which will actually return the view associated to the drawing sheet.
  2. Get the first actual model view by calling View::GetNextView
  3. Get the model name inside the view by calling View::GetReferencedModelName
  4. 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

Reply to
Heikki Leivo

I'll give this a shot, starting tomorrow. Thanks for the head start...it'll make it much easier for me.

Brian

Reply to
Brian Mears

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

Reply to
Brian Mears

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

-h-

Reply to
Heikki Leivo

PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.