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

问题描述 投票:-2回答:2

我正在尝试使用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 "
excel vba excel-vba
2个回答
1
投票

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

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

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

您还可以使用:

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

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

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


-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

© www.soinside.com 2019 - 2024. All rights reserved.