Using Workbooks.Open when file has 'unreadable content' / VBS - excel

Below a VBS code converts .xls into .csv.
Set oBook = oExcel.Workbooks.Open(sfile)
oBook.SaveAs fulldest, 6
oBook.Close False
If works fine for most files but one of them has the usual Excel found unreadable content in filename.xls'. do you want to recover the contents of this workbook ? If you trust the source of this workbook, click Yes..
When the .Workbooks.Open method executes for this file, I get Unable to get the Open property of the Workbooks.
I tried oExcel.Workbooks.Open(sfile, xlRepairFile) without success.

Related

Cannot update excel file using VBScript on SharePoint

I have the following simple VBScript for updating an Excel workbook.
Dim objExcel
Set objExcel = CreateObject("Excel.application")
objExcel.DisplayAlerts = False
objExcel.Visible = False
objExcel.Workbooks.Open "path\myworkbook.xlsx"
objExcel.ActiveWorkbook.RefreshAll
objExcel.ActiveWorkbook.Save
objExcel.Quit
Set objExcel = Nothing
When I use path to local excel file like C:\Users\sbrbot\Documents\myworkbook.xlsx the script works and updates my excel workbook fine. But when I use the remote path like \\sharepoint\library\myworkbook.xlsx then script executes without errors (so path is correct) but the workbook is not really updated! (SharePoint version 2013). Even if I map SharePoint's remote server path \\sharepoint\library to logical M:\ drive, and my path is M:\myworkbook.xlsx the same is hapening, it does not update workbook. Seems like SharePoint refuses to update file. Path is correct, that's not the problem.
I have to mention one strange bahaviour with SharePoint. When I manually open Excel file from SharePoint remote directory, make some changes in it, and save the file - in Excel's titlebar it is reported as Saved. Immediately after this when I go to close this Excel file, Excel asks me to save it again although I did not change anything inside the file.

Open excel file with url in vba code error

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

Converting xls to csv but unable to close excel using vbs

I am using a VBScript to convert a xls file to csv. The process seems to be working how I need it except after the file gets converted my script won't close Excel. Both files remain open in excel preventing them from being updated or opened by other apps.
Here is the script I am using:
Set ExcelObj = CreateObject("Excel.Application")
ExcelObj.DisplayAlerts = True
ExcelObj.Visible = false
ExcelObj.Workbooks.Open "P:\Pro2010\folder\Ovations\ovations.xls"
ExcelObj.Workbooks(1).SaveAs "P:\Pro2010\folder\Ovations\edited\company-JULY-TEST.csv", 6
OverwriteExisting
oExcel.Quit
When I open the file in Excel after the script has run it says "The file is in use by 'Another user'. Open a read Only?'
change
oExcel.Quit
to this
ExcelObj.Quit

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

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