Question about my macro

Hello

 

I created a macro using what I found, it must convert my MEP to pdf and save it in the folder of the year in which it was created, if the folder does not exist the create.

 

There she is:

 

Dim swApp As Object

 

Dim Part As Object

Dim FileNamePDF As String

 

Sub main()

Dim swApp As SldWorks.SldWorks

Dim SWmoddoc As SldWorks.ModelDoc2

Sun NumberPlan As String

Dim PathFile As String

Dim FileName As String

Dim NumberLeaf As String

Dim FileNamePDF As String

Dim PathFileNamePDF As String

Dim NameFolderPDF As String

Dim nErrors             As Long

Dim nWarnings           As Long

Set swApp = Application.SldWorks

Set SWmoddoc = swApp.ActiveDoc

 

'Retrieving the path and filename

Example: \\MERCURE\Sharing\xMethods\Public\DAO\Solidworks\2 - Drawing\2014\046-1-2014-A.SLDDRW

PathName = SWmoddoc.GetPathName

FilePath = Left(PathName, InStrRev(PathName, "\")) '\\MERCURY\Sharing\xMethods\Public\DAO\Solidworks\2 - Drawing\2014\

FileName = Right(PathName, Len(PathName) - InStrRev(PathName, "\")) '046-1-2014-A.SLDDRW

FileNamePDF = Right(FileName, 13) '2014-A.SLDDRW

FileNamePDF = Left(FileNamePDF, 4) '2014

FileNamePathPDF = "\\MERCURY\Sharing\xMethods\Public\DAO\PDF Plans\" & FolderNamePDF '\\MERCURY\Sharing\xMethods\Public\DAO\PDF Plans\2014\

 

'------------------------------------------------------------------

'Retrieving custom properties from the plan file

 

If SWmoddoc.GetType = swDocDRAWING Then check that we are on a plan file

    FileNamePDF = Replace(FileName, "SLDDRW", "pdf")

End If

 

'------------------------------------------------------------------

'Test if the folder with the year exists, if not create it

 

    If Dir$(FileNamePathPDF) = "" Then

        MkDir "FileNamePDF"

       End If

'------------------------------------------------------------------

'Test if the file already exists or confirm

'then recording

 

    If Dir$(FileNamePathPDF) = FileNamePDF Then 'The file already exists

        If MsgBox("The file: " & FileNamePDF & vbNewLine & " already exists. Do you want to replace him?", _

         vbOKCancel + vbExclamation) = vbOK Then

            nErrors = SWmoddoc.SaveAs(FileNamePathPDF)

        Else

            If MsgBox("PDF file was not created.", vbInformation) = vbOK Then Exit Sub '-------Message and EXIT-------

        End If

    Else

        If MsgBox("File: " & FileNamePDF & vbNewLine & " Is Going to Be Created", vbOKCancel + vbInformation) = vbOK Then

            nErrors = SWmoddoc.SaveAs(FileNamePathPDF)

        Else

            If MsgBox("PDF file was not created.", vbInformation) = vbOK Then Exit Sub '-------Message and EXIT-------

        End If

    End If

    

 

 

End Sub

 

 

I don't know how to test it and I would like to know before launching it if I haven't done something stupid because it plays with the folders of my company's network.

 

Thank you in advance.

Hello

 

To test a macro, the best thing to do is to put a breakpoint at the beginning and then press F8 to advance line by line.

It can also be interesting to put spies on the important variables.

 

 

See this link: http://www.tomshardware.fr/forum/id-1348092/tutoriel-excel-macro-vba-debogage.html

1 Like

The debug.print statement allows you to display the content of a variable in the execution window (which you display with CTRL G I think).

 

When I test my macros, I change the file paths to local paths, it allows me to check without using the network.

2 Likes

I changed the path to test.

 

So he creates the folder for me which does not exist on the other hand he does not create the pdf file, did you know or it could crash because the macro does not crash.

You have to check your conditions.

 

Make a stop on the line

 

nErrors = SWmoddoc.SaveAs(FileNamePathPDF)

 

Does the program stop there?

 

If not, are all the conditions OK? You can check with spies or debug.print

1 Like

Hello everyone,

 

To test a macro, the best is to put a breakpoint at the beginning and then press F8 to advance line by line as Lucas P says. Another solution is to enable the 'Debug' menu bar or better yet customize the macro editor by placing the 'Detailed Step' button (') in front of the 'Run' button () on the standard toolbar so as not to overload. > modus operandi > see attachment.

Otherwise, in the macro editor, "View" Local Window allows you to view the values of all variables when the macro is running.

 

 

Have a nice day.


personnaliser_lediteur_de_macro.png

Sorry, I don't understand what you're asking me to do, on the other hand as I was telling you it creates the folder if it doesn't exist and it creates the PDF but in the same folder as the SLDDRW I don't understand.

I hadn't delved into your code yet, I admit it!

 

I just saw that the following line is probably wrong:

 

        MkDir "FileNamePDF"

 

1) It seems to me that to create a directory, you have to give the full name with the path!

2) You don't need quotation marks to your variable "FileNamePDF", otherwise it's no longer a variable but text!

 

A convenient way to check and read VBA code is to use Notepad++  found here:

http://notepad-plus-plus.org/download/v6.6.7.html

 

Then paste the code into a new file and choose from the menu Language > V > VB.

 

The color formatting is much more telling than the one in the basic VBA editor.

Variables appear in black while text appears in gray.

2 Likes

I modified the variable in the SWmoddoc.SaveAs(PathFileNamePDF), I had put the full address with the file, it didn't work I just put the name of the pdf file and it creates it for me in the same folder as the slddrw, and something else if the folder in which I want to save the pdf already exists, This bug, can someone help me understand here is the text of my macro.


sans_titre.png

Hello

 

I think the error comes from the "PathPDFfileName" variable in this command:

nErrors = SWmoddoc.SaveAs(FileNamePathPDF)

 

When you save with the "SaveAs" command, you have to put the full name of the file as an argument, WITH the extension (.pdf in your case).

 

Kind regards.

 

Franck.

I saw this after Lucas, I modified it, but it still doesn't work.

Fgirard, I also saw this, I modified it but still doesn't work entirely.

For the breakpoints, look at the link I put above:

http://www.tomshardware.fr/forum/id-1348092/tutoriel-excel-macro-vba-debogage.html

 

This explains several ways to debug code, including breakpoints.

 

1 Like

If it still doesn't work, add spies as shown here:

http://www.tomshardware.fr/forum/id-1348092/tutoriel-excel-macro-vba-debogage.html

 

And what are the values of the variables:

NameFilePDF 

PathNameFilePDF

 

 

Where does your macro end?

1 Like

re

 

To help test a macro > in the equation editor, click on "View", on "Local Variable Window", > see attachment


editeur_de_macro_fenetre_variables_locales.png
2 Likes

Our posts intersect, sorry.

 

It's weird that "SaveAs" doesn't work properly with the full file name (path+name+extension).

Same for the bug with the folder that already exists.

 

Try with the "SaveAs" command for ModelDocExtension. This is the one I use in my macros.

Here's how to report:

   Dim SWmodext as As SldWorks.ModelDocExtension

   Dim swExportPDFData     As SldWorks.ExportPdfData

Dim Errors              As Long   
   Dim Warnings            As Long

and how to affect:

   Set SWmodext = SWmoddoc.Extension

then:

   nErrors = SWmodext.SaveAs(FileNamePathPDF, 0, 0, swExportPDFData, Errors, Warnings)

 

 

 

For folder management, I use the FileSystemObject:

Dim oFSO                As Scripting.FileSystemObject   
Dim oFld                  As Folder   

 

   Set oFSO = New Scripting.FileSystemObject

 

   If oFSO.FolderExists(FileNamePathPDF) = False Then
        Set oFld = oFSO.CreateFolder(FileNamePathPDF)
    End If

2 Likes

Thank you caronmaxime, very useful this window!

 

So with your window, my variables have the value I want to give them, but it doesn't work the way I want it to.

 

he will create the 2014 file for me, on the other hand if the file already exists it stops at the Mkdir line, (1st point that I don't understand)

 

and he creates the pdf file for me, but on the other hand in the same folder as the active file when he should put it in the 2014 folder (2nd point that I don't understand)

 

HELP!!

1 Like

Wouldn't the error come from the fact that you test a folder and create another folder?

 

If Dir$(FileNamePathPDF) = "" Then
MkDir FileNamePDF

End if

 

PathFileNamePDF or should be used here 2 times, right? Once for the Dir$ test and once for the MkDir?

3 Likes

re re

 

Mathieu, in my opinion your   [ MkDir  FileNamePDF]  should be replaced by    [MkDir FileNamePathPDF]

 

In your macro editor, you can double-click on the MkDir keyword to select it and press F1. Help will allow you to understand.

re re re

 

take stock taking into account the excellent recommendations of Fgirard and maybe take a look at the question "my PDF DXF macro what to say? " that I published.

 

Otherwise this is how I proceed (see attachment) to debug a macro when it creates folders or files


capture_decran_sw_et_editeur_sw_et_explorer_windows.png