-
Re: Macro Automatic Start when a Drawing is Opened
Alex Burnett Aug 23, 2017 1:47 PM (in response to John Vincent)This got it working for me. I don't know how to run the macro when SW is open so you'll have to run the macro manually. Once it's running though, you can open and close several files and it will give you a message each time telling you what you opened.
You were on the right track for sure. You were just missing the code in your main() function to create an instance of the event listener class module you made.
Module 1:
Option Explicit Dim eventListener As Class1 Sub main() Set eventListener = New Class1 End Sub
Class Module (Class1):
Option Explicit Private WithEvents swApp As SldWorks.SldWorks Private Sub Class_Initialize() Set swApp = Application.SldWorks End Sub Private Function swApp_FileOpenNotify(ByVal FileName As String) As Long Dim msgString As String Dim msgStatus As Integer msgString = "File " & FileName & "was opened." 'swApp.SendMsgToUser msgString msgStatus = swApp.SendMsgToUser2(msgString, swMbInformation, swMbOk) End Function
-
Re: Macro Automatic Start when a Drawing is Opened
Christian Chu Aug 23, 2017 2:17 PM (in response to Alex Burnett)I don't know how to run the macro when SW is open so you'll have to run the macro manually. Once it's running though, you can open and close several files and it will give you a message each time telling you what you opened.
Yes, please see the thread - run macro at start up SW
or it might be easier with an addin
-
Re: Macro Automatic Start when a Drawing is Opened
John Vincent Aug 23, 2017 3:52 PM (in response to Christian Chu)Thank you, Christian. I am familiar with the thread you noted and plan to use it once I get the macro to work in the VBA environment.... My final objective is to write a script to update properties when drawings with weldments are opened. This is just a test to see how to get a script to run when a file is a opened.
-
-
Re: Macro Automatic Start when a Drawing is Opened
John Vincent Aug 23, 2017 3:52 PM (in response to Alex Burnett)Thank you, Alex. In VBA, I renamed Module1 to Auto_open1 (I wanted to use AutoOpen but VBA wouldn't allow it). I renamed Class1 to NewDocOpen. So where you have eventListener in your code, I have AutoOpen in my code, and where you have Class1, I have NewDocOpen in my code. So our code should be equivalent. It's working for you, but you indicated you're not in running it with SolidWorks open. (?!) I have no idea how to do that. Could you explain how to run it that way?
I run it in SolidWorks by using Tools > Macro > Edit with a part or drawing open, and navigating to the .swp file for the project. (For a new project use Tools > Macro > New) Can you see if it works for you when you use it that way?
What version of SolidWorks are you running. (I'm using 2016 SP5.)
-
Re: Macro Automatic Start when a Drawing is Opened
Alex Burnett Aug 23, 2017 4:12 PM (in response to John Vincent)I am also using 2016 SP5.0. I suppose I was not terribly clear with what I meant. I am running this with SW open, however I didn't know how to get it to automatically start the macro when I open a session of SolidWorks. I tried out Christian's suggestion and edited the target of a SW shortcut on my desktop to change the target of the file by adding the "/m" "path to macro" to the text field. Now when I open SW using that shortcut, it automatically starts the macro with it. I can open a drawing or part from there and it pops up a window.
-
Re: Macro Automatic Start when a Drawing is Opened
John Vincent Aug 24, 2017 12:25 PM (in response to Alex Burnett)Alex, I thought our macros were equivalent. But in checking closely I found I was missing the
Set swApp = Application.SldWorks
statement in the Class_Initialize() procedure in the Class Module.
So I've got it working now. Thanks for your help.
-
-
-
-
Re: Macro Automatic Start when a Drawing is Opened
Christian Chu Aug 23, 2017 2:02 PM (in response to John Vincent)would you please tell us what you want to do after firing the event, open the dwg?
-
Re: Macro Automatic Start when a Drawing is Opened
John Vincent Aug 23, 2017 3:52 PM (in response to Christian Chu)Yes. I open a drawing or part file after running the macro.
-
-
Re: Macro Automatic Start when a Drawing is Opened
Aaron Larson Aug 23, 2017 2:52 PM (in response to John Vincent)I had the same question awhile back and found this post with a solution from Keith Rice. It may help you. Been awhile but if I remember correctly it adds a macro feature to the drawing tree which you can then map to some macro. So when you open/rebuild the drawing it runs the macro feature. I did not 100% verify it but I did run it and it did insert the macro feature to my recollection.
-
Re: Macro Automatic Start when a Drawing is Opened
Keith Rice Aug 23, 2017 3:12 PM (in response to Aaron Larson)Macro features can replace equation triggered macros (ETMs) in that macro features will run when the model is rebuilt, however they will NOT run when the model is opened. That was what made ETMs unique --- since equations got run when a document opened, you could run your code right then. Macro features require the user to rebuild the model.
Keith
-
Re: Macro Automatic Start when a Drawing is Opened
Aaron Larson Aug 23, 2017 3:17 PM (in response to Keith Rice)Thanks for the clarification Keith - I knew I tagged the right guy.
-
-
Re: Macro Automatic Start when a Drawing is Opened
John Vincent Aug 23, 2017 3:52 PM (in response to Aaron Larson)Thank you, Aaron. It looks like an interesting option.
-
-
Re: Macro Automatic Start when a Drawing is Opened
Keith Rice Aug 23, 2017 3:14 PM (in response to John Vincent)John,
Are you saying that you want a macro embedded in a specific drawing document that runs any time that document is opened on any computer? Or are you saying that on a given computer, any time a drawing document is opened, you want a certain macro to run?
Eqn Triggered Macros were intended for the first case but, as you noticed, they no longer work. If you're looking for the latter, then you can set a macro to run when SolidWorks opens (and then have that macro listen for a document opening), but that's a rather frail solution as its dependent on SolidWorks being run from a shortcut you created.
What I would strongly recommend is that you write an addin, which can be set to always load with SolidWorks, regardless of how SolidWorks was opened. Addins are, in general, more robust than macros, anyway, but they are more complicated to write. If you're new to addins and/or .NET but agree that this is the best way to go for the long term, email me and I'll give you some low cost options on how we can get you started down that path.
Keith
-
Re: Macro Automatic Start when a Drawing is Opened
John Vincent Aug 23, 2017 3:52 PM (in response to Keith Rice)Thank you, Keith. What I would like is the first case - a macro embedded in a specific drawing document. My supervisor said he knew there was a way to do it and asked me to look into it. We are a ***very*** small company with limited resources (no training budget), so I think they'll want to stick to a manual procedure for now. I will definitely keep this in mind and will contact you if there is an opportunity to go that route.
-
Re: Macro Automatic Start when a Drawing is Opened
Alex Burnett Aug 23, 2017 3:56 PM (in response to Keith Rice)1 person found this helpfulThis is exactly what I did when I realized there was a lot of functionality that I wanted out of SolidWorks that macros didn't make sense. You'd be surprised what you can do with some Google ingenuity and a little c# programming know how.
-