Excel 2016 - Compile Error in Hidden Module

jeevaaviindos

New Member
Joined
Dec 20, 2016
Messages
2
I'm creating a simple report excel where it merge xls files when user select a folder. It was working perfectly until when i test in different PC, i got " Compile Error in Hidden Module" error. Below is the screenshot of the error.

pZuzA.jpg


Below are the MergeSheets module codes that i got from internet.

Code:
<code>Option Explicit
Public strPath As String
Public Type SELECTINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As SELECTINFO) As Long
Function SelectFolder(Optional Msg) As String
Dim sInfo As SELECTINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer
sInfo.pidlRoot = 0&

If IsMissing(Msg) Then
    sInfo.lpszTitle = "Select your folder."
Else
    sInfo.lpszTitle = Msg
End If

sInfo.ulFlags = &H1

x = SHBrowseForFolder(sInfo)

path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
    pos = InStr(path, Chr$(0))
    SelectFolder = Left(path, pos - 1)
Else
    SelectFolder = ""
End If
End Function

"Merging Part"
Sub MergeExcels()
Dim path As String, ThisWB As String, lngFilecounter As Long
Dim wbDest As Workbook, shtDest As Worksheet, ws As Worksheet
Dim Filename As String, Wkb As Workbook
Dim CopyRng As Range, Dest As Range
Dim RowofCopySheet As Integer

RowofCopySheet = 1 

ThisWB = ActiveWorkbook.Name

path = SelectFolder("Select a folder containing Excel files you want to merge")

Application.EnableEvents = False
Application.ScreenUpdating = False

Set shtDest = ActiveWorkbook.Sheets(1)
Filename = Dir(path & "\*.xls", vbNormal)
If Len(Filename) = 0 Then Exit Sub
Do Until Filename = vbNullString
    If Not Filename = ThisWB Then
        Set Wkb = Workbooks.Open(Filename:=path & "\" & Filename)
        Set CopyRng = Wkb.Sheets(1).Range(Cells(RowofCopySheet, 1), Cells(ActiveSheet.UsedRange.Rows.Count, ActiveSheet.UsedRange.Columns.Count))
        Set Dest = shtDest.Range("A" & shtDest.UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1)
        CopyRng.Copy Dest
        Wkb.Close False
    End If

    Filename = Dir()
Loop

Range("A1").Select

Application.EnableEvents = True
Application.ScreenUpdating = True

MsgBox "Files Merged!"
End Sub</code>

I managed find the source of problem which is the incompatibility of 32bit and 64bit OS. And i managed to find the solution where i need to include PtrSafe into my declare part. After i include the "PtrSafe" as below, the error is not showing up anymore.

Code:
<code>Private Declare PtrSafe Function SHGetPathFromIDList Lib "shell32.dll" _
                                 Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long


Private Declare PtrSafe Function SHBrowseForFolder Lib "shell32.dll" _
                               Alias "SHBrowseForFolderA" (lpBrowseInfo As SELECTINFO) As Long</code>

However when i execute the code, my Excel program crash.
Although this code is created for 32bit OS, it is working when i developing on my PC which is 64bit version. I even tried the same code on 2nd PC which also 64bit. However when i tried on 3rd PC which also 64bit, the error showed up.
Hope you give can give idea for this. Thanks.


**Update

-Seems that the 3rd pc is using Office 64bits thus not working.
-I even changed all the pointers to LongPtr. after change i get new error which is "Run-time error '1004' Cannot run macro".
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
See if this works for you ... modifications in red :

Code:
Option Explicit

Type SELECTINFO
        [COLOR=#ff0000]hOwner As LongPtr[/COLOR]
        [COLOR=#ff0000]pidlRoot As LongPtr[/COLOR]
        pszDisplayName As String
        lpszTitle As String
        ulFlags As Long
        [COLOR=#ff0000]lpfn As LongPtr[/COLOR]
        [COLOR=#ff0000]lParam As LongPtr[/COLOR]
        iImage As Long
End Type


Declare PtrSafe Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" _
        (ByVal pidl As LongPtr, ByVal pszPath As String) As [COLOR=#ff0000]LongPtr[/COLOR]
        
Declare PtrSafe Function SHBrowseForFolder Lib "shell32.dll" _
                               Alias "SHBrowseForFolderA" (lpBrowseInfo As SELECTINFO) As Long
                               
Function SelectFolder(Optional Msg) As String
Dim sInfo As SELECTINFO
Dim path As String
Dim [COLOR=#ff0000]r As LongPtr,[/COLOR] x As Long, pos As Integer
sInfo.pidlRoot = 0&


If IsMissing(Msg) Then
    sInfo.lpszTitle = "Select your folder."
Else
    sInfo.lpszTitle = Msg
End If


sInfo.ulFlags = &H1


x = SHBrowseForFolder(sInfo)


path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
    pos = InStr(path, Chr$(0))
    SelectFolder = Left(path, pos - 1)
Else
    SelectFolder = ""
End If
End Function


Sub MergeExcels()
Dim path As String, ThisWB As String, lngFilecounter As Long
Dim wbDest As Workbook, shtDest As Worksheet, ws As Worksheet
Dim Filename As String, Wkb As Workbook
Dim CopyRng As Range, Dest As Range
Dim RowofCopySheet As Integer


RowofCopySheet = 1


ThisWB = ActiveWorkbook.Name


path = SelectFolder("Select a folder containing Excel files you want to merge")


Application.EnableEvents = False
Application.ScreenUpdating = False


Set shtDest = ActiveWorkbook.Sheets(1)
Filename = Dir(path & "\*.xls", vbNormal)
If Len(Filename) = 0 Then Exit Sub
Do Until Filename = vbNullString
    If Not Filename = ThisWB Then
        Set Wkb = Workbooks.Open(Filename:=path & "" & Filename)
        Set CopyRng = Wkb.Sheets(1).Range(Cells(RowofCopySheet, 1), Cells(ActiveSheet.UsedRange.Rows.Count, ActiveSheet.UsedRange.Columns.Count))
        Set Dest = shtDest.Range("A" & shtDest.UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1)
        CopyRng.Copy Dest
        Wkb.Close False
    End If


    Filename = Dir()
Loop


Range("A1").Select


Application.EnableEvents = True
Application.ScreenUpdating = True


MsgBox "Files Merged!"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top