views:

1030

answers:

2

In many of my Access (2002) programs I use the GetOpenFileNameA and GetSaveFileNameA functions from comdlg32.dll. I often set the initial directory to the user's My Documents folder (using calls to SHGetSpecialFolderLocation and SHGetPathFromIDListA from shell32). This all works fine under Windows XP.

However, I recently switched to Windows 7 as my development environment and have been getting the following error message:

You can’t open this location using this program. Please try a different location.

The function I use to get the My Documents location is returning the correct folder. However, even if I hard code that directory location into the GetOpenFileNameA call, I still get the error.

I came across this post: http://social.msdn.microsoft.com/Forums/en-US/windowsuidevelopment/thread/3391f1dd-25b0-4102-9d5c-58309cc72c9d but, even adapting it to work with Access instead of Excel, I had no luck.

EDIT: Suddenly this is no longer a problem for me. I suspect a windows update went out addressing this issue. Does anyone know if that's true or not?

EDIT: It turns out this is still a problem. Also, in case it helps in the troubleshooting I have found that I get this error message for any of the special folder locations (My Music, My Documents, etc). Also, if I change the location of the My Music folder to, say, C:\Test then I get this message when I try to open the folder C:\Test, while the folder C:\Users\Mike\Music (the original My Music location) opens without a hitch.

+1  A: 

Windows 7 adds the concept of a "library", which is basically a virtual folder that includes the contents of at least two actual subdirectories. One place it uses the library is the "My Documents" folder, which (at least by default) is a library that includes both the user's documents directory ("c:\users\whoever\documents") and the public documents directory (C:\users\public\documents").

As such, the basic approach you're using simply can't work -- there is no path that denotes the Documents folder. The documents folder needs to be specified by a PIDL, not a path.

Edit: It's not clear what's going on if you can't open C:\users\user\Documents. A quick test in C++ works fine using code like:

OPENFILENAME data = {0};
wchar_t filename[256] = {0};

data.lpstrInitialDir = L"C:\\users\\jerry\\documents";
data.lStructSize = sizeof(data);
data.lpstrFile = filename;
data.nMaxFile = sizeof(filename);

GetOpenFileName(&data);

OTOH, there's no real need to specify the initial path -- the Documents folder is the default anyway.

Jerry Coffin
except that the function call I'm using to get the "My Documents" folder IS returning an actual path ("C:\Users\Mike\Documents"). If I call GetOpenFileName with "C:\Users\Mike" as the initial directory, it opens into that directory fine; if call it with "C:\Users\Mike\Documents\SomeSubFolder" as the initial directory, that works, too. As soon as I call it with "C:\Users\Mike\Documents" it blows up with the error message.I can worry about incorporating the new Win7 library concept in the future, for now I just want the dialog to open the folder "C:\Users\Mike\Documents" without erroring.
mwolfe02
If I leave the initial path empty, I avoid the error message but it still refuses to open in the Documents folder.
mwolfe02
It seems Microsoft is highly recommending using a different api call (IFileOpenDialog vs GetOpenFileName) in Windows Vista and later (http://msdn.microsoft.com/en-us/magazine/dd861346.aspx#id0370015). I'm not entirely sure how to get at that call from vba. I'll probably ask that as a separate question. I'm still not sure why I can't use an explicit path in the dialog box, but it seems like the solution should be something like: 1) Check for the current windows version; 2) if Vista or later use the new dialog.
mwolfe02
@mwolfe02: Hmm...it sounds like VBA is doing something behind the scenes, so what you try to supply to GetOpenFileName isn't quite the same as what it receives. In that case, you're probably right, that about the only thing you can do is sidestep it complete (or maybe create a thunk in a DLL in something like C++, and call that instead so VBA won't "adjust" your parameters).
Jerry Coffin
My original problem seems to have solved itself, but there is some great info here, so I'll mark this answer as accepted.
mwolfe02
Turns out my original problem did not solve itself (see edit to original question), so I've un-marked this answer as accepted. Still good info, but I'm holding out hope for an actual solution to the original problem someday.
mwolfe02
A: 

The link I posted in my original question (http://social.msdn.microsoft.com/Forums/en-US/windowsuidevelopment/thread/3391f1dd-25b0-4102-9d5c-58309cc72c9d) held the answer after all. I'll summarize things here. To fix this behavior, you need to do away with the STRIPFOLDERBIT flag in the shell compatibility registry entry for all affected programs.

Keep in mind (and this is what tripped me up for so long) that 32-bit programs have entries in a special registry section if you have 64-bit windows. Here's the quick and dirty:

Rename STRIPFOLDERBIT to xSTRIPFOLDERBIT for the following keys (as applicable):

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ShellCompatibility\Applications\excel.exe HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ShellCompatibility\Applications\msaccess.exe HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\ShellCompatibility\Applications\excel.exe HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\ShellCompatibility\Applications\msaccess.exe

mwolfe02
Does this have something to do with those odd duplicate links to My Documents and such that show up in the documents folder? I find it enormously confusing as an end user, and my users find it incredibly frustrating.
David-W-Fenton
I'm not exactly sure what you are referring to when you talk about the duplicate links; I may not have run into that yet. It does seem to be tied directly to the special folders identified by class IDs (My Documents, My Music, etc) and not necessarily to the Libraries system (I did not get the error when opening folders that I had added to the Documents library; only when opening the one whose location I had marked as THE 'My Documents' folder).
mwolfe02
I'd love to know what I blew up by making the registry change or whether there is some other way around the problem so I don't have to make the registry change on client computers. So far none of my clients are using Win 7, so it hasn't been a real issue for me yet (just an annoyance). That said, I'd like to see an official response from Microsoft on the true purpose of the STRIPFOLDERBIT registry entry. There must have been SOME reason they went to the trouble of adding it...
mwolfe02
I meant the duplicate My Documents links that show up in %Profile%/Documents. These are driving one of my clients crazy, because one of them works and one of them is not accessible.
David-W-Fenton