Sub refreshAndPrepareForCSVExport()
' 1. Gather parameters
Dim strPARAMETER_1 As String
strPARAMETER_1 = Range("NAMED_RANGE_OR_CELL_REF").Value
' 2. Update the
connection... customize if if necessary
With
ActiveWorkbook.Connections("CONNECTION_NAME").ODBCConnection
.BackgroundQuery = False
.CommandText = "exec DB.dbo.STORED_PROCEDURE_NAME " &
strPARAMETER_1 & ", 'HARD_CODED_PARAMETER_2_EXAMPLE';"
.RefreshOnFileOpen = False
.SavePassword = False
.SourceConnectionFile = ""
.SourceDataFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
End With
' 3. Refresh the
connection
ActiveWorkbook.Connections("CONNECTION_NAME").Refresh
' Save!
DoEvents
ActiveWorkbook.Save
' 4. Remove headers
Rows("1:4").Select
Selection.Delete Shift:=xlUp
' 5. Save as CSV
Dim strFileName As String
strFileName = _
CreateObject("WScript.Shell").specialfolders("Desktop")
& _
"\" & _
strFrom & "FILE_NAME.csv"
ActiveWorkbook.SaveAs _
Filename:=strFileName _
,
FileFormat:=xlCSV _
,
CreateBackup:=False
' 6. Close
ThisWorkbook.Close False
End Sub
nd Sub
No comments:
Post a Comment