Hoping to get some help. I’m working with an older established Macro that would open the file browser and allow the selection of multiple files and then parse specific data from them into an excel sheet. I have updated it the best I can and will function except it will only transfer data from the last file selected. What I need is to be able to select multiple files and have it write a new Row for each file. Below is the code I’m using
<code>' DLL declarations
Type tagOPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
strFilter As String
strCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
strFile As String
nMaxFile As Long
strFileTitle As String
nMaxFileTitle As Long
strInitialDir As String
strTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
strDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
#If VBA7 Then
Declare PtrSafe Function aht_apiGetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean
#Else
Declare Function aht_apiGetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean
#End If
#If VBA7 Then
Declare PtrSafe Function aht_apiGetSaveFileName Lib "comdlg32.dll" _
Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
#Else
Declare Function aht_apiGetSaveFileName Lib "comdlg32.dll" _
Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
#End If
#If VBA7 Then
Declare PtrSafe Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
#Else
Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
#End If
Private Declare PtrSafe Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare PtrSafe Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
'// Functions
Function GetFromINI(sSection As String, sKey As String, sDefault As String, sIniFile As String)
Dim sBuffer As String, lRet As Long
' Fill String with 255 spaces
sBuffer = String$(255, 0)
' Call DLL
lRet = GetPrivateProfileString(sSection, sKey, "", sBuffer, Len(sBuffer), sIniFile)
If lRet = 0 Then
' DLL failed, save default
If sDefault <> "" Then AddToINI sSection, sKey, sDefault, sIniFile
GetFromINI = sDefault
Else
' DLL successful
' return string
GetFromINI = Left(sBuffer, InStr(sBuffer, Chr(0)) - 1)
End If
End Function
'// Returns True if successful. If section does not
'// exist it creates it.
Function AddToINI(sSection As String, sKey As String, sValue As String, sIniFile As String) As Boolean
Dim lRet As Long
' Call DLL
lRet = WritePrivateProfileString(sSection, sKey, sValue, sIniFile)
AddToINI = (lRet)
End Function
Sub Main()
Dim iRow As Integer
Dim INIFilePath1 As String
Dim DE_DS_0 As Variant
Dim DE_DS_1 As Variant
Dim INIFilePath As Variant
Dim INIFileLoop As String
Dim idx As Integer
INIFilePath1 = BrowseForFile
Application.ScreenUpdating = False
If Len(INIFilePath1) > 0 Then
strOutput = "You selected:" & vbCrLf
INIFilePath = Split(INIFilePath1, Chr$(0))
If UBound(INIFilePath) = 0 Then
idx = 0
Else
idx = 1
End If
For intLoop = idx To UBound(INIFilePath)
strOutput = strOutput & strFolder & INIFilePath(intLoop) & vbCrLf
If intLoop = 0 Then
INIFileLoop = INIFilePath(0)
Else
INIFileLoop = INIFilePath(0) & "" & INIFilePath(intLoop)
End If
If Len(INIFileLoop) > 0 Then
'Find next Blank Row
iRow = 2
Do Until IsEmpty(Cells(iRow, 1))
iRow = iRow + 1
Loop
On Error Resume Next
Cells(iRow, 6) = GetFromINI("DATAENTRY_1", "DE_NM", "", INIFileLoop) 'Data Entry Name
DE_DS_0 = Split(GetFromINI("DATAENTRY_1", "DE_DS_0", "", INIFileLoop), ",")
DE_DS_1 = Split(GetFromINI("DATAENTRY_1", "DE_DS_1", "", INIFileLoop), ",")
'DE_DS_1 = Split(GetFromINI("DATAENTRY_1", "DE_DS_1", "", INIFileLoop), ",")
Cells(iRow, 1) = DE_DS_1(2) 'Part Group
Cells(iRow, 2) = DE_DS_1(4) 'Part Name
Cells(iRow, 3) = DE_DS_0(2) 'Process Group
Cells(iRow, 4) = DE_DS_0(4) 'Process Name
Cells(iRow, 7) = INIFileLoop 'Project Path
Cells(iRow, 8) = DE_DS_1(3) 'Part ID #
Cells(iRow, 9) = DE_DS_0(3) ' Process ID #
End If
Next intLoop
End If
Application.ScreenUpdating = True
End Sub
Function BrowseForFile() As String
Dim strFilter As String
Dim lngFlags As Long
lngFlags = ahtOFN_FILEMUSTEXIST Or _
ahtOFN_HIDEREADONLY Or _
ahtOFN_NOCHANGEDIR Or _
ahtOFN_EXPLORER Or _
ahtOFN_ALLOWMULTISELECT
strFilter = ahtAddFilterItem(strFilter, "InfinityQS Files (*.ipj)", "*.IPJ")
strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)", "*.TXT")
strFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")
BrowseForFile = ahtCommonFileOpenSave(InitialDir:="T:InfinityQS ProjectProjects_SPC", _
Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
DialogTitle:="File Browser")
' Since you passed in a variable for lngFlags,
' the function places the output flags value in the variable.
End Function
Function ahtAddFilterItem(strFilter As String, _
strDescription As String, Optional varItem As Variant) As String
' Tack a new chunk onto the file filter.
' That is, take the old value, stick onto it the description,
' (like "Databases"), a null character, the skeleton
' (like "*.mdb;*.mda") and a final null character.
If IsMissing(varItem) Then varItem = "*.*"
ahtAddFilterItem = strFilter & _
strDescription & vbNullChar & _
varItem & vbNullChar
End Function
Function ahtCommonFileOpenSave( _
Optional ByRef Flags As Variant, _
Optional ByVal InitialDir As Variant, _
Optional ByVal Filter As Variant, _
Optional ByVal FilterIndex As Variant, _
Optional ByVal DefaultExt As Variant, _
Optional ByVal FileName As Variant, _
Optional ByVal DialogTitle As Variant, _
Optional ByVal hwnd As Variant, _
Optional ByVal OpenFile As Variant) As Variant
If IsMissing(InitialDir) Then InitialDir = CurDir
If IsMissing(Filter) Then Filter = ""
If IsMissing(FilterIndex) Then FilterIndex = 1
If IsMissing(OpenFile) Then OpenFile = True
If IsMissing(DialogTitle) Then DialogTitle = ""
#If VBA7 Then
If OpenFile Then
With Application.FileDialog(3) ' const msoFileDialogFilePicker = 3
.AllowMultiSelect = True
' deal with filters
.Filters.Clear
Dim FiltersArray() As String
FiltersArray = Split(Filter, vbNullChar)
For ct = 0 To UBound(FiltersArray) - 2 Step 2
.Filters.Add FiltersArray(i), FiltersArray(i + 1)
Next
.FilterIndex = FilterIndex
.Title = DialogTitle
If .Show <> 0 Then
ahtCommonFileOpenSave = .SelectedItems(1)
Else
ahtCommonFileOpenSave = vbNullString
End If
End With
Else
' not sure how to do a save file in new VBA... 20181127 Jace Gregg
End If
#Else
' This is the entry point you'll use to call the common
' file open/save dialog. The parameters are listed
' below, and all are optional.
'
' In:
' Flags: one or more of the ahtOFN_* constants, OR'd together.
' InitialDir: the directory in which to first look
' Filter: a set of file filters, set up by calling
' AddFilterItem. See examples.
' FilterIndex: 1-based integer indicating which filter
' set to use, by default (1 if unspecified)
' DefaultExt: Extension to use if the user doesn't enter one.
' Only useful on file saves.
' FileName: Default value for the file name text box.
' DialogTitle: Title for the dialog.
' hWnd: parent window handle
' OpenFile: Boolean(True=Open File/False=Save As)
' Out:
' Return Value: Either Null or the selected filename
Dim OFN As tagOPENFILENAME
Dim strFilename As String
Dim strFileTitle As String
Dim fResult As Boolean
' Give the dialog a caption title.
If IsMissing(Flags) Then Flags = 0&
If IsMissing(DefaultExt) Then DefaultExt = ""
If IsMissing(FileName) Then FileName = ""
If IsMissing(hwnd) Then hwnd = Application.hWndAccessApp
' Allocate string space for the returned strings.
strFilename = Left(FileName & String(256, 0), 256)
strFileTitle = String(256, 0)
' Set up the data structure before you call the function
With OFN
.lStructSize = Len(OFN)
.hwndOwner = hwnd
.strFilter = Filter
.nFilterIndex = FilterIndex
.strFile = strFilename
.nMaxFile = Len(strFilename)
.strFileTitle = strFileTitle
.nMaxFileTitle = Len(strFileTitle)
.strTitle = DialogTitle
.Flags = Flags
.strDefExt = DefaultExt
.strInitialDir = InitialDir
' Didn't think most people would want to deal with
' these options.
.hInstance = 0
'.strCustomFilter = ""
'.nMaxCustFilter = 0
.lpfnHook = 0
'New for NT 4.0
.strCustomFilter = String(255, 0)
.nMaxCustFilter = 255
End With
' This will pass the desired data structure to the
' Windows API, which will in turn it uses to display
' the Open/Save As Dialog.
If OpenFile Then
fResult = aht_apiGetOpenFileName(OFN)
Else
fResult = aht_apiGetSaveFileName(OFN)
End If
' The function call filled in the strFileTitle member
' of the structure. You'll have to write special code
' to retrieve that if you're interested.
If fResult Then
' You might care to check the Flags member of the
' structure to get information about the chosen file.
' In this example, if you bothered to pass in a
' value for Flags, we'll fill it in with the outgoing
' Flags value.
If Not IsMissing(Flags) Then Flags = OFN.Flags
ahtCommonFileOpenSave = TrimNull(OFN.strFile)
Else
ahtCommonFileOpenSave = vbNullString
End If
#End If
End Function
</code>
<code>' DLL declarations
Type tagOPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
strFilter As String
strCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
strFile As String
nMaxFile As Long
strFileTitle As String
nMaxFileTitle As Long
strInitialDir As String
strTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
strDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
#If VBA7 Then
Declare PtrSafe Function aht_apiGetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean
#Else
Declare Function aht_apiGetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean
#End If
#If VBA7 Then
Declare PtrSafe Function aht_apiGetSaveFileName Lib "comdlg32.dll" _
Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
#Else
Declare Function aht_apiGetSaveFileName Lib "comdlg32.dll" _
Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
#End If
#If VBA7 Then
Declare PtrSafe Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
#Else
Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
#End If
Private Declare PtrSafe Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare PtrSafe Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
'// Functions
Function GetFromINI(sSection As String, sKey As String, sDefault As String, sIniFile As String)
Dim sBuffer As String, lRet As Long
' Fill String with 255 spaces
sBuffer = String$(255, 0)
' Call DLL
lRet = GetPrivateProfileString(sSection, sKey, "", sBuffer, Len(sBuffer), sIniFile)
If lRet = 0 Then
' DLL failed, save default
If sDefault <> "" Then AddToINI sSection, sKey, sDefault, sIniFile
GetFromINI = sDefault
Else
' DLL successful
' return string
GetFromINI = Left(sBuffer, InStr(sBuffer, Chr(0)) - 1)
End If
End Function
'// Returns True if successful. If section does not
'// exist it creates it.
Function AddToINI(sSection As String, sKey As String, sValue As String, sIniFile As String) As Boolean
Dim lRet As Long
' Call DLL
lRet = WritePrivateProfileString(sSection, sKey, sValue, sIniFile)
AddToINI = (lRet)
End Function
Sub Main()
Dim iRow As Integer
Dim INIFilePath1 As String
Dim DE_DS_0 As Variant
Dim DE_DS_1 As Variant
Dim INIFilePath As Variant
Dim INIFileLoop As String
Dim idx As Integer
INIFilePath1 = BrowseForFile
Application.ScreenUpdating = False
If Len(INIFilePath1) > 0 Then
strOutput = "You selected:" & vbCrLf
INIFilePath = Split(INIFilePath1, Chr$(0))
If UBound(INIFilePath) = 0 Then
idx = 0
Else
idx = 1
End If
For intLoop = idx To UBound(INIFilePath)
strOutput = strOutput & strFolder & INIFilePath(intLoop) & vbCrLf
If intLoop = 0 Then
INIFileLoop = INIFilePath(0)
Else
INIFileLoop = INIFilePath(0) & "" & INIFilePath(intLoop)
End If
If Len(INIFileLoop) > 0 Then
'Find next Blank Row
iRow = 2
Do Until IsEmpty(Cells(iRow, 1))
iRow = iRow + 1
Loop
On Error Resume Next
Cells(iRow, 6) = GetFromINI("DATAENTRY_1", "DE_NM", "", INIFileLoop) 'Data Entry Name
DE_DS_0 = Split(GetFromINI("DATAENTRY_1", "DE_DS_0", "", INIFileLoop), ",")
DE_DS_1 = Split(GetFromINI("DATAENTRY_1", "DE_DS_1", "", INIFileLoop), ",")
'DE_DS_1 = Split(GetFromINI("DATAENTRY_1", "DE_DS_1", "", INIFileLoop), ",")
Cells(iRow, 1) = DE_DS_1(2) 'Part Group
Cells(iRow, 2) = DE_DS_1(4) 'Part Name
Cells(iRow, 3) = DE_DS_0(2) 'Process Group
Cells(iRow, 4) = DE_DS_0(4) 'Process Name
Cells(iRow, 7) = INIFileLoop 'Project Path
Cells(iRow, 8) = DE_DS_1(3) 'Part ID #
Cells(iRow, 9) = DE_DS_0(3) ' Process ID #
End If
Next intLoop
End If
Application.ScreenUpdating = True
End Sub
Function BrowseForFile() As String
Dim strFilter As String
Dim lngFlags As Long
lngFlags = ahtOFN_FILEMUSTEXIST Or _
ahtOFN_HIDEREADONLY Or _
ahtOFN_NOCHANGEDIR Or _
ahtOFN_EXPLORER Or _
ahtOFN_ALLOWMULTISELECT
strFilter = ahtAddFilterItem(strFilter, "InfinityQS Files (*.ipj)", "*.IPJ")
strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)", "*.TXT")
strFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")
BrowseForFile = ahtCommonFileOpenSave(InitialDir:="T:InfinityQS ProjectProjects_SPC", _
Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
DialogTitle:="File Browser")
' Since you passed in a variable for lngFlags,
' the function places the output flags value in the variable.
End Function
Function ahtAddFilterItem(strFilter As String, _
strDescription As String, Optional varItem As Variant) As String
' Tack a new chunk onto the file filter.
' That is, take the old value, stick onto it the description,
' (like "Databases"), a null character, the skeleton
' (like "*.mdb;*.mda") and a final null character.
If IsMissing(varItem) Then varItem = "*.*"
ahtAddFilterItem = strFilter & _
strDescription & vbNullChar & _
varItem & vbNullChar
End Function
Function ahtCommonFileOpenSave( _
Optional ByRef Flags As Variant, _
Optional ByVal InitialDir As Variant, _
Optional ByVal Filter As Variant, _
Optional ByVal FilterIndex As Variant, _
Optional ByVal DefaultExt As Variant, _
Optional ByVal FileName As Variant, _
Optional ByVal DialogTitle As Variant, _
Optional ByVal hwnd As Variant, _
Optional ByVal OpenFile As Variant) As Variant
If IsMissing(InitialDir) Then InitialDir = CurDir
If IsMissing(Filter) Then Filter = ""
If IsMissing(FilterIndex) Then FilterIndex = 1
If IsMissing(OpenFile) Then OpenFile = True
If IsMissing(DialogTitle) Then DialogTitle = ""
#If VBA7 Then
If OpenFile Then
With Application.FileDialog(3) ' const msoFileDialogFilePicker = 3
.AllowMultiSelect = True
' deal with filters
.Filters.Clear
Dim FiltersArray() As String
FiltersArray = Split(Filter, vbNullChar)
For ct = 0 To UBound(FiltersArray) - 2 Step 2
.Filters.Add FiltersArray(i), FiltersArray(i + 1)
Next
.FilterIndex = FilterIndex
.Title = DialogTitle
If .Show <> 0 Then
ahtCommonFileOpenSave = .SelectedItems(1)
Else
ahtCommonFileOpenSave = vbNullString
End If
End With
Else
' not sure how to do a save file in new VBA... 20181127 Jace Gregg
End If
#Else
' This is the entry point you'll use to call the common
' file open/save dialog. The parameters are listed
' below, and all are optional.
'
' In:
' Flags: one or more of the ahtOFN_* constants, OR'd together.
' InitialDir: the directory in which to first look
' Filter: a set of file filters, set up by calling
' AddFilterItem. See examples.
' FilterIndex: 1-based integer indicating which filter
' set to use, by default (1 if unspecified)
' DefaultExt: Extension to use if the user doesn't enter one.
' Only useful on file saves.
' FileName: Default value for the file name text box.
' DialogTitle: Title for the dialog.
' hWnd: parent window handle
' OpenFile: Boolean(True=Open File/False=Save As)
' Out:
' Return Value: Either Null or the selected filename
Dim OFN As tagOPENFILENAME
Dim strFilename As String
Dim strFileTitle As String
Dim fResult As Boolean
' Give the dialog a caption title.
If IsMissing(Flags) Then Flags = 0&
If IsMissing(DefaultExt) Then DefaultExt = ""
If IsMissing(FileName) Then FileName = ""
If IsMissing(hwnd) Then hwnd = Application.hWndAccessApp
' Allocate string space for the returned strings.
strFilename = Left(FileName & String(256, 0), 256)
strFileTitle = String(256, 0)
' Set up the data structure before you call the function
With OFN
.lStructSize = Len(OFN)
.hwndOwner = hwnd
.strFilter = Filter
.nFilterIndex = FilterIndex
.strFile = strFilename
.nMaxFile = Len(strFilename)
.strFileTitle = strFileTitle
.nMaxFileTitle = Len(strFileTitle)
.strTitle = DialogTitle
.Flags = Flags
.strDefExt = DefaultExt
.strInitialDir = InitialDir
' Didn't think most people would want to deal with
' these options.
.hInstance = 0
'.strCustomFilter = ""
'.nMaxCustFilter = 0
.lpfnHook = 0
'New for NT 4.0
.strCustomFilter = String(255, 0)
.nMaxCustFilter = 255
End With
' This will pass the desired data structure to the
' Windows API, which will in turn it uses to display
' the Open/Save As Dialog.
If OpenFile Then
fResult = aht_apiGetOpenFileName(OFN)
Else
fResult = aht_apiGetSaveFileName(OFN)
End If
' The function call filled in the strFileTitle member
' of the structure. You'll have to write special code
' to retrieve that if you're interested.
If fResult Then
' You might care to check the Flags member of the
' structure to get information about the chosen file.
' In this example, if you bothered to pass in a
' value for Flags, we'll fill it in with the outgoing
' Flags value.
If Not IsMissing(Flags) Then Flags = OFN.Flags
ahtCommonFileOpenSave = TrimNull(OFN.strFile)
Else
ahtCommonFileOpenSave = vbNullString
End If
#End If
End Function
</code>
' DLL declarations
Type tagOPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
strFilter As String
strCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
strFile As String
nMaxFile As Long
strFileTitle As String
nMaxFileTitle As Long
strInitialDir As String
strTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
strDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
#If VBA7 Then
Declare PtrSafe Function aht_apiGetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean
#Else
Declare Function aht_apiGetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean
#End If
#If VBA7 Then
Declare PtrSafe Function aht_apiGetSaveFileName Lib "comdlg32.dll" _
Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
#Else
Declare Function aht_apiGetSaveFileName Lib "comdlg32.dll" _
Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
#End If
#If VBA7 Then
Declare PtrSafe Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
#Else
Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
#End If
Private Declare PtrSafe Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare PtrSafe Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
'// Functions
Function GetFromINI(sSection As String, sKey As String, sDefault As String, sIniFile As String)
Dim sBuffer As String, lRet As Long
' Fill String with 255 spaces
sBuffer = String$(255, 0)
' Call DLL
lRet = GetPrivateProfileString(sSection, sKey, "", sBuffer, Len(sBuffer), sIniFile)
If lRet = 0 Then
' DLL failed, save default
If sDefault <> "" Then AddToINI sSection, sKey, sDefault, sIniFile
GetFromINI = sDefault
Else
' DLL successful
' return string
GetFromINI = Left(sBuffer, InStr(sBuffer, Chr(0)) - 1)
End If
End Function
'// Returns True if successful. If section does not
'// exist it creates it.
Function AddToINI(sSection As String, sKey As String, sValue As String, sIniFile As String) As Boolean
Dim lRet As Long
' Call DLL
lRet = WritePrivateProfileString(sSection, sKey, sValue, sIniFile)
AddToINI = (lRet)
End Function
Sub Main()
Dim iRow As Integer
Dim INIFilePath1 As String
Dim DE_DS_0 As Variant
Dim DE_DS_1 As Variant
Dim INIFilePath As Variant
Dim INIFileLoop As String
Dim idx As Integer
INIFilePath1 = BrowseForFile
Application.ScreenUpdating = False
If Len(INIFilePath1) > 0 Then
strOutput = "You selected:" & vbCrLf
INIFilePath = Split(INIFilePath1, Chr$(0))
If UBound(INIFilePath) = 0 Then
idx = 0
Else
idx = 1
End If
For intLoop = idx To UBound(INIFilePath)
strOutput = strOutput & strFolder & INIFilePath(intLoop) & vbCrLf
If intLoop = 0 Then
INIFileLoop = INIFilePath(0)
Else
INIFileLoop = INIFilePath(0) & "" & INIFilePath(intLoop)
End If
If Len(INIFileLoop) > 0 Then
'Find next Blank Row
iRow = 2
Do Until IsEmpty(Cells(iRow, 1))
iRow = iRow + 1
Loop
On Error Resume Next
Cells(iRow, 6) = GetFromINI("DATAENTRY_1", "DE_NM", "", INIFileLoop) 'Data Entry Name
DE_DS_0 = Split(GetFromINI("DATAENTRY_1", "DE_DS_0", "", INIFileLoop), ",")
DE_DS_1 = Split(GetFromINI("DATAENTRY_1", "DE_DS_1", "", INIFileLoop), ",")
'DE_DS_1 = Split(GetFromINI("DATAENTRY_1", "DE_DS_1", "", INIFileLoop), ",")
Cells(iRow, 1) = DE_DS_1(2) 'Part Group
Cells(iRow, 2) = DE_DS_1(4) 'Part Name
Cells(iRow, 3) = DE_DS_0(2) 'Process Group
Cells(iRow, 4) = DE_DS_0(4) 'Process Name
Cells(iRow, 7) = INIFileLoop 'Project Path
Cells(iRow, 8) = DE_DS_1(3) 'Part ID #
Cells(iRow, 9) = DE_DS_0(3) ' Process ID #
End If
Next intLoop
End If
Application.ScreenUpdating = True
End Sub
Function BrowseForFile() As String
Dim strFilter As String
Dim lngFlags As Long
lngFlags = ahtOFN_FILEMUSTEXIST Or _
ahtOFN_HIDEREADONLY Or _
ahtOFN_NOCHANGEDIR Or _
ahtOFN_EXPLORER Or _
ahtOFN_ALLOWMULTISELECT
strFilter = ahtAddFilterItem(strFilter, "InfinityQS Files (*.ipj)", "*.IPJ")
strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)", "*.TXT")
strFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")
BrowseForFile = ahtCommonFileOpenSave(InitialDir:="T:InfinityQS ProjectProjects_SPC", _
Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
DialogTitle:="File Browser")
' Since you passed in a variable for lngFlags,
' the function places the output flags value in the variable.
End Function
Function ahtAddFilterItem(strFilter As String, _
strDescription As String, Optional varItem As Variant) As String
' Tack a new chunk onto the file filter.
' That is, take the old value, stick onto it the description,
' (like "Databases"), a null character, the skeleton
' (like "*.mdb;*.mda") and a final null character.
If IsMissing(varItem) Then varItem = "*.*"
ahtAddFilterItem = strFilter & _
strDescription & vbNullChar & _
varItem & vbNullChar
End Function
Function ahtCommonFileOpenSave( _
Optional ByRef Flags As Variant, _
Optional ByVal InitialDir As Variant, _
Optional ByVal Filter As Variant, _
Optional ByVal FilterIndex As Variant, _
Optional ByVal DefaultExt As Variant, _
Optional ByVal FileName As Variant, _
Optional ByVal DialogTitle As Variant, _
Optional ByVal hwnd As Variant, _
Optional ByVal OpenFile As Variant) As Variant
If IsMissing(InitialDir) Then InitialDir = CurDir
If IsMissing(Filter) Then Filter = ""
If IsMissing(FilterIndex) Then FilterIndex = 1
If IsMissing(OpenFile) Then OpenFile = True
If IsMissing(DialogTitle) Then DialogTitle = ""
#If VBA7 Then
If OpenFile Then
With Application.FileDialog(3) ' const msoFileDialogFilePicker = 3
.AllowMultiSelect = True
' deal with filters
.Filters.Clear
Dim FiltersArray() As String
FiltersArray = Split(Filter, vbNullChar)
For ct = 0 To UBound(FiltersArray) - 2 Step 2
.Filters.Add FiltersArray(i), FiltersArray(i + 1)
Next
.FilterIndex = FilterIndex
.Title = DialogTitle
If .Show <> 0 Then
ahtCommonFileOpenSave = .SelectedItems(1)
Else
ahtCommonFileOpenSave = vbNullString
End If
End With
Else
' not sure how to do a save file in new VBA... 20181127 Jace Gregg
End If
#Else
' This is the entry point you'll use to call the common
' file open/save dialog. The parameters are listed
' below, and all are optional.
'
' In:
' Flags: one or more of the ahtOFN_* constants, OR'd together.
' InitialDir: the directory in which to first look
' Filter: a set of file filters, set up by calling
' AddFilterItem. See examples.
' FilterIndex: 1-based integer indicating which filter
' set to use, by default (1 if unspecified)
' DefaultExt: Extension to use if the user doesn't enter one.
' Only useful on file saves.
' FileName: Default value for the file name text box.
' DialogTitle: Title for the dialog.
' hWnd: parent window handle
' OpenFile: Boolean(True=Open File/False=Save As)
' Out:
' Return Value: Either Null or the selected filename
Dim OFN As tagOPENFILENAME
Dim strFilename As String
Dim strFileTitle As String
Dim fResult As Boolean
' Give the dialog a caption title.
If IsMissing(Flags) Then Flags = 0&
If IsMissing(DefaultExt) Then DefaultExt = ""
If IsMissing(FileName) Then FileName = ""
If IsMissing(hwnd) Then hwnd = Application.hWndAccessApp
' Allocate string space for the returned strings.
strFilename = Left(FileName & String(256, 0), 256)
strFileTitle = String(256, 0)
' Set up the data structure before you call the function
With OFN
.lStructSize = Len(OFN)
.hwndOwner = hwnd
.strFilter = Filter
.nFilterIndex = FilterIndex
.strFile = strFilename
.nMaxFile = Len(strFilename)
.strFileTitle = strFileTitle
.nMaxFileTitle = Len(strFileTitle)
.strTitle = DialogTitle
.Flags = Flags
.strDefExt = DefaultExt
.strInitialDir = InitialDir
' Didn't think most people would want to deal with
' these options.
.hInstance = 0
'.strCustomFilter = ""
'.nMaxCustFilter = 0
.lpfnHook = 0
'New for NT 4.0
.strCustomFilter = String(255, 0)
.nMaxCustFilter = 255
End With
' This will pass the desired data structure to the
' Windows API, which will in turn it uses to display
' the Open/Save As Dialog.
If OpenFile Then
fResult = aht_apiGetOpenFileName(OFN)
Else
fResult = aht_apiGetSaveFileName(OFN)
End If
' The function call filled in the strFileTitle member
' of the structure. You'll have to write special code
' to retrieve that if you're interested.
If fResult Then
' You might care to check the Flags member of the
' structure to get information about the chosen file.
' In this example, if you bothered to pass in a
' value for Flags, we'll fill it in with the outgoing
' Flags value.
If Not IsMissing(Flags) Then Flags = OFN.Flags
ahtCommonFileOpenSave = TrimNull(OFN.strFile)
Else
ahtCommonFileOpenSave = vbNullString
End If
#End If
End Function
New contributor
Michael Slogar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.