Report OutputTo to PDF format In Seperated folder By month then by Day

Falcon88

Registered User.
Local time
Today, 21:36
Joined
Nov 4, 2014
Messages
309
hiiii all dears
i have code to save my report to pdf ,
my report contains a field "OrderDate" , i need to save reports of every year in seperated folder inside the Main Folder (MyPath) by the number of the year and inside every Year folder months folders that contains day folders like: 2015 -- January 2015 -- 01 January 2015 -- inside it PDF Files of this day.

i have this code:
Code:
Dim myPath As String
Dim myFileN As String
myPath = "C:\Users\QSH\Desktop\ultrasonography\"

myFileN = Me.PtName & " - " & Format(Me.OrderDate, "dddd , dd  mmmm  yyyy") & ".pdf"

DoCmd.OpenReport "MainRpt", acViewPreview, , "OrderID = " & OrderID, acHidden

DoCmd.OutputTo acOutputReport, "MainRpt", acFormatPDF, myPath & myFileN, True

DoCmd.RunMacro "RptClose"

what is the best way to save my reports inside that folders for every day.
 
when you have only one report I should prefer yyyymmddfilename.pdf in 1 directory
easy to sort.

Ben
 
when you have only one report I should prefer yyyymmddfilename.pdf in 1 directory
easy to sort.

Ben

please give me the code that could create folders and subfolders
 
please give me that code or an example to create folders and subfolders
 
VBA.MkDir "pathName", example:

will create arnelgp on C: drive (provided that i have the right to create).

VBA.MkDir "C:\arnelgp"

create subdir myFolder on C:\arnelgp"

VBA.MkDir "C:\arnelgp\myFolder"

you must test first if the directory already exists before creating it, otherwise an error will occur. to test:

If Len(Dir("C:\arnelgp\myFolder")) = 0 then
'subfolder does not exits
'create one
VBA.MkDir "C:\arnelgp\myFolder"
End If
 
Hello Falcon88

This is the code MKDIR and the usage is pretty straightforward.
Is there a question about the links I sent you?
 
Ok
how to use format fun. to format OrderDate firstly to create Year folder and than Inside format OrderDate to create month folder,
 
