如何使用vba从Google云端硬盘下载Excel文件?

时间:2016-07-02 22:31:26

标签: excel vba excel-vba

我正在尝试使用vba代码从Google云端硬盘下载excel文件。该文件将在此路径C:/MyDownloads/seriall.xlsx中下载。但是在第一张表中下载的excel文件中添加了一些奇怪的文本。我还会收到一条弹出消息,其中包含您尝试打开的文件格式与指定格式不同的消息。所以我单击是以通过此弹出窗口然后我得到一个css文件丢失错误弹出窗口。为什么会发生这种情况以及为什么这些错误出现在我下载的excel文件中。我的数据也显示在excel附加的奇怪文本的底部。

Dim FileNum As Long
Dim FileData() As Byte
Dim MyFile As String
Dim WHTTP As Object

On Error Resume Next
    Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
    If Err.Number <> 0 Then
        Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
    End If
On Error GoTo 0

MyFile = "https://docs.google.com/spreadsheets/d/1e6DNpw3y5NrMR9cNLmIZdPYO79WLui7mua5I-5pEyKo/edit?usp=sharing"

WHTTP.Open "GET", MyFile, False
WHTTP.send
FileData = WHTTP.ResponseBody
Set WHTTP = Nothing

If Dir("C:\Downloads", vbDirectory) = Empty Then MkDir "C:\Downloads"

FileNum = FreeFile
Open "C:\Downloads\serial.xls" For Binary As #FileNum
    Put #FileNum, 1, FileData
Close #FileNum

MsgBox "Open the folder [ C:\Downloads ] for the "

3 个答案:

答案 0 :(得分:1)

使用'GetSpecialFolder'UDF,您可以从任何云驱动器下载文件,如下所示:

FileCopy GetSpecialFolder(vbDirGoogleDrive)&amp; “seriall.xlsx”,“C:/MyDownloads/seriall.xlsx”

http://www.EXCELGAARD.dk/Lib/GetSpecialFolder/

您也可以使用:

FileCopy GetSpecialFolder(vbDirGoogleDrive)&amp; “seriall.xlsx”,GetSpecialFolder(vbDirDownloads)&amp; “seriall.xlsx” *

您甚至可以在Google云端硬盘和Dropbox之间移动文件:

FileCopy GetSpecialFolder(vbDirGoogleDrive)&amp; “seriall.xlsx”,GetSpecialFolder(vbDirDropbox)&amp; “seriall.xlsx” *

答案 1 :(得分:0)

寻找作者

// InnerClass.gs

function InnerClass() {
  this.myProperty = 42;
}

InnerClass.prototype.myFunction = function() {
  return 43;
};

答案 2 :(得分:-1)

尝试以下代码,

Sub Basic_Web_Query() Dim chromePath As String Sheets("Sheet2").Select Range("A2").Select Selection.Copy Shell ("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe") SendKeys "^v" SendKeys "~" Application.Wait (Now + TimeValue("00:00:10")) SendKeys "^a" SendKeys "^c" Application.Wait (Now + TimeValue("00:00:10")) Sheets("Sheet1").Select Range("A1").Select ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:= _ False End Sub