By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,045 Members | 1,760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,045 IT Pros & Developers. It's quick & easy.

Need to turn on file extension in windows within Access VBA

100+
P: 443
I'm using the ImportZippedFile to unzip files from within my Access application, however I've run into a problem that I don't know if I can fix.
In the ImportZippedFile it looks for the extension of the file in order to unzip the it. I had a client that the extension in windows was turned off so the unzip didn't work. I'm wondering is there a way to turn file extensions on from within my application. If you need the full code for the ImportZippedFile let me know and I'll post it.

Thanks for any help.
Sep 27 '17 #1

✓ answered by ADezii

  1. This one is a little tricky, Tom. You can accomplish this by using the WshShell Object which enables you to interact with various aspects of the Windows Shell.
  2. The Logic is as follows:
    1. You must query a specific Registry Key is see if the Hide extensions for known file types is ON or OFF (1 or 0). You can use the RegRead() Method of the WshShell Object.
    2. If this Option is ON (1), then you must turn it OFF via the RegWrite() Method of the WshShell Object.
    3. After enabling File Extension and performing your operations(s), you may wish to return it to its prior state.
  3. Enough rambling on, the Code to do this is listed below:
    Expand|Select|Wrap|Line Numbers
    1. Dim WshShell As Object
    2. Dim strRegKey As String
    3.  
    4. strRegKey = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\HideFileExt"
    5.  
    6. Set WshShell = CreateObject("WScript.Shell")
    7.  
    8. If WshShell.RegRead(strRegKey) Then
    9.   WshShell.RegWrite strRegKey, 0, "REG_DWORD"
    10. End If
  4. Good Luck with your Project.

Share this Question
Share on Google+
15 Replies


NeoPa
Expert Mod 15k+
P: 31,419
Can you be a little more precise. I know of configuring extensions but 'turning them on' means nothing to me.
Sep 28 '17 #2

PhilOfWalton
Expert 100+
P: 1,430
This may help.

Basically I think you have to change

HKEY_CURRENT_USER\Software\Microsoft\Windows\Curre ntVersion\Explorer\Advanced registry subkey.

In the registry.

There are lots of examples of how to do this on the web.

Phil
Sep 28 '17 #3

ADezii
Expert 5K+
P: 8,623
  1. This one is a little tricky, Tom. You can accomplish this by using the WshShell Object which enables you to interact with various aspects of the Windows Shell.
  2. The Logic is as follows:
    1. You must query a specific Registry Key is see if the Hide extensions for known file types is ON or OFF (1 or 0). You can use the RegRead() Method of the WshShell Object.
    2. If this Option is ON (1), then you must turn it OFF via the RegWrite() Method of the WshShell Object.
    3. After enabling File Extension and performing your operations(s), you may wish to return it to its prior state.
  3. Enough rambling on, the Code to do this is listed below:
    Expand|Select|Wrap|Line Numbers
    1. Dim WshShell As Object
    2. Dim strRegKey As String
    3.  
    4. strRegKey = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\HideFileExt"
    5.  
    6. Set WshShell = CreateObject("WScript.Shell")
    7.  
    8. If WshShell.RegRead(strRegKey) Then
    9.   WshShell.RegWrite strRegKey, 0, "REG_DWORD"
    10. End If
  4. Good Luck with your Project.
Sep 28 '17 #4

100+
P: 443
Well that works sometimes, I don't know why. Here's what I did, I went into Explorer and turned off the file extension. Then I ran my program and your code turned the file extension On, I have your code in the ImportZippedfile routing at the very beginning. I still get the unzip problem because the extension is still off. When I look in the Windows Explorer I see that the file extension is turned On but the file extension of the files don't show. If I turn the file extension off and then on again it works.
Any ideas what I'm doing wrong.
Sep 28 '17 #5

Rabbit
Expert Mod 10K+
P: 12,357
You probably need to kill and restart the explorer process.
Sep 28 '17 #6

ADezii
Expert 5K+
P: 8,623
A simple Refresh of the Explorer Window (F5) should do the trick. There should be no need to close and re-open Explorer, I think (LOL).
Sep 28 '17 #7

100+
P: 443
is there a way to do a refresh from with the Access VBA.
Sep 28 '17 #8

