Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Download Excel file from Google drive

0

Hi all,

I want to add a custom button to my excel application that will download and open an excel file from my google drive.

I can create the customer button, for this I will use the Custom Editor for Microsoft. Then i want to click that button or another and it will download and open the correct file. I also want to publish this workbook so others can access these templates

Any thoughts

Answer
Discuss

Discussion

Hi again Semaj. 

Hope you get a reply from Willie since I have no spare time until Thursday. If not, I'll try to look then.

In the meantime, is it always the same file to open or does it depend on the user? 
John_Ru (rep: 6722) Oct 27, '24 at 10:39 am
semaj, 
Sorry, but I have no experience with Google Drive so hopefully John can provide a solution for you.
Also, please answer John's question above.
WillieD24 (rep: 687) Oct 29, '24 at 5:29 pm
There will be a number of files on the drive, and a corresponding button in the excel sheet. I will put the buttons in a custom ribbon
semaj (rep: 14) Nov 19, '24 at 6:38 am
Hi guys, spent a bit of time on this last week and have come up with something that works. Still test the security side, but looks very promising
semaj (rep: 14) Nov 22, '24 at 4:45 pm
That's good to know, well done. Semaj. Sorry we couldn't help this yime. 
John_Ru (rep: 6722) Nov 23, '24 at 2:37 am
All good, still working on it. I think what i have done will work well. 
semaj (rep: 14) Dec 23, '24 at 7:09 am
Thats good to hear. Sorry I couldn't help this time. 
John_Ru (rep: 6722) Dec 23, '24 at 10:58 am
thanks John
semaj (rep: 14) Jan 7, '25 at 11:06 pm
Add to Discussion

Answers

0
Selected Answer

Try this

Option Explicit
Declare PtrSafe 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

Sub DownloadAndOpenTempFile()
    Dim fileURL As String
    Dim tempPath As String
    Dim savePath As String

    ' Get the temporary folder path
    tempPath = Environ("TEMP")
    savePath = tempPath & "\YourFile.xlsx" ' Define the file name in temp
     'Tip: right click on the file in Google Drive, choose share then copy link
    fileURL = "https://drive.google.com/uc?export=download&id=FILE_ID" ' Replace FILE_ID with actual file ID

    ' Download the file to temp
    URLDownloadToFile 0, fileURL, savePath, 0, 0

    ' Open the file
    Workbooks.Open savePath
End Sub

When you download a file using a direct URL from Google Drive, the saved file name is determined by what you specify in your VBA code, not automatically by the original name in Google Drive.

However, if you want to maintain the original file name, you'd need to retrieve it from Google Drive before downloading. This would usually require using the Google Drive API with authentication, since Google Drive doesn't automatically append file names to download links.

For simplicity, you could manually specify the file name in the savePath variable  as in the provided code. If you're looking for an automated approach to get the exact file name dynamically, you'd need to authenticate and retrieve file metadata via the API.

Discuss


Answer the Question

You must create an account to use the forum. Create an Account or Login