Need to turn on file extension in windows within Access VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CD Tom
    Contributor
    • Feb 2009
    • 495

    Need to turn on file extension in windows within Access VBA

    I'm using the ImportZippedFil e 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 ImportZippedFil e 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 ImportZippedFil e let me know and I'll post it.

    Thanks for any help.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32645

    #2
    Can you be a little more precise. I know of configuring extensions but 'turning them on' means nothing to me.

    Comment

    • PhilOfWalton
      Recognized Expert Top Contributor
      • Mar 2016
      • 1430

      #3
      This may help.

      Basically I think you have to change

      HKEY_CURRENT_US ER\Software\Mic rosoft\Windows\ CurrentVersion\ Explorer\Advanc ed registry subkey.

      In the registry.

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

      Phil

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        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:
          Code:
          Dim WshShell As Object
          Dim strRegKey As String
          
          strRegKey = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\HideFileExt"
          
          Set WshShell = CreateObject("WScript.Shell")
          
          If WshShell.RegRead(strRegKey) Then
            WshShell.RegWrite strRegKey, 0, "REG_DWORD"
          End If
        4. Good Luck with your Project.

        Comment

        • CD Tom
          Contributor
          • Feb 2009
          • 495

          #5
          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 ImportZippedfil e 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.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            You probably need to kill and restart the explorer process.

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #7
              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).

              Comment

              • CD Tom
                Contributor
                • Feb 2009
                • 495

                #8
                is there a way to do a refresh from with the Access VBA.

                Comment

                • ADezii
                  Recognized Expert Expert
                  • Apr 2006
                  • 8834

                  #9
                  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.

                  Comment

                  • CD Tom
                    Contributor
                    • Feb 2009
                    • 495

                    #10
                    Thanks, looking forward to seeing how this could be done.

                    Comment

                    • ADezii
                      Recognized Expert Expert
                      • Apr 2006
                      • 8834

                      #11
                      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:
                        Code:
                        Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
                        Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal nCmdShow As Long) As Long
                        Public Const SW_SHOWMAXIMIZED = 3
                      3. Code Definition:
                        Code:
                        Dim WshShell As Object
                        Dim strRegKey As String
                        Dim lngHnd As LongPtr
                        Dim lngRetVal As Long
                        
                        strRegKey = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\HideFileExt"
                        
                        Set WshShell = CreateObject("WScript.Shell")
                        
                        If WshShell.RegRead(strRegKey) Then
                          WshShell.RegWrite strRegKey, 0, "REG_DWORD"   'Enable File Extensions
                          
                          lngHnd = FindWindow("CabinetWClass", vbNullString)      'Find Windows Explorer
                          lngRetVal = ShowWindow(lngHnd, SW_SHOWMAXIMIZED)        'MAXIMIZE Windows Explorer
                        
                          WshShell.SendKeys "{F5}"        'Pass the F5 Key to Windows Explorer
                        End If
                        
                        Set WshShell = Nothing

                      Comment

                      • CD Tom
                        Contributor
                        • Feb 2009
                        • 495

                        #12
                        This would seem to work except I get an error on the LongPtr do I need some new reference?

                        Comment

                        • CD Tom
                          Contributor
                          • Feb 2009
                          • 495

                          #13
                          I still can't get this to work here's the code
                          Code:
                          Dim WshShell as Object
                          Dim strRegKey as String
                          Dim lngHnd As longPtr
                          dim lngRetVal as long
                          
                          strRegKey = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\HideFileExt"
                          Set WshShell = CreateObject("WScript.Shell")
                          If WshShell.RegRead(strRegKey) then
                          WshShell.RegWrite strRegKey, 1, "REG_DWORD"
                          lngHnd = FindWindow("CabinetWClass", vbNullString)
                          lngRetVal = ShowWindow(lnghnd, SW_SHOWMAXIMIZED)
                          WshShell.Sendkeys "(F5)"
                          endif
                          Set WshShell = nothing
                          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.SendKe ys "(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.

                          Comment

                          • ADezii
                            Recognized Expert Expert
                            • Apr 2006
                            • 8834

                            #14
                            The correct Syntax for SendKeys is
                            Code:
                            WshShell.SendKeys "{F5}"
                            not
                            Code:
                            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.

                            Comment

                            • CD Tom
                              Contributor
                              • Feb 2009
                              • 495

                              #15
                              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.

                              Comment

                              Working...