vba.mkdir ("c:\" & Format(Date(), "yyyy"))

this will create a folder in c: drive with current year, ie. 2016.

vba.mkdir ("c:\" & Format(Date(), "yyyy") & "\" & Format(Date(),"mm")

will create sub folder with month number, ie 01 (for january), 02 (for feb)

vba.mkdir ("c:\" & Format(Date(), "yyyy") & "\" & Format(Date(),"mmm")

will create sub folder with month name (first three letter), ie: jan, feb, mar, ...

vba.mkdir ("c:\" & Format(Date(), "yyyy") & "\" & Format(Date(),"mmmm")

will create sub folder with full month name, ie: january, february, march, ...
 
i try this code , but gives me wrong:
Code:
Private Sub aPDFCopy1_Click()
Dim MyMonth As String
Dim myPath As String
Dim myFileN As String

myPath = "c:\Users\QSH\Desktop\ultrasonography\"
If Len(Dir("c:\Users\QSH\Desktop\ultrasonography")) = 0 Then
'subfolder does not exits
'create one
VBA.MkDir myPath & Format(Me.OrderDate, "yyyy") & "\" & Format(Me.OrderDate, "mmmm")
End If
myFileN = Format(Me.OrderDate, "dddd dd mmmm yyyy") & " - " & Me.PtName & ".pdf"

DoCmd.OpenReport "MainRpt", acViewPreview, , "OrderID = " & OrderID, acHidden

DoCmd.OutputTo acOutputReport, "MainRpt", acFormatPDF, myPath & myFileN, True

DoCmd.RunMacro "RptClose"
End Sub
 
i try this code and other wrong :
Code:
Dim myPath As String
Dim myYear As String
Dim MyMonth As String
Dim myFileN As String

myPath = "c:\Users\QSH\Desktop\ultrasonography\myYear\MyMonth"
myYear = Format(Me.OrderDate, "yyyy")
MyMonth = Format(Me.OrderDate, "mmmm")
myFileN = Format(Me.OrderDate, "dddd dd mmmm yyyy") & " - " & Me.PtName & ".pdf"
If Len(Dir("c:\Users\QSH\Desktop\ultrasonography\& myYear &" \ "")) = 0 Then
MkDir ("c:\Users\QSH\Desktop\ultrasonography\& myYear &" \ "")
End If
If Len(Dir("c:\Users\QSH\Desktop\ultrasonography\myYear\& MyMonth &" \ "")) = 0 Then
MkDir ("c:\Users\QSH\Desktop\ultrasonography\myYear\& MyMonth &" \ "")
End If

DoCmd.OpenReport "MainRpt", acViewPreview, , "OrderID = " & OrderID, acHidden

DoCmd.OutputTo acOutputReport, "MainRpt", acFormatPDF, myPath & myFileN, True

DoCmd.RunMacro "RptClose"


this give me run error 13 .
 
Last edited:
i try this code and works good to create year folder only :
Code:
Dim strFolderName As String
Dim myFileNx As String
strFolderName = CurrentProject.Path & "\" & fails & "\" & Format(Me.OrderDate, "yyyy") & "\"      ' folder to save
 If Len(Dir(strFolderName, vbDirectory)) = 0 Then
     MkDir (strFolderName)
 End If
myFileNx = Format(Me.OrderDate, "dddd dd mmmm yyyy") & " - " & Me.PtName & ".pdf"
DoCmd.OpenReport "MainRpt", acViewPreview, , "OrderID = " & OrderID, acHidden
DoCmd.OutputTo acOutputReport, "MainRpt", acFormatPDF, strFolderName & myFileNx, True
DoCmd.RunMacro "RptClose"

but when i try to chang it to create month folder inside year folder like this:
Code:
Dim strFolderName As String
Dim myFileNx As String
strFolderName = CurrentProject.Path & "\" & fails & "\" & Format(Me.OrderDate, "yyyy") & "\" & Format(Me.OrderDate, "mmmm yyyy") & "\"       ' folder to save
 If Len(Dir(strFolderName, vbDirectory)) = 0 Then
     MkDir (strFolderName)
 End If
myFileNx = Format(Me.OrderDate, "dddd dd mmmm yyyy") & " - " & Me.PtName & ".pdf"
DoCmd.OpenReport "MainRpt", acViewPreview, , "OrderID = " & OrderID, acHidden
DoCmd.OutputTo acOutputReport, "MainRpt", acFormatPDF, strFolderName & myFileNx, True
DoCmd.RunMacro "RptClose"

it gives me run time error 76 "Path not found"

the last code works good if the year folder exists.


please help me to make that code (last one) works good.
 
i try this code but gives me error 2501

Code:
Dim strFolderYear As String
Dim strFolderMonth As String
Dim myFileNx As String
strFolderName = CurrentProject.Path & fails & "\" & Format(Me.OrderDate, "yyyy") & "\"    ' folder to save
strFolderMonth = strFolderName & Format(Me.OrderDate, "mmmm yyyy") & "\"      ' folder to save
 If Len(Dir(strFolderYear, vbDirectory)) = 0 Then
     MkDir (strFolderYear)
     If Len(Dir(strFolderMonth, vbDirectory)) = 0 Then
     MkDir (strFolderMonth)
     End If
 End If
myFileNx = Format(Me.OrderDate, "dddd dd mmmm yyyy") & " - " & Me.PtName & ".pdf"
DoCmd.OpenReport "MainRpt", acViewPreview, , "OrderID = " & OrderID, acHidden
DoCmd.OutputTo acOutputReport, "MainRpt", acFormatPDF, strFolderMonth & myFileNx, True
DoCmd.RunMacro "RptClose"
 

Users who are viewing this thread

Back
Top Bottom