【问题标题】:Download file using VBA for Mac Word 2016使用 VBA for Mac Word 2016 下载文件
【发布时间】:2018-08-28 22:29:21
【问题描述】:

我正在尝试更新 Word 2016 for Mac 的模板。

在之前的代码中,我能够运行 MacScript() 命令来运行 AppleScript,然后再运行 shell 脚本。

现在运行这样一个脚本的唯一方法似乎是使用 AppleScriptTask,这要求该脚本已经存在于 Application Scripts 文件夹中,当我尝试将其分发给其他(非精通)用户时会出现问题.

我正在尝试找出替代方法,但经过数周的研究,我仍然感到困惑。

我正在运行的脚本可以做各种各样的事情,但现在最重要的是从网站下载模板的更新版本。我在 Windows 端使用 ActiveX 来执行此操作,但在 Mac 上无法执行此操作。

任何人都可以为 Mac Word 2016 建议任何替代方法,使用 VBA(最好)?

谢谢!

【问题讨论】:

  • 我在下面使用了 Rich 的建议。它运作良好。我将它下载到默认的下载文件夹,然后将其移动到我需要的位置(使用 VBA)。

标签: vba macos ms-word


【解决方案1】:

试试这个:

ActiveDocument.FollowHyperlink "http://yoursite.com/yourpackage.zip"

【讨论】:

  • 这适用于下载文件。但它似乎没有让我控制文件下载的位置,或者给我下载位置,虽然我没有深入了解它,但似乎试图找到浏览器的默认保存位置导致更多并发症。
  • 默认为用户目录下的Downloads文件夹。
  • 是的。我只是在想如果有人更改默认下载文件夹,我将无法找到它。
【解决方案2】:

以下是我为实现此目的所做的。我设置了一个计时器来等待下载,因为它是在 VBA 之外完成的。

Function Download(Path As String) As String

On Error GoTo Handler
Dim User, UserPath, dlFile, base_dest, final_dest, FName As String
Dim Timer As Boolean
Timer = False
Dim timeout As Variant

timeout = Now + TimeValue("00:00:10")
User = Environ("USER")
UserPath = "/Users/" & User & "/Downloads/"
base_dest = "/Users/" & User & [move/path/]
final_dest = base_dest & FName
ActiveDocument.FollowHyperlink Address:=Path

If Dir(final_dest) <> "" Then
    Kill final_dest
End If

Timer = True
ReTry:
If Dir(dlFile) <> "" Then
    FileCopy dlFile, final_dest
    Kill dlFile
    Download = final_dest
    Exit Function
End If

Handler:
If Err.Number = 53 And Timer = False Then
    Resume Next
 ElseIf Err.Number = 53 And Timer = True Then
    If Now > timeout Then
        MsgBox "There is a problem downloading the file. Please check your internet connection."
        End
    Else
        Resume ReTry
    End If
Else
    MsgBox Err.Number & vbNewLine & Err.Description
    End
End If
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多