Print Page | Close Window

PDF File Name Name

Printed From: www.exp-systems.com
Category: PDF reDirect
Forum Name: Programming
Forum Discription: VBA and Batch Tools to control PDF reDirect Pro
URL: http://www.exp-systems.com/Forum_exp/forum_posts.asp?TID=374
Printed Date: 22 Nov 24 at 8:27AM


Topic: PDF File Name Name
Posted By: Gene
Subject: PDF File Name Name
Date Posted: 26 Dec 07 at 10:17AM
I purchased the pro version a couple of days ago.  I am creating PDF files from MS Access reports and everything seems to work fine with the exception the name of the PDF file that is created.  Even though I would change the file name property to the file name that I wanted it would create the PDF with the name of the report so I changed everything to work that way and just renamed the file at the end of the code.
 
This worked well with several reports and then for no reason that I can understand it named the PDF the name of a report that I had run prior to that report (like it had stored the name somewhere).  Has anyone seen anything similar to this and have a fix?
 
Thanks,



Replies:
Posted By: Michel_K17
Date Posted: 26 Dec 07 at 11:13AM
I'll take a look to see if I can duplicate the problems and fix them. Thanks for letting me know. It'll probably be late today before I have an update.

Cheers!



-------------
Michel Korwin-Szymanowski
EXP Systems LLC


Posted By: Michel_K17
Date Posted: 27 Dec 07 at 12:21AM
Hi,

    I think I figured outwhat is going on: are you trying to create multiple PDF reports very quickly (ie in quick succession)?If so, then it is possible that the next print job sets the printer name of the previous job before the current print job has completed.

    In the module, there is a line that reads:
