Question about my macro

I made the changes but it still does the same, and when I restart it here or it bugs!

 

 


sans_titre.png

Ah I think it's because you don't have the backslash at the end of your path!!

 

To verify this, add this:

 

If right(FileNamePathPDF,1) = "\" then

FileNamePathPDF = FileNamePathPDF & "\"

End if

 

You have to put it before your line:

If Dir$(FileNamePathPDF) = "" Then

1 Like

No lucas, it's the same I didn't do it like you said, I did it like that.

 

FileNamePathPDF = "C:\ANGER\pdf\" & FileNamePDF & "\"

 

But that doesn't change anything.

 

On the other hand, how does it work?

nErrors = SWmoddoc.SaveAs(FileNamePDF)    FileNamePDF=045/1-2014-A

 

 

What should we have in parentheses?

I don't find it and I don't really understand what's on the internet.

As mentioned above, your PDFFileName variable must contain the file extension!

So for you.pdf

And maybe the path too?

 

Try both.

I don't have the possibility to test it currently.

1 Like

I tried the 2 knowing that the variable "FileNamePDF" has the extension originally, but it remains the same, it saves it in the folder where the file is active.

So you didn't try to include the destination path (where you want to save it) in FileNamePDF?

Because a "save as" without the path saves it in the folder where the original part is!

 

 

1 Like

It will be:

nErrors = SWmoddoc.SaveAs(FileNamePathPDF & FileNamePDF)

 

By making sure that there is a backslash between the 2 (in the first variable or the second, it doesn't matter).

1 Like

And for my message of 15:47, it was about the creation of the folder, not the saving of the PDF:

 

Lucas P

July 15, 2014 - 03:47 PM

Ah I think it's because you don't have the backslash at the end of your path!!

 

To verify this, add this:

 

If right(FileNamePathPDF,1) = "\" then

FileNamePathPDF = FileNamePathPDF & "\"

End if

 

You have to put it before your line:

If Dir$(FileNamePathPDF) = "" Then

1 Like

re re re re

 

I'm afraid to insist but Fgirard's advice is to be followed both for the use of [Scripting.FileSystemObject] and [SaveAs].

 

Well now we can persist in using Dir but we have to make the beast a good mess, make the distinction between files and folders !!

 

Your snippet of code:

'Test if the folder with the year exists, if not create it
    If Dir(PathFileNamePDF) = "" Then
        MkDir PathFileNamePDF
    End If

 

I translate: If there is no file at the address [PathFileNamePDF] then call MkDir to create the last subfolder contained in the address [PathFileNamePDF].

 

Your test is not the right one so MkDir can crash if the last subfolder of the address exists (runtime error 75).

MkDir also crashes if the second-to-last subfolder of the address does not exist (runtime error 76).

MkDir also crashes if the drive, [R:] for example, is not available, if ...

On the other hand, MkDir is insensitive to whether or not there is a [\] at the end of the address.

 

a slightly modified program from the VBA help (select Dir and press F1 to get the help):

 

' Displays in the execution window (Ctrl+G) the names of the folders present at [C:\A\]
' and if one of these folder names is "2014" then the program stops
Dim MyFile, MyPath, MyName
MyPath = "C:\A\"    ' Defines the path.
MyName = Dir(MyPath, vbDirectory)    ' Gets the first entry.
Do While MyName <> ""    ' Starts the loop.
    ' Ignores the current folder and the
    ' containing the current folder.
    If MyName <> "." And MyName <> ".. " Then
        ' Uses a bit-level comparison to verify that MyName is a folder.
        If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
            Debug.Print MyName    ' Displays the entry only if it represents a folder.
            If MyName = "2014" Then Stop 'the '2014' folder exists!!
        End If
    End If
    MyName = Dir    ' Gets the following entry.
Loop

 

 

this snippet of code is advantageously replaceable by (see Fgirard Remarks):

 

 Dim oFSO                As Scripting.FileSystemObject
 Dim oFld                  As Folder

 Set oFSO = New Scripting.FileSystemObject

If oFSO.FolderExists(" "c:\A\2014") = True Then
    Stop

End If

 

OR even simpler and unsurprising

 

Sun fs
Set fs = CreateObject("Scripting.FileSystemObject")

If fs. FolderExists("c:\A\2014") Then
    Stop
End If

 

 

Remains to be seen for your [SaveAs]

 

I just tried this:

 

  Dim swApp       As SldWorks.SldWorks
  Dim swModel   As SldWorks.ModelDoc2

  Sun Rep           As Boolean

  Set swApp = Application.SldWorks
  Set swModel = swApp.ActiveDoc
  Dir = swModel.SaveAs("C:\A\toto.pdf") ' "     C:\A" should exist, if "toto.pdf" exists then it will be automatically overwritten

 

And it works very well. >> When debugging, start with text in parentheses (here "C:\A\toto.pdf"). If it works then Dir returns True. Then, you can try to substitute your text with a variable (here a variable declared as a string).

 

 

A+

 

 

 

2 Likes

Hello to you,

 

So I tried a little bit what you gave me Caronmaxime

 

Sun fs
Set fs = CreateObject("Scripting.FileSystemObject")

If fs. FolderExists("c:\A\2014") Then
    Stop
End If

 

On the other hand, I left my registration phase by putting the full address.

 

AND IT WORKS !!!

 

on the other hand and yes there is just a problem, if the folder exists you have marked "STOP" I have to mark what so that it does not recreate the folder and that it saves normally.

 

I'm attaching my code.

 

Sun fs

Set fs = CreateObject("Scripting.FileSystemObject")

If fs. FolderExists(FileNamePathPDF) Then

    Stop

End If

       MkDir (FileNamePathPDF)

 

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

'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 + FileNamePDF)

        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 + FileNamePDF)

        Else

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

        End If

    End If

    

End Sub

We must test the opposite condition:

If fs. FolderExists(FileNamePathPDF) Then

 

Else

       MkDir (FileNamePathPDF)

End If

2 Likes

Perfect Lucas, it works!!

THANK YOU ALL FOR YOUR HELP!!!!