No problem, whatever i can do to help myself and you solve this
Stephen Lebans PDF code - set my own folder for saving the file
Collapse
X
-
-
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
Comment
-
I added your code. The same line
Code:strEMFUncompressed = GetUniqueFilename(sPath, "SNP", "tmp")
Code:If Len(UseExtension) > 0 Then lpTempFileName = Left(lpTempFileName, Len(lpTempFileName) - 3) & UseExtension End If
Comment
-
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
-
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
If i can bring more details to the table please let me know.Comment
-
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 12 lpTempFileName = "".
In line 18 lpTempFileName is of course still empty, and after this line an error occurs.Comment
-
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 :- For VBA code specifically :
- Ensure you have Option Explicit set (See Require Variable Declaration).
- 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).
- For SQL as well as VBA :
- 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.
- 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
Comment
- For VBA code specifically :
-
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
-
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
-
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
-
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
-
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
-
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|
Comment
Comment