如何使用vba从Google Drive下载excel文件?

如何使用vba从Google Drive下载excel文件?,vba,excel,Vba,Excel,我正在尝试使用vba代码从Google drive下载excel文件。该文件通过以下路径下载:C:/MyDownloads/seriall.xlsx。但是一些奇怪的文本被添加到了下载的excel文件的第一页上。我还收到一条弹出消息,其中显示您试图打开的文件的格式与指定的格式不同。所以我点击yes来浏览这个弹出窗口,然后我得到一个css文件丢失错误弹出窗口。为什么会发生这种情况,为什么这些错误会出现在我下载的excel文件中。我的数据也显示在excel添加到其上的奇怪文本的底部 Dim FileN

我正在尝试使用vba代码从Google drive下载excel文件。该文件通过以下路径下载:C:/MyDownloads/seriall.xlsx。但是一些奇怪的文本被添加到了下载的excel文件的第一页上。我还收到一条弹出消息,其中显示您试图打开的文件的格式与指定的格式不同。所以我点击yes来浏览这个弹出窗口,然后我得到一个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 "

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

FileCopy GetSpecialFoldervbDirGoogleDrive&seriall.xlsx,C:/MyDownloads/seriall.xlsx

您还可以使用:

文件复制GetSpecialFoldervbDirGoogleDrive和seriall.xlsx,GetSpecialFoldervbDirDownload和seriall.xlsx*

您甚至可以在谷歌硬盘和Dropbox之间移动文件:

文件复制GetSpecialFoldervbDirGoogleDrive和seriall.xlsx,GetSpecialFoldervbDirPropBox和seriall.xlsx*

尝试下面的代码

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
寻找作者

Option Compare Database
Public stTXT As String
'This line is to enable the 'Sleep' function which I use later.
Private Declare Sub Sleep Lib "kernel32" (ByVal lngMilliSeconds As Long)

'To enable Excel.Application, Excel.Workbook and Excel.Worksheet - you need to enable
'the Excel objects in your Access file: in the VBA application go to 'Tools' menu > 
References.
'Find the Microsoft Excel 12.0 Object Library, and activate the checkbox.
'Now you have the full Excel library at your service.
'Here I used 'Object' - which is enough to make it work without the excel library.
    Dim appXL As Object 'Excel.Application
    Dim wbk As Object 'Excel.Workbook
    Dim wst As Object 'Excel.Worksheet
    Dim Timer As Integer

    Set appXL = CreateObject("Excel.Application")
'    appXL.Visible = True 'If you want to see the excel sheet - enable this row (good 
for debugging)
    Set wbk = appXL.Workbooks.Add
    Set wst = wbk.Worksheets(1)

    With wst
'In the following row, after the word 'key=' until the '&gid' - put the code-number 
of the google-doc spreadsheet, which you extract from the link you get for the 
spreadsheet google-doc (looks like: 'KeXnteS6n6...')

        .QueryTables.Add Connection:= _
            "URL;https://spreadsheets.google.com/tq?tqx=out:html&tq=&key=???&gid=1" _
            , Destination:=.Range("$A$1")
        .Name = "Worksheet1"
'The following fields are available if enabling Excel library (See above)
'        .FieldNames = True
'        .RowNumbers = False
'        .FillAdjacentFormulas = False
'        .PreserveFormatting = True
'        .RefreshOnFileOpen = False
'        .BackgroundQuery = True
'        .RefreshStyle = xlInsertDeleteCells
'        .SavePassword = False
'        .SaveData = True
'        .AdjustColumnWidth = True
'        .RefreshPeriod = 0
'        .WebSelectionType = xlEntirePage
'        .WebFormatting = xlWebFormattingNone
'        .WebPreFormattedTextToColumns = True
'        .WebConsecutiveDelimitersAsOne = True
'        .WebSingleBlockTextImport = False
'        .WebDisableDateRecognition = False
'        .WebDisableRedirections = False
'        .Refresh BackgroundQuery:=False

        .QueryTables(1).Refresh
    End With

    'Wait for google-doc data to be downloaded.
    Timer = 0
    Do While Left(wst.Cells(1, 1), 12) = "ExternalData" And Timer < 40
        Sleep 250   ' Wait 0.25 sec before re-checking data
        Timer = Timer + 1
    Loop

    MsgBox "The value of cell AG2 is: " & wst.Cells(2, 34)

'Here you can work with the data...

'    wbk.Close SaveChanges:=False 'Don't save excel sheet
    wbk.Close SaveChanges:=True, FileName:="C:\Users\(User Name)\Desktop\GDocs" 'Save 
excel sheet in 'Documents' folder
    appXL.Quit

 'On Error GoTo ErrorHandler

  Exit Sub

请参阅刚刚在公共站点上测试的内容。您的版本使用文本文件编写器,而.xls文件不是。@Zesane您所指的人都是志愿者,在社区之外有生命和责任。仅仅发布一个问题并不能确保得到答复,或者答复可能需要几天时间。许多问题需要特殊的知识,而只有少数人可能拥有这些知识。获得答案可能需要时间,但发布第一条评论等内容可能会阻碍那些本来可以提供帮助的人。耐心点,年轻的学徒。看这个元讨论:嗨,冻糕。你建议的链接在从谷歌硬盘下载的文件上方也添加了特殊字符。你说寻找作者是什么意思?找不到原创文章来赞扬作者。不是我的代码。