14 Replies Latest reply on Oct 9, 2018 9:34 AM by Clark Honzik

    Active document change notification

    Sergio Monti

      Hi all, I'm trying to write a macro to create a file log that records every change of active document.

      I would like to write to a file a list of date / time / filename of the active document (part/assembly/drawing).

      Therefore the macro should trigger a function that write to a file every time the active document chages.

      Following the suggestions I found on the following link

      https://www.lynkoa.com/sites/default/files/questions/answer/21/04/2017/using_notifications_events_in_solidworks_macros.p…

      I created the following macro:

       

      Main Module:

           Sub main()

               Dim MyClass As New Notification_Class

               MyClass.MonitorSolidWorks

           End Sub

       

      Class Module: (Notification_Class)

           Option Explicit

           Dim WithEvents swApp As SldWorks.SldWorks

           Dim MyModel As SldWorks.ModelDoc2

      -------------------------

           Public Sub MonitorSolidWorks()

               Set swApp = Application.SldWorks

           End Sub

      --------------------------

           Private Function swApp_ActiveDocChangeNotify() As Long

               Dim Time As Date

               Dim FileName As String

               ' Get active document

               Set MyModel = swApp.ActiveDoc

               ' Open output file for writing document change log file

               Open "c:\temp\swActiveDocLog.txt" For Output As #1

               FileName = MyModel.GetPathName

               Time = DateTime.Date & DateTime.Time

               '  Write to output file

               Write #1, Time & "Active doc: " & FileName

               ' Close the output file

               Close #1

           End Function

       

      I get no results. It seems the function will not run when the active document changes.

      What's wrong?

      Thanks in advance for your help.

        • Re: Active document change notification
          Jim Sculley

          Your Main sub exits which ends the macro.  You need to put it into a loop which will still process events. You can add the following to the end of your Main function:

           

          Do While True
              DoEvents
          Loop
          

           

          Once you do that, you will find a bug in your code.  Your 'Time' variable needs to be declared as a String not a Date.  Also, you code simply overwrites the file each time so the only thing in the file is the most recent document that was active.  If you want a log of all the files, you need to append to the file:

           

          Open "c:\temp\swActiveDocLog.txt" For Append As #1
          
          • Re: Active document change notification
            Deepak Gupta

            Sergio, try the attached one but you should also use/try what has been suggested by Jim

            • Re: Active document change notification
              Sergio Monti

              Thanks to everybody for your help, as usual.

              I modified my macro by putting 'Open'  and 'Close' in main module and replacing 'Open' statement with 'Append' in the class module and solving the issues with string / date.

              It worked.

              I also replaced the 'Write' command with 'Print' to get rid of the quotation marks in the output text file.

              Let me apply some other little changes and I will share it to know your opinion.

              • Re: Active document change notification
                Sergio Monti

                I finished my macro eventually.

                I also added a statement to check if the file already exists. In that case will add records to it instead of creating a new one.

                The macro works as I was expected to do. See a screenshot below.

                The blank filename relates to a 'new document' still not saved.

                I also added the "/m" command to run the macro when starting SolidWorks in the shortcut

                "C:\Program Files\SolidWorks Corps\SOLIDWORKS(2)\SLDWORKS.exe" /m "...***Macro folder location***...\ActiveDocFileLog.swp"

                I think in future I will create a single file per month instead of a file per day.

                I tried to import the output text file in an Excel spread sheet and it's pretty straightforward to work out the time spent on each project by adding simple formulas to the cells.

                It's a another simple alternative option to Tracking time for Project . It's not 100% reliable for tracking time for projects (btw, as far as I'm concerned, it's not possible to achieve it) but it's something that may come useful.

                I will mark Jim's answer as correct because he was the first and I solved the problem with it.

                Thanks to Deepak and Josh, I really appreciated your help too.

                I share the macro. Any comments and remarks are welcome.

                • Re: Active document change notification
                  John Stoltzfus

                  Sergio Monti

                   

                   

                  Would it be possible to edit the macro to file the txt report in the same file as the document, is it either Part/Assembly or Drawing file??

                   

                  Thanks

                    • Re: Active document change notification
                      Sergio Monti

                      I think so.

                      That's a good idea actually.

                      The output filename is a string, so you should replace "c:\temp\" with a string variable, i.e. 'OutputPath'  that contains the full path of the document minus the file name.

                      Something like:

                      OutputPath = left(MyModel.GetPathName, length(MyModel.GetPathName) - lehgth(MyModel.GetTitle)

                       

                      I'm sorry I have to leave now, I will try it tomorrow.

                      • Re: Active document change notification
                        Sergio Monti

                        Hi John,

                        Here is the modified macro.

                        It creates a txt file in the same folder of the active document file. If you have 1000 files in the projects, it creates 1000 txt files!!!!

                        Did you mean to attach it to the sldworks file inside Design Binder folder? I don't know if it's possible to do it via Macro.

                        You can easily modify the path and file name playing with strings to create a single output file at project level.

                        Unfortunately it is not very useful for tracking project time , as it gives the time log of when the document has been activated but doesn't tell when the user skipped to another document/project.

                          • Re: Active document change notification
                            Josh Brady

                            Sergio Monti wrote:

                             

                            Hi John,

                            Here is the modified macro.

                            It creates a txt file in the same folder of the active document file. If you have 1000 files in the projects, it creates 1000 txt files!!!!

                            Did you mean to attach it to the sldworks file inside Design Binder folder? I don't know if it's possible to do it via Macro.

                            You can easily modify the path and file name playing with strings to create a single output file at project level.

                            Unfortunately it is not very useful for tracking project time , as it gives the time log of when the document has been activated but doesn't tell when the user skipped to another document/project.

                             

                            Super easy to tweak it to report both activating and deactivating.  Definitely misses deactivation due to crashing, probably misses deactivation due to closing out SW as well...

                             

                            Sorry, not debugged at all, but you should get the idea...

                        • Re: Active document change notification
                          Clark Honzik

                          Just a thought.  Instead of writing to text files you could write this to a Microsoft SQL database.  If you don't have SQL you can get SQL Express for free from Microsoft.  If you rewrite your macro in VSTA instead of VBA you will find easy methods to connect to SQL using .net.  You can find endless examples of writing to SQL via .net online(Google).  Just a thought if your feeling daring or if your feeling really adventurous create a SolidWorks addin.  You will need Microsoft Visual Studio for that.