Share via

Visual Basic Macro in SolidWorks.

Anonymous
2025-01-17T14:57:56+00:00

Hello everyone, I'm here in need of help for a macro in Solidworks that works, but is lacking a crucial check while working.

The macro simply saves the document (drawing) as a PDF. Simple right?

Well, it saves the file in the same location the file is being worked from, locally or in a network and with the same name but the ".pdf" extension.

So far so good.

The problem is access rights. usually you have rights to write the file, but depending on certain conditions you don't.

This is what happens:

  • If there is no PDF of that file in the target location, it creates the PDF and it finishes fine.
  • If there is a PDF file already, ( say you are updating changes to the drawing), then it overwrites the file but only IF you have access to it.

Here is the problem:

  • If the file exists in the location and it is READ ONLY, then the macro never finishes and hangs up.
  • This also hangs up SolidWorks, causing it to crash-hang.
  • Your only option is force quit SolidWorks and restart, loosing your changes.
  • If the program is hanged up and you try to manually force DELETE the read only file in Explorer, it also crashes Explorer.

The macro is only missing a step to check if the file is read only.

If it is, then is should just tell the user the file is read only and finish.

I have tried many ways to do this , but since I'm not trained at all in VB, I simply cannot figure it out...

I have cut and paste several other VB subs but I cannot make it work properly... having errors.

Here is the macro steps that work fine, (I recorded back in 2012) but it lacks the file checking for read only:

____________________________________________________________________________________________________

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

' Save a Part, Assembly or Drawing as PDF in the same directory.

' By: Adrian Crisci 02/01/2012

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

Dim swApp As SldWorks.SldWorks

Dim Part As SldWorks.ModelDoc2

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

On Error Resume Next

Dim FilePath As String

Dim PathSize As Long

Dim PathNoExtension As String

Dim NewFilePath As String

FilePath = Part.GetPathName

PathSize = Strings.Len(FilePath)

PathNoExtension = Strings.Left(FilePath, PathSize - 6)

NewFilePath = PathNoExtension & "pdf"

Part.SaveAs2 NewFilePath, 0, True, False

End Sub

__________________________________________________________________________________

Can someone add the missing steps to check if the file to be recorded is read only and then quit the macro with a " File is read only" message?

Then the user can go and take control of the file prior to re-writing over it using the macro.

Much appreciated, Thanks!

Adrian

Windows for home | Windows 10 | Files, folders, and storage

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Ramesh 176.2K Reputation points Volunteer Moderator
    2025-01-17T17:58:49+00:00

    Hi Adrian.

    Glad to assist.

    All the best,

    Ramesh

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2025-01-17T17:57:45+00:00

    Ramesh,

    Thank you for the heads up....

    Did not know where to start.... I'll post it there...

    Regards,

    Adrian

    Was this answer helpful?

    0 comments No comments
  3. Ramesh 176.2K Reputation points Volunteer Moderator
    2025-01-17T16:31:39+00:00

    Hi Adrian,

    Sorry, the Microsoft Community is a forum for home users. Due to the scope of your question, I suggest you ask this question in a VBA forum. 'VBA' Questions - Stack Overflow: https://stackoverflow.com/questions/tagged/vba

    Thanks,

    Was this answer helpful?

    0 comments No comments