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?
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?
re
To help test a macro > in the equation editor, click on "View", on "Local Variable Window", > see attachment
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
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!!
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?
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
I made the changes but it still does the same, and when I restart it here or it bugs!
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
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.
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!
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).
And for my message of 15:47, it was about the creation of the folder, not the saving of the PDF:
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
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+
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
Perfect Lucas, it works!!
THANK YOU ALL FOR YOUR HELP!!!!