Open excel file with url in vba code error - excel

Using a link "https://~" that downloads excel file from web, the following code was tried.
Workbooks.Open("https://~")
Just copy and pasting the url link in any web browser works fine - downloads excel file with all the contents intact.
However, with the above vba link, an excel file opens in [read only] format and completely blank even without the vertical and horizontal lines in excel.
How can I solve this?

It might be because your file is not stored in your pc (only as a temp file).
You need to SaveAs the file after you open it from web.
Sub SaveFile()
Dim wb As Workbook
Set wb = Workbooks.Open("https://yoururl.com/yourfile.xls")
wb.SaveAs ("C:\yourPath\SavedFile.xls", FileFormat:=56)
End Sub

Related

Open Excel file in SharePoint through VBA

I'm trying to open an excel file in SharePoint through a vba code in another local excel file. However, it gives me a Dialog Box and lets me save the file instead of directly opening in Excel.
Below is the code I used. Would be very grateful for your help. Thanks.
Dim wb AS Workbook
FilePath = "https://company.sharepoint.com/sites/Folder1/Folder2/Filename.xlsm"
Set wb = Workbooks.Open(Filename:=FilePath, UpdateLinks:=0)
I had this issue also. First, you have to fix something in the excel options. So, go to File > Options > Advanced > then scroll down until you see Link Handling > then select the box that says "Open Supported hyperlinks to Office in Office Desktop Apps". Next, I used the coding below to get the link to work. you just need to reply the part of the coding with you sites url sharepoint file. the only issue I came up with is that you have to run the coding below, wait for the file to completely load, and then you can run macros on this file.
Sub OPEN_YRLY()
'
' OPEN_YRLY Macro
'
'
Range("C1").Select
ActiveWorkbook.FollowHyperlink Address:="https://aramark365-my.sharepoint.com/:x:/r/personal/......", NewWindow:=False, AddHistory:=True
End Sub

Activating Macro view button sends excel in non-responding mode

In Excel 2013: macro was working fine until today. Now, I can open the "Personal.xlsb" file, but I cannot use the Macro view button anymore, all I get is Excel in unresponding mode; and then, only way is to stop Excel entirely. When I go in design mode, I cannot access the macro registry at all (trying to do so results in the same unresponding state). I rebooted the PC entirely, did not help.
Pb seems specific to one specific "Personal.xlsb" file (I replaced the incriminated file with another "Personal" file in the XSTART folder and Excel macros worked just fine with a different .xlsb file). So I am suspecting a file corruption pb. If that is the case, is it possible to recover the original macros, or at least crack the macro file open and get a copy of the original coding?
You can try to get back your code if you manage to open the workbook from a macro in another workbook. Give this a shot:
create a folder where you will get the recovered code modules. Let's say "C:\myRecoveredCode". Put in a copy of your corrupt file "Personal.xlsb"
In Excel Options, Trust Center Settings, Check Trust Access to the VBA project object module
create a fresh workbook, copy/paste and run the following macro:
Sub TryRecovery()
myFolder = "C:\myRecoveredCode\"
Set wb = CreateObject(myFolder & "Personal.xlsb")
For Each comp In wb.VBProject.VBComponents
comp.Export myFolder & comp.Name
Next
wb.Close False
End Sub
If all goes well, you will have files a set of files that you can edit, or import to another workbook.

Applescript: Open Excel File and Save in a Different Format (same name and path)

I have a folder of .xls files of which some are genuine excel files and some are excel xml files (but still with the .xls extension). To work with the files in a separate program, I'm trying to convert them all to a consistent format (Excel 98-2004) and want to use an applescript to do this while retaining the original file names and directory structure.
As of now, I have a working loop and list of files, and am accomplishing everything but the save function:
set file_Name to "/Users/me/path/to/data.xls"
tell application "Microsoft Excel"
activate
open file_Name
save workbook as workbook (active workbook) filename file_Name file format (Excel98to2004 file format) with overwrite
close workbooks
quit
end tell
When I run this code, I get the following replies:
tell application "Microsoft Excel"
activate
open "/Users/drewmcdonald/Desktop/Madhupur_10February2014.xls"
get active workbook
--> active workbook
save workbook as workbook (active workbook) filename "/Users/drewmcdonald/Desktop/test.xlsx"
--> missing value
close every workbook
quit
end tell
The spreadsheets are successfully opening and closing, but the file format is not changing and I don't know what to make of the "missing value" error. Any idea how to get this to work? Thanks!
EDIT: Just noticed that the file format parameter is missing from the reply text, so I'm guessing the problem is in there.
Got it.
I needed to set a workbook name variable to get full name of active workbook before trying to save it. This is because the workbook name that Excel assigns as it opens sheets is somewhat inconsistent. Final code looks like this:
set file_Name to "/Users/me/path/to/data.xls"
tell application "Microsoft Excel"
activate
open file_Name
set wkbk_name to get full name of active workbook
save workbook as workbook wkbk_name filename file_Name file format Excel98to2004 file format with overwrite
close workbooks
quit
end tell

Referencing workbook/sheet from another open workbook

I have an online report that when downloaded automatically opens as a *.csv file. I then open another Excel workbook containing various macros and want to copy the entire downloaded sheet (sheet1) over to the my main workbook either as a new sheet or into an existing blank sheet named "DATA".
My problem is referencing the open downloaded sheet as each time it is downloaded the name is different. My preference is to not save the downloaded workbook and then copy as I just delete the file when I am done.
Any help with referencing the downloaded file and activating it would be appreciated.
I've been thinking about Application.RecentFiles today and wonder if it would work here. The assumption is that the csv is the most recently opened file, i.e., it's at the top of the Home>Recent list:
Sub ActivateMostRecentOpened()
Dim WbName As String
With Application
WbName = Workbooks(.RecentFiles(1).Name, InStrRev(.RecentFiles(1).Name, .PathSeparator) + 1).Activate
End With
End Sub

How to open a csv file from Excel with a particular line highlighted?

I have a large CSV file and I want to programmatically open it in excel with a particular line highlighted (I know the line number). What is the easiest way to this?
I think my options are:
Auto convert the csv file to an xlsx file. How can I do this from a script?
Give Excel some arguments when it opens up. No idea what command line arguments Microsoft products take.
Somehow interact with Excel after it opens up the CSV file and tell it to highlight a particular. Again not sure how.
I prefer Java/Python/Shell or anything that would work across Mac/Windows assuming the system has Excel installed. So, my best bet is probably #1 which brings me back to the question how can I convert a CSV file to a xlsx file.
You could run a basic vbs which avoids the need to have Excel already open, and conversion isn't necessary.
Paste the code below into a text editor NotePad
Change the path to your CSV file to suit (ie "c:\temp\test2.csv")
Save the file as something like MyCSV.vbs say to your Desktop
Click on the final vbs to open the CSV file to Row X (8 in the sample below)
Dim objExcel
Dim WB
Set objExcel = CreateObject("excel.application")
Set WB = objExcel.Workbooks.Open("c:\temp\test2.csv")
With objExcel
.Goto WB.Sheets(1).Rows(8)
.Visible = True
End With
this works simple save it in an empty workbook.
Private Sub Workbook_Open()
Workbooks.Open ("test.csv")
Range("8:8").Select
End Sub
also if you save that in your normal.dot (the default template document when opening excel) it will run on any document it opens. so what you could do is:
save this to your normal.dot
Private Sub Workbook_Open()
Range("8:8").Select
End Sub
then change the default application for opening .csv files to excel. then whenever you double-click on .csv file it will be opened with excel and excel will run the Workbook_Open() sub and viola!

Resources