30 Replies Latest reply on Oct 18, 2012 4:35 PM by Pat Lafontaine

    SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.

    Pat Lafontaine

      Hi,

      I have a macro that copy all sheet from a drawing and open new template and paste the sheets on. Then close old drawing and saveas the new one with the same name of the original one to overwrite it.

       

      The problem is that I cannot save after the paste is done. It works for a while but not anymore and I change nothing.

       

      Do you have any ideas ?

       

      here my code..It works well with the part and assy section.. Thank you

       

       

       

      Dim vSheetName As Variant
      Dim SheetCount As Integer
      Dim boolstatus As Boolean
      Dim longstatus As Long, longwarnings As Long
      Dim PART As Object
      Dim PARTTITLE As String
      Dim X As String
      Dim SWAPP As SldWorks.SldWorks
      Dim swModel As ModelDoc2

      Sub main()
      Set SWAPP = Application.SldWorks
      Set DOC = SWAPP.ACTIVEDOC
      If DOC Is Nothing Then MsgBox "A SOLIDWORKS DOCUMENT MUST BE OPEN" & Chr(13) & "TO PERFORM REFRESH THIS WAY!!": End
      Dim swDocTypeLong As Long
      Set PART = SWAPP.ACTIVEDOC

       

      X = PART.GetPathName


      PARTTITLE = PART.GetTitle
            
        Set PART = SWAPP.ACTIVEDOC
      Set swModel = SWAPP.ACTIVEDOC
      Set SWDWG = swModel
      Set swDraw = swModel
      vSheetName = swDraw.GetSheetNames

      SheetCount = PART.GetSheetCount
      SWDWG.ActivateSheet vSheetName(SheetCount - (SheetCount))
      PARTTITLE = PART.GetTitle
      boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 1), "SHEET", 0, 0, 0, False, 0, Nothing, 0)
      If SheetCount - 1 = 0 Then GoTo 8
      boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 2), "SHEET", 0, 0, 0, True, 0, Nothing, 0)
      If SheetCount - 2 = 0 Then GoTo 8
      boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 3), "SHEET", 0, 0, 0, True, 0, Nothing, 0)
      If SheetCount - 3 = 0 Then GoTo 8
      boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 4), "SHEET", 0, 0, 0, True, 0, Nothing, 0)
      If SheetCount - 4 = 0 Then GoTo 8
      boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 5), "SHEET", 0, 0, 0, True, 0, Nothing, 0)
      If SheetCount - 5 = 0 Then GoTo 8
      boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 6), "SHEET", 0, 0, 0, True, 0, Nothing, 0)
      If SheetCount - 6 = 0 Then GoTo 8
      boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 7), "SHEET", 0, 0, 0, True, 0, Nothing, 0)
      If SheetCount - 7 = 0 Then GoTo 8
      boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 8), "SHEET", 0, 0, 0, True, 0, Nothing, 0)
      If SheetCount - 8 = 0 Then GoTo 8
      boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 9), "SHEET", 0, 0, 0, True, 0, Nothing, 0)
      If SheetCount - 9 = 0 Then GoTo 8
      boolstatus = PART.Extension.SelectByID2(vSheetName(SheetCount - 10), "SHEET", 0, 0, 0, True, 0, Nothing, 0)
      If SheetCount - 10 > 0 Then MsgBox "DRAWING COUNTAIN MORE THAN 10 SHEETS," & Chr(13) & "ONLY 10 FIRST WILL BE COPY," & Chr(13) & "SO CHECK TO MANUALLY COPY MISSING SHEETS."

      8 PART.EditCopy
      Set PART = SWAPP.NewDocument("s:\aaatemplates\solidworks 2010 template\DRAWING.drwdot", 12, 0.2794, 0.4318)
      SWAPP.ActivateDoc2 "Draw7 - Sheet1", False, longstatus
       


      Set PART = SWAPP.ACTIVEDOC
      Dim myDrawingSheet As Object
      Set myDrawingSheet = PART.GetCurrentSheet()
      myDrawingSheet.SetName "SHEET TO DELETE"


      Set PART = SWAPP.ACTIVEDOC
      boolstatus = PART.Extension.SelectByID2("SHEET TO DELETE", "SHEET", 0, 0, 0, False, 0, Nothing, 0)
      PART.Paste
        PARTTITLE2 = PART.GetTitle
       
      SWAPP.CloseDoc PARTTITLE
      Set PART = SWAPP.ActivateDoc2(PARTTITLE2, 0, 0)
      'PART.Save2 (silent)
      Set PART = SWAPP.ACTIVEDOC
      longstatus = PART.SaveAs3(X, 0, 0)
         
      End Sub

        • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
          Artem Taturevych

          Check attached macro:

          ______________________

          Regards,

          Artem Taturevych

          Application Engineer at Intercad

          http://intercad.com.au/

          Tel: +61 2 9454 4444

            • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
              Pat Lafontaine

              Thank you for your reply! But same problem!

              If I perform saveas before paste, everything is all right!

              but after pasting, nothing happen!

              I dont know why! the first time a tried my macro, it have not work..but after some try, it have work..and later stop working..

              I dont know what the problem is!

              do you have an idea?

              thanks again!

                • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                  Artem Taturevych

                  Try to run this macro. It will display the message box with error type. Post it here and I will take a look.

                  Dim swApp As SldWorks.SldWorks

                  Dim swDraw As SldWorks.DrawingDoc

                  Dim swModel As SldWorks.ModelDoc2

                   

                   

                  Sub main()

                   

                   

                      On Error GoTo Final

                   

                   

                      Set swApp = Application.SldWorks

                     

                      Set swModel = swApp.ActiveDoc

                     

                      Set swDraw = swModel

                     

                      Dim vSheetNames As Variant

                      vSheetNames = swDraw.GetSheetNames

                     

                      Dim i As Integer

                     

                      swModel.ClearSelection2 True

                     

                      For i = 0 To UBound(vSheetNames)

                          swModel.Extension.SelectByID2 vSheetNames(i), "SHEET", 0, 0, 0, True, -1, Nothing, 0

                      Next

                     

                      swModel.EditCopy

                     

                      Dim title As String

                      Dim path As String

                     

                      title = swModel.GetTitle

                      path = swModel.GetPathName

                     

                      Dim template As String

                      template = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplateDrawing)

                     

                      Set swModel = swApp.NewDocument(template, 12, 0.2794, 0.4318)

                     

                      swModel.Paste

                     

                      swApp.CloseDoc title

                     

                      Dim lerr As Long

                      Dim lwarn As Long

                     

                      swModel.Extension.SaveAs path, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, lerr, lwarn

                     

                  Final:

                   

                      MsgBox err.Number & ";" & err.Description & ";" & lerr & "; " & lwarn

                     

                  End Sub

                   

                  ______________________

                  Regards,

                  Artem Taturevych

                  Application Engineer at Intercad

                  http://intercad.com.au/

                  Tel: +61 2 9454 4444

                    • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                      Pat Lafontaine

                      Thanks for helping me!

                                

                      The code from msgbox= 0;;1;0

                        • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                          Artem Taturevych

                          Hmmmm, this is the 'generic' error which says nothing to us. I wonder are you able to save the file manually? Try to run the macro than save this file. Try also to change the save as name so it is not overrides the existing file - it's just for test.

                           

                          ______________________

                          Regards,

                          Artem Taturevych

                          Application Engineer at Intercad

                          http://intercad.com.au/

                          Tel: +61 2 9454 4444

                            • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                              Pat Lafontaine

                              Ok..

                                         If I save manually, saveas window open normally and I m able to save.

                              If I change location, it save but the name of my drawing (on top of the window) do not update like if I still in the old drawing (drawn2-sheet1), but if I put a note with the filename inside, it give me the right name. but once I refresh, it gives me the not saved file name.

                               

                              when I go to see in the new folder location, I can see the drawing newly save but it look different, like if it have not been updated or something (the icon look different), and if I try to open it, it failed.

                               

                              Hope this can help!

                              Thanks

                                • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                  Pat Lafontaine

                                  in addition, I even try to save by using sendkey command, (open file menu, go down until save fonction, enter) but it does not work, except if it done before paste..

                                    • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                      Artem Taturevych

                                      Very strange. What SolidWorks version and SP are you using? Try to call previous version of save API: ModelDoc2::SaveAs.

                                       

                                      So what is your final goal? To show the updated title? What if you save as different name (which is working as I understand), close the file. Then delete the original file and rename new one. You can also set the title for new document ModelDoc2::SetTitle2 if this is the case.

                                      ______________________

                                      Regards,

                                      Artem Taturevych

                                      Application Engineer at Intercad

                                      http://intercad.com.au/

                                      Tel: +61 2 9454 4444

                                        • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                          Pat Lafontaine

                                          IT S 2010 SP0

                                          How do I call previous version? Nothing work (with new name or not). I must stop the macro and save manually. With a new name it make a file but I can not open it..It s like the save is incomplete.

                                            • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                              Artem Taturevych

                                              I'm sorry, Pat, we run into an issue which seems to be machine specific which is practically very hard to fix. I think you should contact API Support and request remote session so they can see what happens.

                                               

                                              How do I call previous version?

                                               

                                              Us swModel.SaveAs path instead of swModel.Extension.SaveAs in the macro I sent you previously.

                                               

                                              Have you tried this macro on other machines?

                                               

                                              ______________________

                                              Regards,

                                              Artem Taturevych

                                              Application Engineer at Intercad

                                              http://intercad.com.au/

                                              Tel: +61 2 9454 4444

                                                • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                                  Pat Lafontaine

                                                  I do try on other computer but the problem still there..Thanks for your help!

                                                  I ll check computer settings if I find something

                                                    • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                                      Artem Taturevych

                                                      Another one thing you could try. Do not open the file you are going to rewrite within this SolidWorks session (I suspect SolidWorks may not release the file when closing so it cannot be overwritten). Create a simple macro which just saves as the active document and rewrites destination file. Try this macro, what happens?

                                                      ______________________

                                                      Regards,

                                                      Artem Taturevych

                                                      Application Engineer at Intercad

                                                      http://intercad.com.au/

                                                      Tel: +61 2 9454 4444

                                                        • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                                          Pat Lafontaine

                                                          If I save as at any place before to paste it work perfectly. It seem to be something with the pasting, maybe solidworks wont release the file due to the paste that make a certain link between the files. Is there a way to force solidworks to release the file? And if it have work several times...What could have change in the parameters itselfs to provoque this?

                                                            • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                                              Artem Taturevych

                                                              Good point. This may be the case. Try to force release the document: ModelDoc2::ForceReleaseLocks.

                                                              ______________________

                                                              Regards,

                                                              Artem Taturevych

                                                              Application Engineer at Intercad

                                                              http://intercad.com.au/

                                                              Tel: +61 2 9454 4444

                                                                • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                                                  Pat Lafontaine

                                                                  I m not able to aply it.. how I can do that?

                                                                  sorry but i m a beginer!

                                                                    • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                                                      Michael Jeffries

                                                                      I apologize if I missed something skimming through this briefly, but couldn't the same thing be acheived by replacing the template in the drawing without having to create a new file and save it over the existing one?  I haven't looked at the API in depth for this, but I know it's rather easy to do within the solidworks drawing itself.

                                                                        • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                                                          Pat Lafontaine

                                                                          The goal is to completly reset the drawing. So I wish to open a new template and then copy the sheet from the old drawing, then the creation date and others property can be reset. If I could make it directly during pack and go it would be perfect but I think it s impossible to do it with sw 2010 api.

                                                                            • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                                                              Michael Jeffries

                                                                              I had gotten as far as copying the sheets from one drawing to a new one, but i quit after i realized it copied the old template in place of the new one (to check this, I created the new drawing from an A size template and the drawing i was copying from was B Size).  It seems to me in order to do what you are asking, you need not copy the sheet from one to another, but you have to copy the objects (views, annotations, etc...) which will make things tremendously more complex if I'm not mistaken, which is why I quit trying

                                                                                • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                                                                  Pat Lafontaine

                                                                                  yes, I understand, but if I can copy the shhet it should work.

                                                                                    • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                                                                      Michael Jeffries

                                                                                      So in the end result, you're not trying to change the Sheet Template, but rather just create a 'new' file?

                                                                                        • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                                                                          Pat Lafontaine

                                                                                          If I understand, yes. Changing the sheet template is not a problem. I just wish to create a new file (drawing), but I wish not to remake the drawing, so copying the sheet gives me that opportunity.

                                                                                            • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                                                                              Michael Jeffries

                                                                                              Well, I think i had what you wanted, but I deleted the project.. lol.  I'm trying to remember what the real hurdle was, but basically I created two ModelDoc2 references (old drawing, new drawing), two DrawDoc References, and two Sheet References and cycled through the first one using EditCopy to create the copy of the sheet to paste to the new ModelDoc2, then I closed the old ModelDoc2, but I didn't try the save over (the last thing you wanted it to do).  Have you gotten that far?

                                                                                                • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                                                                                  Pat Lafontaine

                                                                                                  yes! good idea to work with 2 references, it could work! I do pack and go parts and assy before to "refresh it", so that s the reason to overwrite the original file within the new folder.

                                                                                                    • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                                                                                      Michael Jeffries

                                                                                                      Ok Pat, try this out, I apologize for the lack of comments, I was just trying to get it to work, so I would recommend stepping through the debugger.  Also, note that I commented out the code that saves over the existing model at the end.  Please uncomment knowing that it will overwrite the original file !

                                                                                                       

                                                                                                      >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

                                                                                                          Dim swApp As SldWorks.SldWorks

                                                                                                          Dim swModel_Old As ModelDoc2

                                                                                                          Dim swDraw_Old As DrawingDoc

                                                                                                          Dim swSheet_Old As Sheet

                                                                                                          Dim swSheetCollection_Old As Variant

                                                                                                       

                                                                                                       

                                                                                                          Dim swModel_New As ModelDoc2

                                                                                                          Dim swDraw_New As DrawingDoc

                                                                                                          Dim swSheet_New As Sheet

                                                                                                          'Dim i As Integer

                                                                                                          Dim bool As Boolean

                                                                                                          Dim SheetWidth As Double

                                                                                                          Dim SheetHeight As Double

                                                                                                          Dim Papersize As Integer

                                                                                                          Dim TemplateName As String

                                                                                                          Dim Model_Old_Path As String

                                                                                                          Dim CustomInfoCount As Integer

                                                                                                          Dim CustomPropertyNames As Variant

                                                                                                         

                                                                                                          Dim cPropValues(1000, 1) As Variant

                                                                                                       

                                                                                                          Sub Main()

                                                                                                              Set swApp = CreateObject("Sldworks.Application")

                                                                                                              Set swModel_Old = swApp.ActiveDoc

                                                                                                              Model_Old_Path = swModel_Old.GetPathName

                                                                                                              Debug.Print Model_Old_Path

                                                                                                             'Set CustomPropertyNames = swModel_Old.Extension.CustomPropertyManager("@").GetNames

                                                                                                              CustomPropertyNames = swModel_Old.GetCustomInfoNames

                                                                                                             

                                                                                                             Return_CustomInfo

                                                                                                       

                                                                                                              Select Case swModel_Old.GetType

                                                                                                                  Case swDocumentTypes_e.swDocDRAWING

                                                                                                                      Set swDraw_Old = swModel_Old

                                                                                                                      swSheetCollection_Old = swDraw_Old.GetSheetNames

                                                                                                                      Set swSheet_Old = swDraw_Old.GetCurrentSheet

                                                                                                       

                                                                                                       

                                                                                                                  Case Else

                                                                                                                      swApp.SendMsgToUser ("Error! Open a DrawingDoc and try again!")

                                                                                                                      Exit Sub

                                                                                                              End Select

                                                                                                       

                                                                                                       

                                                                                                              Papersize = swSheet_Old.GetSize(SheetWidth, SheetHeight)

                                                                                                              TemplateName = swSheet_Old.GetTemplateName

                                                                                                       

                                                                                                              ' ************ Creates New Document ************

                                                                                                              Set swModel_New = swApp.NewDocument(TemplateName, Papersize, SheetWidth, SheetHeight)

                                                                                                       

                                                                                                              Set swDraw_New = swModel_New

                                                                                                              Set swSheet_New = swDraw_New.GetCurrentSheet

                                                                                                              swSheet_New.SetName ("DELETE")

                                                                                                              ' **********************************************

                                                                                                       

                                                                                                              swApp.ActivateDoc (swModel_Old.GetTitle)

                                                                                                       

                                                                                                              For i = 0 To swDraw_Old.GetSheetCount - 1

                                                                                                                  Debug.Print swSheetCollection_Old(i)

                                                                                                       

                                                                                                       

                                                                                                                  swDraw_Old.ActivateSheet (swSheetCollection_Old(i))

                                                                                                                  Set swSheet_Old = swDraw_New.GetCurrentSheet

                                                                                                                  swModel_Old.SelectByName swSelectType_e.swSelSHEETS, swSheetCollection_Old(i)

                                                                                                                

                                                                                                       

                                                                                                       

                                                                                                                  swModel_Old.EditCopy

                                                                                                       

                                                                                                       

                                                                                                                  swApp.ActivateDoc (swModel_New.GetTitle)

                                                                                                                  swModel_New.Paste

                                                                                                                  Set swDraw_New = swModel_New

                                                                                                                  Set swSheet_New = swDraw_New.GetCurrentSheet()

                                                                                                                  swSheet_New.SetName (swSheetCollection_Old(i))

                                                                                                       

                                                                                                       

                                                                                                       

                                                                                                       

                                                                                                                  swApp.ActivateDoc (swModel_Old.GetTitle)

                                                                                                       

                                                                                                       

                                                                                                              Next

                                                                                                              swApp.ActivateDoc (swModel_New.GetTitle)

                                                                                                       

                                                                                                       

                                                                                                       

                                                                                                       

                                                                                                       

                                                                                                       

                                                                                                       

                                                                                                       

                                                                                                              ' swDraw_New.ActivateSheet("DELETE")

                                                                                                              swModel_New.SelectByName swSelectType_e.swSelSHEETS, "DELETE"

                                                                                                              swModel_New.EditDelete

                                                                                                       

                                                                                                       

                                                                                                              swApp.CloseDoc (swModel_Old.GetTitle)

                                                                                                              swApp.ActivateDoc (swModel_New.GetTitle)

                                                                                                       

                                                                                                       

                                                                                                              SetNewModel_CustomInfo

                                                                                                              Dim SaveInt As Integer

                                                                                                              'SaveInt = swModel_New.SaveAs3(Model_Old_Path, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent)

                                                                                                       

                                                                                                       

                                                                                                          End Sub

                                                                                                       

                                                                                                      Sub Return_CustomInfo()

                                                                                                      i = 0

                                                                                                      Debug.Print UBound(CustomPropertyNames)

                                                                                                       

                                                                                                       

                                                                                                      For i = 0 To UBound(CustomPropertyNames)

                                                                                                          Debug.Print CustomPropertyNames(i) & ": " & swModel_Old.CustomInfo(CustomPropertyNames(i))

                                                                                                          cPropValues(i, 0) = CustomPropertyNames(i)

                                                                                                          cPropValues(i, 1) = swModel_Old.CustomInfo(CustomPropertyNames(i))

                                                                                                      Next i

                                                                                                      End Sub

                                                                                                       

                                                                                                       

                                                                                                      Sub SetNewModel_CustomInfo()

                                                                                                      For i = 0 To UBound(CustomPropertyNames)

                                                                                                          swModel_New.CustomInfo(cPropValues(i, 0)) = cPropValues(i, 1)

                                                                                                      Next i

                                                                                                      End Sub

                                                        • Re: SAVE AS PROBLEM! I M NOT ABLE TO SAVE AS AFTER COPY-PASTE.
                                                          Ivana Kolin

                                                          Is it an ordinary drawing? Made with pats and assy? Or something like this C:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\AutoCAD\7550-021.slddrw?

                                                           

                                                          Because with this one I have same problem as you.