|
-
Oct 24th, 2014, 04:46 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] OpenFileW [saveAs Dialog] not returning FULL Path : FileName only...
Hello,
Just realised that my saveAs dialog [GetSaveFileNameW] only returns the FILENAME rather than the FULLPATH + Filename ?
Code:
Public Function cDlgShowSave(ByVal InitialDir As String, _
ByVal DialogTitle As String, ByVal frmHwnd As Long, _
Optional Filter As String = "Text File(*.txt)|*.txt", _
Optional FileTitle As String = vbNullString, _
Optional ByRef rtnFilter As Long = 1) As String
On Error GoTo errFound
Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim FileFilter As String
Dim strFile As String
If FileTitle = vbNullString Then
strFile = String$(512, 0)
Else
FileTitle = Replace(FileTitle, "\", "_")
FileTitle = Replace(FileTitle, "/", "_")
FileTitle = Replace(FileTitle, ":", "_")
FileTitle = Replace(FileTitle, "*", "_")
FileTitle = Replace(FileTitle, "?", "_")
FileTitle = Replace(FileTitle, Chr$(34), "_")
FileTitle = Replace(FileTitle, "<", "_")
FileTitle = Replace(FileTitle, ">", "_")
FileTitle = Replace(FileTitle, "|", "_")
strFile = String$(512, 0)
strFile = FileTitle & String$(512 - Len(FileTitle), 0)
End If
OpenFile.lStructSize = Len(OpenFile)
OpenFile.hWndOwner = frmHwnd
OpenFile.hInstance = App.hInstance
FileFilter = Replace$(Filter, "|", vbNullChar)
If AscW(Right$(FileFilter, 1)) <> 0& Then FileFilter = FileFilter & vbNullChar
OpenFile.lpstrFilter = StrPtr(FileFilter)
OpenFile.nFilterIndex = rtnFilter
OpenFile.lpstrFile = StrPtr(strFile)
OpenFile.nMaxFile = Len(strFile)
OpenFile.lpstrFileTitle = StrPtr(strFile)
OpenFile.nMaxFileTitle = Len(strFile)
OpenFile.lpstrInitialDir = StrPtr(InitialDir)
OpenFile.lpstrTitle = StrPtr(DialogTitle)
OpenFile.flags = 0
lReturn = GetSaveFileName(OpenFile)
If lReturn = 0 Then
'Cancel Button Pressed
Else
cDlgShowSave = Left$(strFile, InStr(strFile, vbNullChar) - 1)
debug.print strFile
rtnFilter = OpenFile.nFilterIndex
End If
Exit Function
errFound:
MsgBox Err.Description, vbCritical, "File Save"
cDlgShowSave = ""
End Function
on a quick debug.print it prints the following:
NewFile.txt Admin\Desktop\NewFile.txt
So it's stripping out the NewFile.txt.
Even if i did reverse this and strip the right side out, its still missing the Drive letter?
How can i find out the Drive letter to complete the puzzle?
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Oct 24th, 2014, 08:05 PM
#2
Re: OpenFileW [saveAs Dialog] not returning FULL Path : FileName only...
Thinking possibly the entire filename is returned but nullchar separated from the path?
Debug.Print strFile might show that.
As far as I know, the only time the path & filename should be separated is if the flags contain OFN_ALLOWMULTISELECT. If that were the case, the path would be followed by a nullchar & each of the selected files would be null separated.
Edited: I see it. You have both of the structure's file & title pointing to the same string. The Title is overwriting the first Len(Title)+1 characters; the +1 is the null terminator
Also, just FYI. You don't have to mess with all the StrPtrs if you choose not to. The reason you are at the moment is because you declared your API something like the following, correct?
Code:
GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameW" (pOpenfilename As OPENFILENAME) As Long
Now, if you wanted to keep your OPENFILENAME structure String members as Strings vs Longs, do that, change them back & make the coding a bit easier on you. The only change you have to make is how you declare the API & one small tweak in calling it:
Code:
GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameW" (ByVal pOpenfilename As Long) As Long
' Now instead of calling it this way:
lReturn = GetSaveFileName(OpenFile)
' You'd call it this way:
lReturn = GetSaveFileName(VarPtr(OpenFile))
Note: When passing structures containing strings to a Unicode API, if you use the VarPtr(structure) method, VB has no clue that the members contain strings & thus, doesn't try to convert them to ANSI. Kinda neat.
In my UnicodeOpenSave class, I designed it awhile back & built it for both ANSI & UNICODE based on the operating system. My OPENFILENAME String members are defined as String not Long. But to have VB send the structure correctly on a non-unicode O/S, I had to declare the API 2 ways. A lot easier than having 2 structures & populating them differently. In the end, my code looked kinda like this:
Code:
If IsUnicodeSystem Then
lReturn = GetSaveFileNameW(VarPtr(OpenFile))
Else
lReturn = GetSaveFileNameA(OpenFile)
End If
Last edited by LaVolpe; Oct 24th, 2014 at 08:45 PM.
-
Oct 24th, 2014, 10:42 PM
#3
Re: OpenFileW [saveAs Dialog] not returning FULL Path : FileName only...
Hey some1uk03,
Just looking at it, I see that you're Debug.Print'ing the untrimmed strFile variable. That's probably not it, but it may be obscuring your Debug.Print. Why don't you create another sTemp variable and trim, and then Debug.Print.
But upon further inspection, why did you put the same pointer in both lpstrFile and lpstrFileTitle? I don't believe Windows is going to "see" that you've given it the same pointer twice. It's going to think it's two separate buffers, and just overwrite its first return entry with data from the second return entry. I hope that makes sense. In other words, if you want lpstrFileTitle separate from lpstrFile, you'll need two buffers. Otherwise, just pass a zero in lpstrFileTitle. I didn't try it but I bet that's it.
EDIT: Actually, if you don't want lpstrFileTitle, pass a zero in nMaxFileTitle.
-
Oct 25th, 2014, 06:25 AM
#4
Thread Starter
Frenzied Member
Re: OpenFileW [saveAs Dialog] not returning FULL Path : FileName only...
Edited: I see it. You have both of the structure's file & title pointing to the same string. The Title is overwriting the first Len(Title)+1 characters; the +1 is the null terminator
Hi Guys, just sorted it out, it seems the problem was with both of the structure's file & title pointing to the same string.
Thanks
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|