Following up on @Vogel612's friendly advice from the vba-rubberducking
chat room, I am posting my working code for open review. Thanks also to @Mat'sMug for the help!
This will be my first task that I have completely automated via batch file scheduling, so I have included some spaces to pass error messages to the vbscript and batch file that will execute these macros. If some of the error handling seems a little excessive, it's because it is a zealous attempt by me to prevent any possible issues so that I am trusted to automate additional tasks via the scheduler in the future.
Part 1 is simply in charge of changing the list separator and quitting Excel so that Excel can be re-opened with the new list separator active.
Option Explicit
Private Declare Function SetLocaleInfo _
Lib "kernel32" Alias "SetLocaleInfoA" ( _
ByVal Locale As Long, _
ByVal LCType As Long, _
ByVal lpLCData As String) As Boolean
Private Declare Function GetUserDefaultLCID% Lib "kernel32" ()
Private Const LOCALE_SLIST = &HC
Private Const LOCALE_NAME_MAX_LENGTH = 85
Private Const LOCALE_NAME_USER_DEFAULT = vbNullString
'Get Locale Info
Private Declare Function GetLocaleInfoEx _
Lib "kernel32" ( _
ByVal lpLocaleName As String, _
ByVal LCType As Long, _
ByVal lpLCData As String, _
ByVal cchData As Long) As Long
Private Declare Function GetLastError Lib "kernel32" () As Long
Sub M1DelimiterSetup()
Dim lngTryAgainCtr As Long
Dim strBuffer As String
Dim strListSeparator As String
Dim lpLCData As String
Dim Long1 As Long
lngTryAgainCtr = 0
TryAgain:
lngTryAgainCtr = lngTryAgainCtr + 1
'Change delimiter to pipe
Call SetLocaleInfo(GetUserDefaultLCID(), LOCALE_SLIST, "|")
'Check to make sure setting separator as pipe worked correctly
strBuffer = String$(85, 0)
Long1 = GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SLIST, lpLCData, 0)
strListSeparator = String$(Long1, 0)
Long1 = GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SLIST, strListSeparator, Long1)
If Instr(strListSeparator, "|") = 0 Then
If lngTryAgainCtr < 3 Then
GoTo TryAgain
Else
If GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SLIST, strListSeparator, Long1) <> 0 Then Debug.Print GetLastError
'pass part 1 error message
'need error message for if run actively instead of part of script
End If
End If
'Close workbook to allow Excel to reset its memory of delimiter
'Show alerts if more workbooks open
If Workbooks.Count = 1 Then Application.DisplayAlerts = False
Application.Quit
End Sub
Part 2 downloads.
Option Explicit
'Set Locale Info
Private Declare Function SetLocaleInfo _
Lib "kernel32" Alias "SetLocaleInfoA" ( _
ByVal Locale As Long, _
ByVal LCType As Long, _
ByVal lpLCData As String) As Boolean
Private Declare Function GetUserDefaultLCID% Lib "kernel32" ()
Private Const LOCALE_SLIST = &HC
Private Const LOCALE_NAME_MAX_LENGTH = 85
Private Const LOCALE_NAME_USER_DEFAULT = vbNullString
'Get Locale Info
Private Declare Function GetLocaleInfoEx _
Lib "kernel32" ( _
ByVal lpLocaleName As String, _
ByVal LCType As Long, _
ByVal lpLCData As String, _
ByVal cchData As Long) As Long
Private Declare Function GetLastError Lib "kernel32" () As Long
Sub M2ProviderFileAutomation()
'
' M1ResponseSort Macro
'
' Keyboard Shortcut: Ctrl+Shift+Q
'
Dim strProvFileSaveLoc As String 'Full File Name
Dim strProvFileUnzipped As String 'Location of Text File after Unzipping
Dim strProvFileEITcsv As String 'Location in EIT folder where csv is saved
Dim strProvFileWebAddr As String 'url of file being downloaded
Dim Object1 As Object 'Shell Application
Dim Object2 As Object 'File system object
Dim Int1 As Integer 'Input file number (system-assigned number for file management)
Dim oXMLHTTP As Object
Dim oResp() As Byte
Dim Variant1 As Variant
Dim Variant2 As Variant
Dim Long1 As Long
Dim strBuffer As String
Dim strListSeparator As String
Dim lpLCData As String
'Check to make sure Part 1 ran correctly and separator is pipe
strBuffer = String$(85, 0)
Long1 = GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SLIST, lpLCData, 0)
strListSeparator = String$(Long1, 0)
Long1 = GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SLIST, strListSeparator, Long1)
If Instr(strListSeparator, "|") = 0 Then
If GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SLIST, strListSeparator, Long1) <> 0 Then Debug.Print GetLastError
'Pass Error Message about it not being ready from part 2
'need error message for if run actively instead of part of script
GoTo Cancel
End If
'Makes things go faster
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'Save the provider file
strProvFileWebAddr = "http://example.webaddress.com/filename.zip"
strProvFileSaveLoc = "\\example\completepath.zip"
strProvFileUnzipped = "\\exmple\completepath.txt"
'You can also set a ref. to Microsoft XML, and Dim oXMLHTTP as MSXML2.XMLHTTP
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP")
oXMLHTTP.Open "GET", strProvFileWebAddr, False 'Open socket to get the website
oXMLHTTP.Send 'send request
'Wait for request to finish
Do While oXMLHTTP.readyState <> 4
DoEvents
Loop
oResp = oXMLHTTP.responseBody 'Returns the results as a byte array
'Create local file and save results to it
Int1 = FreeFile()
If Dir(strProvFileSaveLoc) <> "" Then Kill strProvFileSaveLoc
Open strProvFileSaveLoc For Binary As #Int1
Put #Int1, , oResp
Close #Int1
'Clear memory
Set oXMLHTTP = Nothing
'Unzip zipped provider file
Set Object1 = CreateObject("Shell.Application")
'Has to be variants, can't be strings
Variant1 = "\\sample\directory\"
Variant2 = strProvFileSaveLoc
Object1.Namespace(Variant1).CopyHere Object1.Namespace(Variant2).items
On Error Resume Next
Set Object2 = CreateObject("scripting.filesystemobject")
Object2.DeleteFolder Environ("Temp") & "\Temporary Directory*", True
On Error GoTo 0
'Excel changes to provider file
Workbooks.OpenText strProvFileUnzipped, DataType:=xlDelimited, _
TextQualifier:=xlTextQualifierDoubleQuote, Other:=True, Otherchar:="|", FieldInfo:=Array(Array(1, 2), _
Array(2, 2), Array(3, 2), Array(4, 2), Array(5, 2), Array(6, 2), Array(7, 2), Array(8, 2), Array(9, 2), _
Array(10, 2), Array(11, 2), Array(12, 2), Array(13, 2), Array(14, 2), Array(15, 2), Array(16, 2), _
Array(17, 2), Array(18, 2), Array(19, 2), Array(20, 2), Array(21, 2), Array(22, 2), Array(23, 2), _
Array(24, 2))
'Delete Headers
ActiveWorkbook.Sheets(1).Rows(1).Delete
'Replace double quotes with single quotes in Columns B and C
ActiveWorkbook.Sheets(1).Columns("B:C").Replace What:="""", Replacement:="'", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
strProvFileEITcsv = "\\sample\directory\" & Format(Now, "mmddyy") & "FileName.csv"
ActiveWorkbook.SaveAs Filename:=strProvFileEITcsv, FileFormat:=xlCSV, local:=True
'Don't have permission to copy from EIT folder
ActiveWorkbook.SaveAs Filename:="\\other\directory\" & Format(Now, "mmddyy") & "Filename.csv", FileFormat:=xlCSV, local:=True
ActiveWorkbook.Close False
'Change delimiter back to comma
Call SetLocaleInfo(GetUserDefaultLCID(), LOCALE_SLIST, ",")
'Move zip file to archive
Name strProvFileSaveLoc As "\\archive\directory\" & Format(Now, "mm.dd.yy") & ".zip"
'Move txt file to archive
Name strProvFileUnzipped As "\\archive\directory\" & Format(Now, "mmddyy") & "Filename.txt"
'Cleanup nested folders from unzipped web file
RmDir "\\sample\path of third nested folder\"
RmDir "\\sample\path of second nested folder\"
RmDir "\\sample\path of first nested folder\"
RmDir "\\sample\path of outer folder\"
MsgBox "File done processing."
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
'Show alerts if more workbooks open
If Workbooks.Count = 1 Then Application.DisplayAlerts = False
Application.Quit
Cancel:
End Sub