Marisoleil
MIS
Hello,
I have a code that works 'some of the time'. Its purpose is to hyperlink to a set of URL's (listed in a column in Excel spreadsheet), which are addresses that automatically begin downloads when navigated to.
Normally, once navigated to on the web, these sites present the surfer with a prompt to Open or Save the download, which arrives as a csv Excel file (but not in read-only form).
With the code I've written below, the prompt for open/save doesn't come up, but the file automatically comes as a Read-Only Excel File, and with a different file name than when surfing to the site.
The problem is: Sometimes this code works, all the way through, and the folder and file(s) are saved. Other times, it stops at the Read-Only file, doesn't re-name it (by SaveAs Method), and doesn't create a new folder or file(since it needs the non read-only file to do so).
p.s. Even if you don't see the possible reason for why the code is not always working, don't hesitate to let me know if you see any 'sloppy' coding. I am always wanting to learn more robust, time-efficient and conflict-free ways to code.
I suspect there's more than one way to do this. Any comments or suggestions welcome.
Thanks
Marie
I have a code that works 'some of the time'. Its purpose is to hyperlink to a set of URL's (listed in a column in Excel spreadsheet), which are addresses that automatically begin downloads when navigated to.
Normally, once navigated to on the web, these sites present the surfer with a prompt to Open or Save the download, which arrives as a csv Excel file (but not in read-only form).
With the code I've written below, the prompt for open/save doesn't come up, but the file automatically comes as a Read-Only Excel File, and with a different file name than when surfing to the site.
The problem is: Sometimes this code works, all the way through, and the folder and file(s) are saved. Other times, it stops at the Read-Only file, doesn't re-name it (by SaveAs Method), and doesn't create a new folder or file(since it needs the non read-only file to do so).
p.s. Even if you don't see the possible reason for why the code is not always working, don't hesitate to let me know if you see any 'sloppy' coding. I am always wanting to learn more robust, time-efficient and conflict-free ways to code.
Code:
Sub TestOpenURL()
' To loop through the cells in column F (stops at a blank cell):
Dim x%
x = 18
'Do While Cells(x, "F").Value <> " " (I haven't activated this yet, I'm still running the code for just one website)
ActiveWorkbook.FollowHyperlink Address:=Cells(x, 6), NewWindow:=True
'Following code saves downloaded file as normal (rather than Read-Only as the download arrives in, (possibly due to having bypassed the open/save prompt window during download))
'This file will be deleted later by this macro, but I suspect it is necessary to first save a non-read-only copy, in order to extract info from cells within this file to help define its new file name to be saved with.
'Since the location to save the file hasn't been specified, it will automatically be saved in current folder. I WOULD LIKE TO SPECIFY A LOCATION, AS IT MAKES DELETING IT EASIER LATER. However, for some reason, the procedure gets buggy if I put a path in here. I've tried many different places, eg. "C:\temp\" & "NewFile.xls". Doesn't seem to work.
If ActiveWorkbook = ReadOnly Then
ActiveWorkbook.SaveAs Filename:="NEWFILE" & ".xls", FileFormat:=xlNormal
End If
Dim StationName, StationID As String
Dim UpdateMonth As Integer
Dim UpdateYear As Integer
StationName = ActiveWorkbook.ActiveSheet.Cells(1,2)
StationID = ActiveWorkbook.ActiveSheet.Cells(6, 2)
UpdateMonth = ActiveWorkbook.ActiveSheet.Cells(18, 3)
UpdateYear = ActiveWorkbook.ActiveSheet.Cells(18, 2)
'Following code create a custom-format timestamp with today's date
Dim MyStr As String
MyStr = Format(Date, "yyyy-mm-dd")
'Following code is to create a folder in .datain with today's date and the station name, but only if folder doesn't already exist (Thanks to someone on this site for the code, by the way! :D )
Dim fso
Dim fol As String
fol = "S:\01 JOBS\job1094-Canadian Wind Index\data.in\" + MyStr + " " + StationName
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(fol) Then
fso.CreateFolder (fol)
End If
ActiveWorkbook.SaveAs Filename:="S:\01 JOBS\job1094-Canadian Wind Index\data.in\" & MyStr & " " & StationName & "\" & UpdateYear & "_" & UpdateMonth & "_" & StationName & "_" & StationID & ".xls", FileFormat:=xlNormal
'
'I'd like to delete the proxy file I created temporarily now. But I'm not sure yet that this is the right command below.
'NewFile.xls.Delete
' x = x + 1
'Loop
End Sub
I suspect there's more than one way to do this. Any comments or suggestions welcome.
Thanks
Marie