TempBool = CheckFileCreated(MyOutputPath & "\" & MyOutputFilename)

    If you do create multiple reports, please make sure that this line is not commented out to ensure that each print job is done sequentially. In fact, if you load the print queue very quickly, you could have multiple files waiting to be printed, and the filename data associated with each one will be lost.

    To test my theory, I tried to print 20 reports in quick succession using the following code, and turning off the post creation editing or opening the PDFs in the Reader:

    For i = 1 To 20
        MyOutputFilename = Replace$(Application.CurrentProject.name & "_" & Format$(i, "00"), ".mdb", ".pdf")
        Convert2PDF MyReportName, MyBatchPrinter, MyOutputPath, MyOutputFilename
        DoEvents
    Next i

This is what I got for the results:
  1. With the CheckFileCreated Function Used. All 20 files were created properly (with the correct name).
  2. Without the CheckFileCreated Function Used. Only 18 of the 20 files were created. The 8th and 13th files are missing. They were created but the filename property of the printer was already set to the new value before the file was written, so "My_Test_08.pdf" was written as "My_Test_09.pdf" and subsequently overwritten by the 9th report.
     Please take a look, and let me know if you think the problem is something else.

    Cheers!

P.S. An alternative if print performance is critical to you would be to setup multiple batch printers, and make sure that only one file is printed to each before a new one is added. You may indeed get performance gains if you have a multi-processor CPU core, but it could allow to have 4 print jobs to get processed concurrently. I am guessing that the Windows Printer spool does provide a multi-threaded environment that would be necessary for that to work though, so this suggestion is untested and may not work.




-------------
Michel Korwin-Szymanowski
EXP Systems LLC


Posted By: Gene
Date Posted: 27 Dec 07 at 1:04PM
I appreciate the time you've spent on this.  I was having a problem which I corrected with an extra Sleep before the rtn =
 
        Sleep (2000)
        ' Changed to Path with MyReport
        rtn = oPDF.Utility_Open_PDF(MyOutputPath & "\" & MyReport & ".pdf", "")
        TempBool = CheckFileCreated(MyOutputPath & "\" & MyReport & ".pdf")
 
I've run about 20 of the same report one after another and they all seemed to print correctly.  I appear to only be having this problem with one report (I've tested it on about 6).  Another similar report with a similar name creates the file name as the report name (as I expect) and then I rename it.  I could probably work around this but I could possible be running a lot of reports off of this and I don't want to spend a lot of time testing each one.  I have several clients that may be interest in me modifying their programs to incorporate the pdf's.  I'm attaching a lot of information just in case it does you any good. 
 
I run a similar report called "arrptClientSummary" and it runs correctly every time

Line Calling Function
FName = Convert2PDF2("ARrptClientSummaryNew", "BatchPDF", Path, FName, True)
 
Name of File that is created (ARrptGenLtr1.pdf").  I have not run a report with this name today - This is a report that I ran previously.
 
Get an error number = 0 and DLL error number = 0 because file is not found because it's naming it the wrong thing
 
Parameter Input Values
? MyReport
ARrptClientSummaryNew
? MyBatchPrinter
BatchPDF
? MyOutputPath
N:\MyDocs\Campbell\TempEMailFiles
? MyOutputFileName
Summary_State Auto Insurance_12270700.pdf
? OpenDoc
True
 
Mostly your code with a couple of changes - I took out most comments to make it shorter
 
Public Function Convert2PDF2(ByRef MyReport As String, ByRef MyBatchPrinter As String, ByRef MyOutputPath As String, ByRef MyOutputFilename As String, OpenDoc As Boolean) As String
    ' Declare variables
    Dim rtn As Long
    Dim TempBool As Boolean
    Dim oDefaultPrinter As Printer
    Dim DefaultPrinter As String
    Dim oPDF As New PDF_reDirect_Pro_Tool
   
    On Error Resume Next
   
    If Right(MyOutputPath, 1) = "\" Then
        MyOutputPath = Left(MyOutputPath, Len(MyOutputPath) - 1)
    End If
    ' Create the target folder if it's missing
    If Dir$(MyOutputPath, vbDirectory) = "" Then MkDir (MyOutputPath)
   
    ' Changed to Path with MyReport
    If Dir$(MyOutputPath & "\" & MyReport & ".pdf") Then Kill (MyOutputPath & "\" & MyReport & ".pdf")
    If Dir$(MyOutputPath & "\" & MyOutputFilename) Then Kill (MyOutputPath & "\" & MyOutputFilename)
   
    With oPDF
        If Not .Batch_Printer_Is_Installed(MyBatchPrinter) Then
            TempBool = .Batch_Printer_Install(MyBatchPrinter)
            If Not TempBool Then GoTo Error_Occured
        End If
        TempBool = .Batch_Printer_Load_Settings(MyBatchPrinter)
        If Not TempBool Then GoTo Error_Occured
       
        .Settings_Output_Path = MyOutputPath    ' Path where to save output PDF to
        ' Changed to MyReport name
        .Settings_Output_FileName = MyReport & ".pdf"   ' Changed to Report Name
        .Settings_Overwrite = OW_NO_WARN        ' OW_NO_WARN,OW_WARN,OW_SAVEAS_DIALOG,OW_NEW_SEQ_NUMBER,OW_APPEND,OW_PREPEND
        .Settings_Trailer_Digits = 3            ' Format of trailing digits (3 = 3 zeros = "000")
        .Settings_Number_Start = 1              ' Default sequence start number
        .Settings_Show_Progress = True          ' Show Progress Bar?
       
        .Settings_Picture_Quality = QUALITY_GOOD '  QUALITY_HIGH, QUALITY_VERY_GOOD, QUALITY_GOOD, QUALITY_LOW
        .Settings_Rotation = ROTATE_AUTO        ' ROTATE_AUTO, ROTATE_90CW, ROTATE_180, ROTATE_90CCW
        .Settings_Fast_Web_View = False         ' Only works on unencrypted & unlocked PDF. (Also called "linearize")
        .Settings_Add_Links = False             ' Run through all of the text, page-by-page, and create hyperlinks when found
        ' changed this
        .Settings_Stamps = ""
                                           
        .Settings_FileType = 0                  ' 0 = PDF, 1 = Encrypted PDF (ie user is asked for password to open the PDF)
        .Settings_Encrypt_Password = "Password" ' Encryption Password
       
        .Settings_Locked = False                ' Set to "True" to Lock the PDF and apply the restrictions
       
        .Settings_Lock_Do_Not_Allow_Printing = True          ' Set to "False" to allow printing
        .Settings_Lock_Do_Not_Allow_Copy_and_Paste = True    ' Set to "False" to allow Copy-and-Paste
        .Settings_Lock_Do_Not_Allow_Making_Changes = True    ' Set to "False" to allow Making Changes
        .Settings_Lock_Do_Not_Allow_Adding_Notes = True      ' Set to "False" to allow Adding Notes
       
        .Settings_Master_Password = "Master"    ' Master Password used for locking a PDF.
        TempBool = oPDF.Batch_Printer_Save_Settings(MyBatchPrinter)
        ' Skip changing settings if an error occured
        If Not TempBool Then GoTo Error_Occured
  
        ' 2002 works here so changed to 10
        If Val(Application.Version) >= 10 Then
            Set oDefaultPrinter = Application.Printer
            Application.Printer = Application.Printers(MyBatchPrinter)
        Else
            DefaultPrinter = GetDefaultPrinter()
            Err.Clear
            If Not SetDefaultPrinter(MyBatchPrinter & ",PDF reDirect Pro v2,PDF_REDIRECT_PORT:") Then
                oPDF.LastErrorNumber = Err.Number
                oPDF.ErrorLastDLL = Err.LastDllError
                oPDF.LastErrorDescription = "Could not set the Batch Printer as the output printer."
                GoTo Error_Occured
            End If
        End If
               
        DoCmd.OpenReport MyReport, acViewPreview  ' Generate a Preview First
        DoCmd.OpenReport MyReport, acViewNormal   ' Print the Report
      
        DoCmd.Close acReport, MyReport
       
        If Val(Application.Version) >= 10 Then
            If Not oDefaultPrinter Is Nothing Then Application.Printer = oDefaultPrinter
        Else
            SetDefaultPrinter DefaultPrinter
        End If
       
        ' Changed to Path with MyReport
        TempBool = CheckFileCreated(MyOutputPath & "\" & MyReport & ".pdf")
        If Not TempBool Then GoTo Error_Occured
       
        ' added this - needed the extra time for the file to be recognized
        Sleep (2000)
        ' Changed to Path with MyReport
        rtn = oPDF.Utility_Open_PDF(MyOutputPath & "\" & MyReport & ".pdf", "")
        TempBool = CheckFileCreated(MyOutputPath & "\" & MyReport & ".pdf")
        If rtn = 0 Then GoTo Error_Occured
        ' Change to new file name
        TempBool = oPDF.Utility_Close_PDF(MyOutputPath & "\" & MyOutputFilename)
        If Not TempBool Then GoTo Error_Occured
        If Dir$(MyOutputPath & "\" & MyReport & ".pdf") Then Kill (MyOutputPath & "\" & MyReport & ".pdf")
       
        TempBool = CheckFileCreated(MyOutputPath & "\" & MyOutputFilename)
        If Not TempBool Then GoTo Error_Occured
       
        ' added a parameter to open if I want
        If OpenDoc Then
            ShellExecute 0, "Open", MyOutputPath & "\" & MyOutputFilename, "", "", 3
        End If
        ' made it a function to return the name of the file
        Convert2PDF2 = MyOutputPath & "\" & MyOutputFilename
       
        If Not TempBool Then
Error_Occured:
            MsgBox "An Error Occured: " & .LastErrorDescription & vbCrLf & _
                     "Error Number =" & Str$(.LastErrorNumber) & vbCrLf & _
                     "DLL Error Number =" & Str$(.ErrorLastDLL), _
                    vbExclamation, "Batch_PDF_Control"
            Convert2PDF2 = ""
        End If
       
    End With
   
    ' Release the object
    Set oPDF = Nothing
End Function


Posted By: Michel_K17
Date Posted: 28 Dec 07 at 1:55AM
Hi,

    Wow... Smile

    You definitely should not have to do the extra kind of work-around that you are doing to get the filename that you are doing.

    A couple of comments,  about this section your code:

        ' Changed to Path with MyReport
        TempBool = CheckFileCreated(MyOutputPath & "\" & MyReport & ".pdf")
        If Not TempBool Then GoTo Error_Occured
       
        ' added this - needed the extra time for the file to be recognized
        Sleep (2000)
        ' Changed to Path with MyReport
        rtn = oPDF.Utility_Open_PDF(MyOutputPath & "\" & MyReport & ".pdf", "")
        TempBool = CheckFileCreated(MyOutputPath & "\" & MyReport & ".pdf")
        If rtn = 0 Then GoTo Error_Occured         ' Change to new file name
        TempBool = oPDF.Utility_Close_PDF(MyOutputPath & "\" & MyOutputFilename)
        If Not TempBool Then GoTo Error_Occured
        If Dir$(MyOutputPath & "\" & MyReport & ".pdf") Then Kill (MyOutputPath & "\" & MyReport & ".pdf")


    Instead of opening the PDF and closing it to a new name, you could simply use the Visual Basic "Name" function which would be easier and faster. That is:

    Name Old_Path_and_Name As New_Path_and_Name.

    (Note, I use the Open/Close functions to do special processing such as adding a stamp. What you did is not wrong, and should work fine, but probably uses more code than necessary)

    You also have what looks like a redundant CheckFileCreated in between the Open- Close. I am not sure what you are doing there, but you already checked for that file just prior to the sleep command.

    And finally, the sleep command should not be necessary, but if it works, then so be it. 2 seconds is a long time. The function
CheckFileCreated already uses the Sleep in a loop until the PDF file becomes available, so I am (like you) confused on why it would be required.

    I hope that helps.






-------------
Michel Korwin-Szymanowski
EXP Systems LLC


Posted By: Gene
Date Posted: 02 Jan 08 at 7:36PM

Thanks for your reply again but I'm not really getting anywhere.  Your program is just what I need and I've purchased several and I've had a client purchase several but I don't want to spend hours trying to figure out a way around the problems I'm having.  Maybe it's just on my machine.

When I first used the control the

.Settings_Output_FileName did not appear to work correctly because the pdf file created was the same name as the report.  So after allowing for that problem and assuming that the pdf file would always be named the same name as the report I had the following problem.

While running a report called "ARrptClientSummaryNew" and assigning the Filename property "ARrptClientSummaryNew.pdf" I had it name the file "ARrptGenLtr1.pdf" which was a report that I first tested the program with (it does it every time).  I've searched the registry to try and find out if it could be picking up the name from there but I don't think so.  I also deleted the temp batch printer and let it be recreated but it didn't do any good.  So I'm left with testing on another computer, reinstalling the control, etc.
 
If you've never had a problem with the program naming the file something other then the name you've assigned to the Filename property then I guess I have nothing to go on.  If you don't have any additional ideas let me know and I'll try to work it out.
 
There's only one thing that I can add that may be important.  In my report "ARrptGenLtr1" I use and ActiveX Memo Control.  I've got no idea if this may conflict with something.
 
Thanks for any help you can give me.


Posted By: Michel_K17
Date Posted: 02 Jan 08 at 11:52PM
Hi,

    I can see how this would be frustrating indeed.

    Here are some additional details on how it works: The filename is not saved in the registry. It is saved in a preferences file for the batch printer which is located in the folder here:

file:///C:/Documents%20and%20Settings/Michel_K17/Application%20Data/PDF%20reDirect/Batch_Printers/BatchPDF.ini - C:\Documents and Settings\UserName\Application Data\PDF reDirect\Batch_Printers

    Note: substitute your login name for where it says "UserName".

    Inside, you will find a settings file for your Batch printer (for example: BatchPDF.ini). The batch printer loads the settings from that ini file everytime it runs, and reacts accordingly. For  example, a typical file might look like this:

"BatchPDF"
"X:\Products\PDF_Redirect_Pro\Code\ActiveX_PDFR_Pro_Component\v22006\PDF"
"MS_Access_Sample_VBA_Code"
#TRUE#
0
3
1
"-P0-Q2-C0-R0-L0-E0-B0-S0-H0"
0
"123219189353202219342237237241233262505742559217"
#FALSE#
"0*0*0*0*0*0*0*0"
"622012393910567701111699745737738723736893557"
#FALSE#
"CONFIDENTIAL.pdf, DRAFT - Text.txt"
#FALSE#
""
0
""
""
""
""
""
""

    The first line is the name of the batch printer (for cross-check ing purposes), the second line is the path where the PDF needs to be saved to. The third line is the filename (without extension). The rest are the various settings you might have selected for this printer.

    So, a way to check to see if the filename was properly written to would be to read the third line of data, and compare it to what you saved in your code. This, for example, would prove that there is a bug in my code. However, there is very little magic here. Here is the code inside the ActiveX control that saves the filename:

Public Function Batch_Printer_Save_Settings(ByRef sPrinter As String, Optional ByRef Path_Override As String) As Boolean
    On Error Resume Next

    ' Reset Errors
    Reset_Errors
   
    ' Refuse to work if not in "Professional" Mode
    If Not Prefs.Pro_Mode Then
        ' Set an error
        LastErrorDescription = "You need to have PDF reDirect Professional activated to use this tool."
        ErrorLastDLL = 1906     ' ERROR_INVALID_PRINTER_STATE = 1906
        Exit Function
    End If

    ' Filename cannot be empty
    If Len(Settings_Output_FileName) = 0 Then
        LastErrorNumber = FILENAME_IS_EMPTY
        LastErrorDescription = "The Batch PDF Output Filename is empty"
        Exit Function
    End If

    ' FileName cannot be more than 200 characters (to leave room for zeros at end)
    If Len(Settings_Output_FileName) > 200 Then
        LastErrorNumber = FILENAME_IS_GREATER_THAN_200_CHARACTERS
        LastErrorDescription = "The Batch PDF Output Filename contains more than 200 characters."
        Exit Function
    End If

    ' Do not accept quotes in Filename
    Settings_Output_FileName = Replace$(Settings_Output_FileName, Chr$(34), "'")

    ' Remove trailing extensions (if there)
    If Len(Settings_Output_FileName) > 5 Then
        If Mid$(Settings_Output_FileName, Len(Settings_Output_FileName) - 3, 1) = "." Then
            Settings_Output_FileName = Left$(Settings_Output_FileName, Len(Settings_Output_FileName) - 4)
        End If
    End If

    ' Path cannot be more than 255 characters
    If Len(Settings_Output_Path) > 255 Then
        LastErrorNumber = PATH_IS_GREATER_THAN_255_CHARACTERS
        LastErrorDescription = "The Batch PDF Output Path contains more than 200 characters."
        Exit Function
    End If

    ' Path  must be greater than two characters ("C:")
    If Len(Settings_Output_Path) = 0 Then
        LastErrorNumber = PATH_IS_EMPTY
        LastErrorDescription = "Path is empty."
        Exit Function
    End If

    ' Make sure Path has no trailing "\"
    If Right$(Settings_Output_Path, 1) = "\" Then Settings_Output_Path = Left$(Settings_Output_Path, Len(Settings_Output_Path) - 1)

    ' Make sure Path is valid
    rtn = PathIsDirectory(Settings_Output_Path)
    If rtn = 0 Then
        ' Not a valid Path
        LastErrorNumber = PATH_IS_NOT_VALID
        LastErrorDescription = "Following Path is not valid:" & vbCrLf & Settings_Output_Path
        Exit Function
    End If

    ' Make sure Override Path to settings ini file is valid
    If Len(Path_Override) > 0 Then
        ' Make sure trailing slash is there
        Path_Override = AddPathSlash(Path_Override)
        ' Let's be nice, and create the Batch Printers folder if not given
        If Path_Override = "C:\Program Files\PDF reDirect\" Then
            Path_Override = Path_Override & BATCH_PATH
            If Len(Dir(Path_Override, vbDirectory)) = 0 Then MkDir Path_Override
        End If
        ' Now check that the folder exist
        rtn = PathIsDirectory(Path_Override)
        If rtn = 0 Then
            ' Not a valid Path
            LastErrorNumber = PATH_IS_NOT_VALID
            LastErrorDescription = "Following Path is not valid:" & vbCrLf & Path_Override
            Exit Function
        End If
    End If

    ' Update the batch printer with the new settings
    '-----------------------------------------------


    ' v2.2.6 - User can now override the output path in case in needs to go to the Program Folder
    ' for System and Network processes that cannot resolve their AppData Path.
    If Len(Path_Override) = 0 Then
        ' Normal ops: use AppData path
        Open MyPaths.AppData & BATCH_PATH & sPrinter & ".ini" For Output As #1
    Else
        ' Use path override (create folders as necessary)
        Open Path_Override & sPrinter & ".ini" For Output As #1
    End If
      
    Write #1, Settings_PrinterName              ' Name of the Batch Printer
    Write #1, Settings_Output_Path             ' Path to Output PDF to
    Write #1, Settings_Output_FileName      ' Filename of the PDF


... etc.

    As you can see, apart from the basic sanity checks, there is nothing particularly unusual.

    Here is the printer's code that does the reverse: it loads the settings as follows:

Public Function LoadBatchSettings() As Boolean
   
    On Error GoTo Error_Handler

    ' Assume we will fail
    LoadBatchSettings = False

    ' Update each printer with the new settings (if they have changed)
    '-----------------------------------------------------------------

    With BatchSettings
        Open MyPaths.AppData & "Batch_Printers\" & .Printer & ".ini" For Input As #1
        Input #1, .Printer              ' Name of the Batch Printer
        Input #1, .Path                 ' Path to Output PDF to
        Input #1, .FileName          ' Filename of the PDF

 
etc...


    If you want to see what the printer code read for the filename, you can start PDF reDirect Pro, go into the Preferences, and select (check) the General button >> Generate Log Files. Then, close PDF reDirect Pro.

    Finally run your code a few times until the problem occurs. When the problem occurs, stop and check the following:

  1. The Batch Printer Settings (ini) file: is the filename correct? then,
  2. check the log files to see if the code that is creating the PDF is loading the filename correctly.  The log file you are looking for is called "Log_CA.txt" and is located in the "Bug Reports" folder. That is:
C:\Documents and Settings\UserName\Application Data\PDF reDirect\Bug_Reports

    If you see anything that does not look right, then send me the log file, and I will take a look.

    Beyond that, the only thing I can offer is to troubleshoot your project myself. If your data is not confidential, you can send me your  .mdb file to me, and I can take a look. Please make sure that the entire file is less than 5MB otherwise my ISP will block it, and zip it as well.

    There is no doubt that the problem could be in my code, despite being very careful, I have made, unfortunately, a few errors in the past. Most of these tend to be in the "beta" series, but not always. Therefore, I am most interested in making sure that my code is not to blame.

    One limit though is that my research will need to be limited to finding bugs in PDF reDirect (ie I will not be able to exhaustively review all of your code and fix any errors: I would rather continue working on PDF reDirect v3).

    Another thing we can do is to use a remote control program such as www.logmein.com so that I can see the problem on your computer first hand. If anything, I can do a bit of troubleshooting and make sure that my code is working OK (or not). Please let me know if this is something that you are interested in pursuing.

    Cheers!

 


-------------
Michel Korwin-Szymanowski
EXP Systems LLC


Posted By: Gene
Date Posted: 03 Jan 08 at 10:01AM
Thanks for all your time on this.  It appears the problem is on my end (which I assumed).  I had PDF995 installed and had found some code for making PDFs with it and had been testing it prior to using your product.  There was obviously some kind of comflict with the printer port.  Also, the report that I was having all of the trouble with had actually, somehow, had the default printer changed to PDF995.  So if anyone else has the same problem your first question might be what other PDF drivers they have installed.  The rest of this information could be useful to me in the future.
 
Thanks again.


Posted By: Michel_K17
Date Posted: 04 Jan 08 at 12:43AM
That's good news! Thanks for the update: I am glad that you found the cause of all of your misery.



-------------
Michel Korwin-Szymanowski
EXP Systems LLC



Print Page | Close Window