ADezii
Expert 5K+
P: 8,623
You would probably have to retrieve the Window Handle of Windows Explores and send it a Message to Close. Will look into this tomorrow if I have a chance.
Sep 28 '17 #9

100+
P: 443
Thanks, looking forward to seeing how this could be done.
Sep 28 '17 #10

ADezii
Expert 5K+
P: 8,623
  1. This was a little tougher than I realized, but I do believe that I have come up with a solution. It involves the following Steps:
    1. If Hide file extensions is turned ON, then Reset this Option to OFF.
    2. Find the Explorer Window and retrieve it's Handle.
    3. MAXIMIZE the Explorer Window.
    4. Send the F5 Keystroke (Refresh) to the Explorer Window.
  2. Required Declarations:
    Expand|Select|Wrap|Line Numbers
    1. Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
    2. Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal nCmdShow As Long) As Long
    3. Public Const SW_SHOWMAXIMIZED = 3
  3. Code Definition:
    Expand|Select|Wrap|Line Numbers
    1. Dim WshShell As Object
    2. Dim strRegKey As String
    3. Dim lngHnd As LongPtr
    4. Dim lngRetVal As Long
    5.  
    6. strRegKey = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\HideFileExt"
    7.  
    8. Set WshShell = CreateObject("WScript.Shell")
    9.  
    10. If WshShell.RegRead(strRegKey) Then
    11.   WshShell.RegWrite strRegKey, 0, "REG_DWORD"   'Enable File Extensions
    12.  
    13.   lngHnd = FindWindow("CabinetWClass", vbNullString)      'Find Windows Explorer
    14.   lngRetVal = ShowWindow(lngHnd, SW_SHOWMAXIMIZED)        'MAXIMIZE Windows Explorer
    15.  
    16.   WshShell.SendKeys "{F5}"        'Pass the F5 Key to Windows Explorer
    17. End If
    18.  
    19. Set WshShell = Nothing
Sep 29 '17 #11

100+
P: 443
This would seem to work except I get an error on the LongPtr do I need some new reference?
Sep 29 '17 #12

100+
P: 443
I still can't get this to work here's the code
Expand|Select|Wrap|Line Numbers
  1. Dim WshShell as Object
  2. Dim strRegKey as String
  3. Dim lngHnd As longPtr
  4. dim lngRetVal as long
  5.  
  6. strRegKey = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\HideFileExt"
  7. Set WshShell = CreateObject("WScript.Shell")
  8. If WshShell.RegRead(strRegKey) then
  9. WshShell.RegWrite strRegKey, 1, "REG_DWORD"
  10. lngHnd = FindWindow("CabinetWClass", vbNullString)
  11. lngRetVal = ShowWindow(lnghnd, SW_SHOWMAXIMIZED)
  12. WshShell.Sendkeys "(F5)"
  13. endif
  14. Set WshShell = nothing
  15.  
  16.  
I have set the extension in Explorer to 0 manually to test this
As I walk through the program (F8) it seems to go through everything until I get to the WshShell.SendKeys "(F5)" that command just writes a F5 on the next line of the code. If I look at the options in Explorer the hideFileExt still shows 0. No change.
I also get an error on the LongPtr I've changed it to just Long
I'm about ready to give up on this.
What do you think.
Sep 29 '17 #13

ADezii
Expert 5K+
P: 8,623
The correct Syntax for SendKeys is
Expand|Select|Wrap|Line Numbers
  1. WshShell.SendKeys "{F5}" 
not
Expand|Select|Wrap|Line Numbers
  1. WshShell.SendKeys "(F5)" 
Let me know how this works out, we're almost there. It works fine on my end, so must be a small adjustment on yours.
Sep 29 '17 #14

100+
P: 443
Well that was some difference I didn't notice the difference at first and was about to tell you it didn't work. when I looked closer I could see they were not ( but { when I made that change it worked. Thank you so much.
Sep 29 '17 #15

ADezii
Expert 5K+
P: 8,623
You are quite welcome, Tom. This was a tough one, gonna take a break now (LOL).
Sep 29 '17 #16

Post your reply

Sign in to post your reply or Sign up for a free account.