Hi all,
VBA noob here - I've been given the task of documenting all of the excel connection strings used by a large variety of workbooks in a folder structure...
what I need is a tool that sniffs out connection strings and documents the file name, location and connection string info sequentially into a workbook or a SQL table.
So far - I have code that iteratively opens up workbooks in folder a structure (found elsewhere) but have no idea how to find and then write the information to a central repository...
The code I have is as below:
Any other help would be greatly appreciated! Thank you!
VBA noob here - I've been given the task of documenting all of the excel connection strings used by a large variety of workbooks in a folder structure...
what I need is a tool that sniffs out connection strings and documents the file name, location and connection string info sequentially into a workbook or a SQL table.
So far - I have code that iteratively opens up workbooks in folder a structure (found elsewhere) but have no idea how to find and then write the information to a central repository...
The code I have is as below:
Code:
Sub Test_Trawler()
On Error GoTo err_TT
'
' Test_Trawler Macro
'
Dim strFolder As String
Dim strFile As String
Dim strTemp As String
Dim strTest As String
Dim intTemp As String
'Set up our intial values
strFolder = Range("C3").Value
If Right(strFolder, 1) <> "\" Then
strFolder = strFolder & "\"
End If
'Check to see if there are any Excel files, and quit if not
strFile = Dir(strFolder & "*.xls*")
If strFile = "" Then
MsgBox "No Excel Files Found"
Exit Sub
End If
'Pick up the name of our temp folder
strTemp = Range("C4").Value
intTemp = 1
'Check to see if the temp folder exists
'If it does, add subscripts until we find one that works
If Dir(strFolder & strTemp, vbDirectory) <> "" Then
strTest = strTemp
Do Until strTest = ""
strTest = strTemp & CStr(intTemp)
If Dir(strFolder & strTest) = "" Then
strTemp = strTest
strTest = ""
Else
intTemp = intTemp + 1
End If
Loop
End If
'Create our temp folder
MkDir (strFolder & strTemp)
'Open each workbook in the folder in turn, process it, then move it to the temp folder
Do Until strFile = ""
Workbooks.Open strFolder & strFile
'Insert code to run while workbook is open
Workbooks(strFile).Close (False)
Call Safe_Move(strFolder & strFile, strFolder & strTemp & "\" & strFile)
strFile = Dir(strFolder & "*.xls*")
Loop
'Finished processing files, now move them from the temp folder back to the original
strFile = Dir(strFolder & strTemp & "\*.xls*")
Do Until strFile = ""
Call Safe_Move(strFolder & strTemp & "\" & strFile, strFolder & strFile)
strFile = Dir(strFolder & strTemp & "\*.xls*")
Loop
'Delete the temp folder
RmDir (strFolder & strTemp)
'Let the user know we've finished
MsgBox "The following folder has been processed: " & vbCrLf & strFolder, vbInformation, "Finished"
Exit Sub
err_TT:
Stop
Resume
'
End Sub
Function Safe_Move(strFrom As String, strTo As String) As Boolean
On Error GoTo err_SM
'Copy the file to the new location
FileCopy strFrom, strTo
'Check the copy has been created
If Dir(strTo) = "" Then
'Copy not found, alert the user
MsgBox "Failed to move file " & strFrom & " to " & strTo & vbCrLf & "Please investigate", vbCritical, "File move failed"
Safe_Move = False
Exit Function
Else
'Delete the original if the copy is found
Kill strFrom
End If
Safe_Move = True
Exit Function
err_SM:
Safe_Move = False
Stop
Resume
End Function
Any other help would be greatly appreciated! Thank you!