VBA从文件名每天不断更改的URL下载文件

时间:2016-04-01 15:35:34

标签: excel excel-vba url vba

有人帮助我,因为我把头发拉了出来。我试图从网站上下载文件&将其复制到另一个位置。如果我使用下面注释掉的实际文件名,它可以工作。可悲的是,文件名每天都在变化。我在小区B1(01 04 2016)中已经考虑到了这一点。无论如何,我得到错误的编译错误:需要持续表达'并突出显示第一个''。有没有人有任何可能有用的想法?

{

Option Explicit

Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
                                           "URLDownloadToFileA" ( _
                                           ByVal pCaller As Long, ByVal szURL As String, _
                                           ByVal szFileName As String, _
                                           ByVal dwReserved As Long, _
                                           ByVal lpfnCB As Long) As Long


`enter code here`Sub DownloadFileFromWeb()
Dim i As Integer

    Const strUrl As String = "https://test.com/sites/Field Completions Reported " & Range("B1") & ".xlsb"
    '"https://test.com/sites/Field Completions Reported 01 04 2016.xlsb"

    Dim strSavePath As String
    Dim returnValue As Long

    strSavePath = "C:\Users\1234\Desktop\Field Completions Reported.xlsb"
    returnValue = URLDownloadToFile(0, strUrl, strSavePath, 0, 0)

End Sub

}

1 个答案:

答案 0 :(得分:2)

  

编译错误:需要常量表达式

您不能将ConstVariable一起使用。当你使用Const时,传递一些不会改变的东西。例如

Const a = "Sid" '<~~~ This will work
Const a = "Sid" & i '<~~ This will not work. i is a variable
Const a = "Sid" & Range("A1").Value  '<~~ This will not work. Range("A1") is a variable

将您的代码更改为此

Dim strUrl As String

'"https://test.com/sites/Field Completions Reported 01 04 2016.xlsb"
strUrl = "https://test.com/sites/Field Completions Reported " & _
         Range("B1").Value & ".xlsb"