[VBA] Runtime Error 3004 - Write to file failed

Seba Robles

Board Regular
Joined
May 16, 2018
Messages
73
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello, so I have a code to download a google sheet as excel to my computer.

I'm getting the following error in my code and can't figure out what's wrong: (line highlighted in red)

Rich (BB code):
Sub ImportGoogleSheet()

Dim URL, Location, FileName As String
Dim objWeb, objWrite As Object

URL = "Google Sheets URL Location"
Location = ThisWorkbook.Path & "\C:\Users\Username\Desktop\Exports"
FileName = "\GoogleSheet.csv"

'Connection to website
Set objWeb = CreateObject("MSXML2.XMLHTTP.3.0")

'Connecting to the website
objWeb.Open "Get", URL, False
objWeb.Send (URL)

If objWeb.Status = 200 Then

'Writer
Set objWrite = CreateObject("ADODB.Stream")

objWrite.Open
objWrite.Type = 1
objWrite.Write objWeb.ResponseBody
objWrite.Position = 0
objWrite.SaveToFile Location & FileName, 2
objWrite.Close
End If

End Sub

Thanks in advance!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Are you sure this returns a valid path?
Code:
Location = ThisWorkbook.Path & "\C:\Users\Username\Desktop\Exports"
 
Upvote 0
Yes, I copied it directly from windows explorer..

Code:
Location = ThisWorkbook.Path & "C:\Users\Username\OneDrive\Desktop\IISE\GS Exports"

That's the full path
 
Last edited:
Upvote 0
Let's say your workbook is saved in "C:\Test", if that was the case then this would be the value of Location.

C:\TestC:\Users\Username\OneDrive\Desktop\IISE\GS Exports"

To me that doesn't look like valid path.:)
 
Upvote 0
Ok I'm not really following

The path where the workbook where I'll run the macro is saved here: C:\Users\username\OneDrive\Desktop\IISE

The path where I want to save the google sheet export is: C:\Users\username\OneDrive\Desktop\IISE\GS Exports

So the location path should be "C:\Users\username\OneDrive\Desktop\IISEC:\Users\srobl\OneDrive\Desktop\IISE\GS Exports" ?
 
Upvote 0
Try this.
Code:
Location = ThisWorkbook.Path & Application.PathSeparator & "GS Exports\"
 
Last edited:
Upvote 0
Do you have a folder named 'GS Exports' in the same folder as the workbook you are running the code from?
 
Upvote 0
I just hard coded the location to the desktop and saved it there and it runs.. I will have to change it later to save it on the desktop regardless of the user running the workbook.

Thanks!
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top