I searched for hours but cannot find a solution. Hoping I can get the answer here.
I am doing a FileCopy through VBA and wants the code to automatically Try Again whenever an "unexpected network error has occurred" error happens. Can this be done via VBA?
Note that I have access to the network folder, the error is just common due to VPN connectivity issues.
file_ext = "*.xlsx"
FSO.CopyFile Source:=dummy_source_path & "\" & file_ext, _
Destination:=dummy_path
Related
I have used this macro for years on a server based solution, moving over to Sharepoint and I have changed the locations but am hitting a wall. All the code is doing is saving the file as a different name, and then saving it again back as the original name; oddly it only ever fails on the first part, never the second; and often it works all the way through, and then other times when trying to do the first saveas it fails. It also always seems to work if there is no existing filename on the first, its when overwriting that it sometimes works and sometimes doesn't. I would kill the file first, but Kill doesnt seem to work with Sharepoint either so that has stopped me doing that to solve the issue. When it fails I get one of two messages but mostly "Run-time error 1004: Method 'SaveAs' if object'_Workbok;failed". But as I say, I can hit it 10 times in a row and it be fine, then suddenly it wont work anymore and I know I am the only person in the file so its not that someone else has it open or anything like that. I have looked at several chains with this error code and they all seem to suddenly say they worked it out because a DIM was in the wrong place or a filename was wrong etc etc, I cannot seem to find such issue, certainly as it works 50% of the time.
ThisWorkbook.SaveAs Filename:= _
"https://x.sharepoint.com/sites/Company/Shared Documents/F-Drive/FUND ADMINISTRATION/Monthly Reporting/" & (ThisWorkbook.Sheets("Misc Data").Range("J3").Value) & "/Factsheets_Presentations_" & (ThisWorkbook.Sheets("Misc Data").Range("J3").Value) & ".xlsm", _
FileFormat:=52, CreateBackup:=False
ThisWorkbook.SaveAs Filename:= _
"https://x.sharepoint.com/sites/Company/Shared Documents/F-Drive/FUND ADMINISTRATION/Monthly Reporting/Factsheet & Presentation Generator.xlsm", _
FileFormat:=52, CreateBackup:=False
I have a macro enabled excel file (loading large set of data) which is supposed to save a light version and a complete version of itself at one of my desktop location. This is done through referencing a cell in excel where complete path for those files are provided.
Error Image
The file was running fine for a while but all of a sudden started throwing 'Error 1004: Document not saved' on running the macro and not able to save the document. When I try to debug it, it stops at the 'ActiveWorkbook.Save' command.
ActiveWorkbook.Save
ChDir _
SharedDriveLocation
ActiveWorkbook.SaveAs Filename:= _
SharedDriveLocation & "Service Level Report.xlsm" _
, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
I have already enabled all the trusted macro settings for the file. Nothing related to naming convention of file (already checked). Can someone please help understand if this could be excel version issue or something else? This file originated from someone else but I changed myself as the author of it. Assuming that has nothing to do with it.
Today I have had the same error. I found that in my case the file name wasn't correct. I put in full path name folder that doesn't exist.
Probably in your case is the same - incorrect Fullpath.
ActiveWorkbook.SaveAs Filename:= _
SharedDriveLocation & "Service Level Report.xlsm"
Try to change
ActiveWorkbook.SaveAs Filename:= _
SharedDriveLocation & "/" & "Service Level Report.xlsm"
I use VBA to create copies (Excel and PDF) of an Excel document.
I save in the OneDrive folder (C:\Users\XXX\OneDrive) so I can access the document from every device later. I use this folder so I can save while I am connected to internet or not.
When I am connected to internet and OneDrive everything works.
When I am not connected to internet I get
error 1004 method saveas of object _workbook failed
My macro stops but when I go to the folder the files are there.
I get the error on:
ActiveWorkbook.SaveAs Filename:=Fname, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
(Fname="C:\Users\XXX\OneDrive\YYY\ZZZ.xlsm")
I couldn't find anything related to this function not working on OneDrive while not connected to internet.
If your file is in OneDrive, I would first make sure to deactivate the AutoSave feature. I've personally had some bugs with it in the past, so it might help.
I'm not aware of this particular bug that you are experiencing, but since you are saying that the file is actually saved properly even if an error is triggered, there is a way to ignore the error and check if the file was actually saved. It would look a little like this:
Const RETRY_LIMIT = 2
Dim DoesFileExist As Boolean
dim RetryCounter as Integer
Do While RetryCounter < RETRY_LIMIT And DoesFileExist = False
On Error Resume Next
ActiveWorkbook.SaveAs Filename:=Fname, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
On Error GoTo 0
DoesFileExist = FileExists(Fname)
RetryCounter = RetryCounter + 1
Loop
If DoesFileExist = False Then
MsgBox "Error: File couldn't be saved."
Exit Sub
End If
Note that I've added a retry limit of 2 to make sure that we don't go into an infinite loop.
Also make sure to add the following function in the module you are working on to make sure the code above works.
Function FileExists(ByVal FullFileName As String) As Boolean
If Dir(FullFileName) <> vbNullString Then FileExists = True
End Function
Some explanations and caveats
If you have never used error handling statements such as : On Error Resume Next or On Error GoTo 0, I would strongly advise you to have a look at C. Pearson's Error Handling In VBA article.
To summarize, what On Error Resume Next does is simply ignore the error and let the code handle the error. In our case, the DoesFileExist does the job of making sure that the code ran without error and the if statement after the loop will make sure to stop the code if all tentative to save the file failed. This does the job, but you could also check Err.Number to handle different type of error as well.
Regarding On Error GoTo 0, to put it simply, it resets the VBA error handling to its normal state where any error will launch the usual run-time error dialog box
I ended up saving the file on my desktop and then using FileSystemObject to move the file. It works fine except that if I try to open the file right away (using VBA or directly in windows explorer) I get a network error. If I wait about 1 min I don't get that error anymore. So I decided to live with it for now.
here is an example:
Dim FSO as Object
Set FSO = CreateObject("Scripting.Filesystemobject")
FSO.MoveFile("SourceFileName", "TargetFileName")
Thank you very much for your help
I am using some very basic coding to create folders and subfolders automatically. I understand the code isn't elegant however others should be able to maintain it.
The goal is to create a named folder on the desktop with name subfolders. The code works fine on my computer but is throwing an error 6 on other computers. Can you assist?
[Business_Name] is a named field in the spreadsheet
MkDir Environ("Userprofile") & "\Desktop\" & [Business_Name]
MkDir Environ("Userprofile") & "\Desktop\" & [Business_Name] & "\1. Sales"
MkDir Environ("Userprofile") & "\Desktop\" & [Business_Name] & "\1. Sales\1. Accepted Quote and Contract"
Thanks
Your code problem may be strictly connected to the path as it is built...
Sometimes, when (on the other computers) is OneDrive installed, the Desktop folder path includes OneDrive after User profile folder. It will be good to check this aspect... The error message will be elocvent for such a case (Run-Time Error 76, instead of 6).
Try, please
Debug.Print Environ("Userprofile") & "\Desktop\" & [Business_Name]: Stop
and see what it returns in Immediate Window...
I am using the below code to download an excel file from a url. The URL (which I can not share here) is for a ticketing system. I used the search button in the website to get all open and in progress tickets. After that there is a Export drop down button and from the drop down I selected Excel. The url I used is the code is the url of this Export to Excel button.
The code is exporting the excel file to given path, but there is only the page header and footer data to show the time of download and two lines saying "Displaying 0 issues at 07/Jul/14 3:14 PM." and "No Issues Found". in the file.
If I use the url directly on any website, it opens the file with data. If I use Workbooks.open "myUrl" same blank file is opening.
Can anyone please check what is wrong?
Sub downloadFile()
Test = Dir("C:\Users\" & Environ("username") & "\" & "SearchResult.xls")
If Not Test = "" Then
Kill ("C:\Users\" & Environ("username") & "\" & "SearchResult.xls")
End If
mylink = ""
myresult = 0
mylink = "http://mylink.com/issueviews:SearchResult-excel-all-fields/temp/SearchResult.xls?jqlQuery=status+in+%28Open%2C+%22In+Progress%22%29&tempMax=1000"
myresult = URLDownloadToFile(0, mylink, "C:\Users\" & Environ("username") & "\" & "SearchResult.xls", 0, 0)
If myresult <> 0 Then
MsgBox "Error downloading " & mylink & Chr(10) & Error(myresult)
Else
MsgBox "File has been downloaded"
End If
End Sub
There is not enough information available from the data you have provided.
Basically there is some difference between what happens (on the HTTP protocol query/response level) when you use IE manually and when you run the code.
Typical things are redirecting status codes (see http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection) and missing authentication headers (see http://en.wikipedia.org/wiki/HTTP_header)
If you can not install tools (e.g. cURL) it will be difficult to guess as I don't know what kind of tools you have available.
There are some network diagnostics tools built right into web browsers (in Internet Explorer press F12, in Google Chrome press Ctrl+Shift+I)
(EDIT after comments) You can find what is the exact sequence of http requestes/responses exchanged between the web browser and the web server using the network tab in the browser's built-in developer tools.
Usage instructions for Google Chrome are at https://developer.chrome.com/devtools/docs/network#network-resource-details
Usage instructions for Internet Explorer are at http://msdn.microsoft.com/en-us/library/ie/dn255004(v=vs.85).aspx
Once you'll know what is the right sequence of messages try to reproduce it using cURL.
Once you succeed you can try to reproduce the sequence using Excel VBA functions with similar meaning or you can just call cURL command line tool from Excel as described here: Execute a command in command prompt using excel VBA (or in some of its linked or related questions)