Stephen Lebans PDF code - set my own folder for saving the file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TravelingCat
    New Member
    • Feb 2010
    • 82

    #16
    No problem, whatever i can do to help myself and you solve this

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #17
      Ah, this is not a problem and can be handled. I say it's not a problem because it seems clear that the intention is actually to delete the file if it already exists. In other words to ensure the file doesn't exist prior to your creating it.

      To handle this, simply add error handling code around your current line #27 of the function :

      Code:
        On Error Resume Next
        Call Kill(lpTempFileName)
        On Error GoTo 0
      Let us know how you get on.

      Comment

      • TravelingCat
        New Member
        • Feb 2010
        • 82

        #18
        I added your code. The same line
        Code:
        strEMFUncompressed = GetUniqueFilename(sPath, "SNP", "tmp")
        calls the GetUniqueFilena me function, but now there's an error in lines 760-762 of my post number 5 (i don't want to post it again due to its length) -
        Code:
          If Len(UseExtension) > 0 Then
            lpTempFileName = Left(lpTempFileName, Len(lpTempFileName) - 3) & UseExtension
          End If
        The error i'm getting is "Invalid procedure call or argument". When i stand on it, lpTempFileName = "", so it basically is Left(-3)..

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #19
          I still don't need all the code again. Just the stuff relevant to where you're having problems.

          The original code doesn't have any of the changes in so, quite apart from there being so much irrelevant code to work through for no benefit, it also doesn't have any of the changes that have already been applied. In effect I'm trying to work with very large amounts of code while having to bear in memory the changes that have already been made. It would make it a lot more practical if you posted the relevant code you're actually struggling with.

          Comment

          • TravelingCat
            New Member
            • Feb 2010
            • 82

            #20
            I genuinely don't know what else can i do to make myself more clear. As i mentioned, i'm still getting stuck in the same function (GetUniqueFilen ame), just in a different place this time. I pasted the relevant lines that i get stuck in, here they are again
            Code:
            If Len(UseExtension) > 0 Then
            [B]lpTempFileName = Left(lpTempFileName, Len(lpTempFileName) - 3) & UseExtension[/B]
            End If
            I copied the error message i'm getting when i reach line 2 (bold) - "Invalid procedure call or argument".
            If i can bring more details to the table please let me know.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #21
              You said in post #18 that lpTempFileName was found to be empty. I need to see the (current) code that runs before this, that sets lpTempFileName. It's where I expect to find the problem.

              Comment

              • TravelingCat
                New Member
                • Feb 2010
                • 82

                #22
                After debugging:
                First time lpTempFileName fills after function GetUniqueFileNa me is called for the first time, right before the OutputTo action (strPathandFile Name = GetUniqueFilena me(strPath, "SNP" & Chr(0), "snp")). That time lpTempFileName has a valid path with snp extension.
                The second time is after GetUniqueFilena me is called for the second time (strEMFUncompre ssed = GetUniqueFilena me(sPath, "SNP", "tmp")). The sPath = the path i defined. So with this sPath the code goes to GetUniqueFileNa me:
                Code:
                Private Function GetUniqueFilename(Optional path As String = "", _
                Optional Prefix As String = "", _
                Optional UseExtension As String = "") As String
                  Dim wUnique As Long
                  Dim lpTempFileName As String
                  Dim lngRet As Long
                
                  wUnique = 0
                  If path = "" Then path = CurDir
                  [B]lpTempFileName = String(MaxPath, 0)[/B]
                  lngRet = GetTempFileName(path, Prefix, wUnique, lpTempFileName)
                  [B]lpTempFileName = Left(lpTempFileName, InStr(lpTempFileName, Chr(0)) - 1)[/B]  On Error Resume Next
                  Call Kill(lpTempFileName)
                  On Error GoTo 0
                
                  If Len(UseExtension) > 0 Then
                    [B]lpTempFileName = Left(lpTempFileName, Len(lpTempFileName) - 3) & UseExtension[/B]
                  End If
                  GetUniqueFilename = lpTempFileName
                End Function
                In line 10 lpTempFileName gets initialized with some unclear signs (in the immediate window it's just empty).
                In line 12 lpTempFileName = "".
                In line 18 lpTempFileName is of course still empty, and after this line an error occurs.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32633

                  #23
                  Line #12 is required before lpTempFilename can be viewed properly. Unfortunately, you have added the On Error Resume Next to the end of this line. I cannot see how this code could possibly compile or run, so I'm confused and am worrying that what you post may not be accurate. See below (indented) for instructions on what to do before posting code. Always.
                  When posting any code on here please :
                  1. For VBA code specifically :
                    1. Ensure you have Option Explicit set (See Require Variable Declaration).
                    2. Try to compile it. If it doesn't compile for any reason please explain that clearly - including the error message and which line of your code it appears on. Compilation is done from the Visual Basic Editor menu - Debug \ Compile Project (Where Project is the actual name of your project).
                  2. For SQL as well as VBA :
                    1. Copy your code (using the Clipboard - Cut / Copy / Paste) from your project directly into your post. Typing in code is not appreciated as it is likely to introduce typos which cause members to waste their time unnecessarily.
                    2. Ensure that the code in your post is enveloped within CODE tags. The hash (#) button in the posting page helps with this. Simply select your code and click on the hash button to have it enveloped automatically.

                  If all these points are covered then all members will be better able to understand, and therefore attempt to answer, your question.

                  Try splitting line #12 properly and test with it as :
                  Code:
                  lpTempFileName = Left(lpTempFileName, InStr(lpTempFileName, Chr(0)) - 1)
                  On Error Resume Next
                  The value will not be properly accessible to VBA until line #12 has run properly. It could have all sorts of unpredictable stuff in there.

                  Comment

                  • TravelingCat
                    New Member
                    • Feb 2010
                    • 82

                    #24
                    As to your instructions, this module has option explicit set and my project compiles.
                    "On error resume next" slided into line 12 by accident while copy-pasting, my bad - i didn't notice. Of course it starts from a new line.
                    And as i mentioned earlier, lpTempFileName in line 12 = "".

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32633

                      #25
                      I daren't think what may have caused a copy/paste of code not to work. I hope it doesn't mean I am not seeing what you're actually testing, as that would be worrying.

                      However, I think I have seen a problem with your line #11. OS functions deal in C type strings, which are always terminated by NullChars. In your code Path and Prefix are not prepared correctly for an OS func. Try instead :
                      Code:
                      lngRet = GetTempFileName(path & Chr(0), Prefix & Chr(0), wUnique, lpTempFileName)

                      Comment

                      • TravelingCat
                        New Member
                        • Feb 2010
                        • 82

                        #26
                        What may have caused it is i might have deleted space between lines when writing the post.
                        I tried your line, it didn't change anything, lngRet = 0 either way.

                        Comment

                        • NeoPa
                          Recognized Expert Moderator MVP
                          • Oct 2006
                          • 32633

                          #27
                          You may want to try calling GetLastError(). I can't see why it wouldn't work, but then I can't see the parameters you're calling it with either, nor what folders exist and are accessible to your PC. This seems like something you'll need to find by debugging yourself. From your earlier post you seem comfortable with that, but let us know if you need any help with it.

                          PS. In case you weren't aware, when the call to GetTempFileName returns zero (0) it means it has failed.

                          Comment

                          • TravelingCat
                            New Member
                            • Feb 2010
                            • 82

                            #28
                            Are you sure this function exists in vba? I can't find such.
                            I'm pretty sure GetTempFileName returns 0 because lpTempFileName is empty,i just have to figure out why it's empty.
                            Thank you for the effort you've put in trying to solve this.

                            Comment

                            • NeoPa
                              Recognized Expert Moderator MVP
                              • Oct 2006
                              • 32633

                              #29
                              It's certainly not in the VBA library (I included a link to the description of it in my previous post), but nor is GetTempFileName. Both are windows functions in fact, and need to be declared as such.

                              The complicated thing about Windows' functions is that they use an interface that is foreign to VBA (and VB incidentally). They expect strings to be NullChar terminated, as is the standard for many programming languages, including C. That is why some fiddling is required whenever calling them from VBA. When sending string data across it must have at least one NullChar (or Chr(0)) appended to the string. When receiving data back, it is important that the correct length of the data be set immediately upon return otherwise the string is in an incomplete state.

                              I'll try to give an illustration of how the string "Where" would be stored in both cases :
                              Code:
                              VBA
                                0  1  2  3  4  5  6
                              |05|00|57|68|65|72|65|
                              |    5| W| h| e| r| e|
                              C or Windows
                                0  1  2  3  4  5
                              |57|68|65|72|65|00|
                              | W| h| e| r| e|NullChar|
                              In the VBA version 05 00 represents the 16-bit number 5 which is the length of the data. In the Windows version the data starts immediately and continues until a 00 is hit, which in C, starting from 0, would be position #5.
                              Last edited by NeoPa; Sep 20 '10, 03:08 PM. Reason: Expanded explanation

                              Comment

                              Working...