我有下载和操作 URL 代码的代码,以从前一个工作日下载相应的 zip 文件。宏然后在 zip 文件中提取 csv 文件(参见下面的代码)并放置在以下文件夹中:\Commercial Gas and Power Analytics\Margin Calculator”。理想情况下,我想引用目录中的最新 CVS/ Windows 脚本主机程序中的文件夹,如下所示:
wsh.currentDirectory = 当前目录 wsh.Run "cmd.exe /S /C marbat.exe -rf IPE0207F.csv -pf Position.csv -of results -od -wfpcrap", windowStyle, waitOnReturn
函数 GetUrl() 作为字符串 将日期变暗为日期 Dim sURl 为字符串,sDate 为字符串,sYm 为字符串,sMd 为字符串
aDate = Application.WorksheetFunction.WorkDay(Now, -1)
sYm = Format(aDate, "yyyy") & "/" & Format(aDate, "MM")
sMd = Format(aDate, "MMdd")
sURl = "https://www.theice.com/publicdocs/irm_files/iceu/" & sYm & "/IPE" & sMd & "F.CSV.zip"
GetUrl = sURl
子下载ZipExtractCsvAndLoad()
Dim UrlFile 为字符串,ZipFile 为字符串,CsvFile 为字符串,文件夹为字符串,s 为字符串
'调用这个子函数' 网址文件 = GetUrl
' 从 UrlFile 中提取 ZipFile、CsvFile ZipFile = Mid(UrlFile, InStrRev(UrlFile, "/") + 1) CsvFile = Left(ZipFile, Len(ZipFile) - 4)
' 定义临时文件夹
Folder = "T:\Commercial Gas and Power Analytics\Margin Calculator"
' 禁用屏幕更新以避免闪烁 Application.ScreenUpdating = 假
' 陷阱错误 错误转到退出_
' 下载 UrlFile 到文件夹中的 ZipFile 如果不是 Url2File(UrlFile, Folder & ZipFile) Then MsgBox "无法下载文件" & vbLf & UrlFile, vbCritical, "错误" 退出子 结束如果
' 从 ZipFile 中提取 CsvFile
With CreateObject("Shell.Application").Namespace((Folder)) .CopyHere 文件夹 & ZipFile & "" & CsvFile 结束于
退出_:
' 恢复屏幕更新 应用程序.ScreenUpdating = True
' 告知被困错误的原因 If Err Then MsgBox Err.Description, vbCritical, "错误"
函数 Url2File(UrlFile 作为字符串,PathName 作为字符串,可选登录作为字符串,可选密码作为字符串)作为布尔值 'ZVI:2017-01-07 下载 UrlFile 并将其保存到 PathName。 ' 如果需要,使用可选的登录名和密码。 ' 下载成功返回 True。 Dim b() 作为字节,FN 作为整数 错误转到退出_ If Len(Dir(PathName)) Then Kill PathName 使用 CreateObject("MSXML2.XMLHTTP") .Open "GET", UrlFile, False, Login, Password 。发送 If .Status <> 200 Then Exit 函数 b() = .responseBody FN = 自由文件 二进制访问的开放路径名写为#FN 放#FN, , b() 出口_: 如果 FN 则关闭 #FN Url2File = .Status = 200 结束于 结束功能