【问题标题】:Download file from website hyperlink in outlook从 Outlook 中的网站超链接下载文件
【发布时间】:2020-07-10 18:48:05
【问题描述】:

所以我使用以下代码从电子邮件中打开超链接。此超链接打开网页并打开下载窗口以选择下载 CSV 的位置和名称(所有这些都在 Chrome 中)。我希望能够选择所述文件的下载位置和名称。非常感谢您的帮助:)

Private Declare PtrSafe Function ShellExecute _
  Lib "shell32.dll" Alias "ShellExecuteA" ( _
  ByVal hWnd As Long, _
  ByVal Operation As String, _
  ByVal Filename As String, _
  Optional ByVal Parameters As String, _
  Optional ByVal Directory As String, _
  Optional ByVal WindowStyle As Long = vbMinimizedFocus _
  ) As Long

Public Sub OpenLinks(olMail As Outlook.MailItem)

 Dim Reg1 As RegExp
 Dim M1 As MatchCollection
 Dim M As Match
 Dim strURL As String
 Dim lSuccess As Long

Set Reg1 = New RegExp

With Reg1
 .Pattern = "(https?[:]//([0-9a-z=\?:/\.&-^!#$%;_])*)>"
 .Global = False
 .IgnoreCase = True
 End With

If Reg1.Test(olMail.Body) Then

Set M1 = Reg1.Execute(olMail.Body)
 For Each M In M1
   strURL = M.SubMatches(0)
   Debug.Print strURL

lSuccess = ShellExecute(0, "Open", strURL)

  Next
  End If

Set Reg1 = Nothing
Set oApp = Nothing

 End Sub

我查看了其他网站,但找不到类似的东西。

【问题讨论】:

  • @Namandeep_Kaur 看起来不错,问题是超链接直接在我的chrome浏览器中打开,一旦我尝试使用它,它什么也没做

标签: vba csv google-chrome outlook download


【解决方案1】:

您可以选择以下方式之一:

  1. 使用 Windows API,参见URLDownloadToFile 函数:
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

Public Function DownloadFile(URL As String, LocalFilename As String) As Boolean
Dim lngRetVal As Long
lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then 
    If Dir(LocalFileName) <> vbNullString Then 
        DownloadFile = True
    End If
End If
End Function

Private Sub Form_Load()
If Not DownloadFile("http://www.test.come", "c:\\file.doc") Then
    MsgBox "Unable to download the file, or the source URL doesn't exist."
End If
End Sub
  1. 使用 Windows API 函数以编程方式单击按钮,有关更多信息,请参阅 VBA - Go to website and download file from save prompt

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    相关资源
    最近更新 更多