I have a set of excel files that open in protected view. I would like to open these excel files, remove the protected view, then save them as tab delimited text files. So far, I have been able to do everything but when I run the script, a prompt comes up asking if the user would like to save the excel file. I want to get rid of this prompt to make the script fully automatic. Here is the current code:
format = -4158
Set objFSO = CreateObject("Scripting.FileSystemObject")
src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(1))
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
oExcel.ProtectedViewWindows.Open(src_file)
oExcel.ActiveProtectedViewWindow.Edit
oExcel.Quit
Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)
oBook.Worksheets(5).Activate
oBook.SaveAs dest_file, format
oBook.Close False
oExcel.Quit
I have found solutions to removing the prompt online, but because I have to edit the protected view window, I could not find a way to use the solutions. How can I make it so that this VB script removes the protected view, and saves the excel file as a tab delimited text file? It should do this without showing any windows or prompts, i.e., it should be automatic.
To simply have Excel ignore message boxes in general it is like this:
oExcel.DisplayAlerts = False
'your code here to close workbook
oExcel.DisplayAlerts = True
Related
My excel file is connected into the analysis services database. I want to auto refresh the excel and save it into my computer everyday even if I do not open the excel.
I am wondering how to do it.
The reason that I want to do it: Since it is connect to the database and it is only show the recent 5 weeks data and I want to save all the data every day so I can have historical data.
Please help if you know how to do thsi
What I want is excel file is auto saved into my computer,Desktop for example everyday even if I did not open it.
You can write a VBA macro (reuse what you have already written) and call it from Excel in a VBS script that it started by Windows Scheduler when you want.
My BAT file
::********************************************************************
::* Generate-Excel-File.bat
::********************************************************************
#echo ON
SETLOCAL ENABLEDELAYEDEXPANSION
C:\windows\syswow64\cscript.exe LoadExcel.vbs
My VBS file
'*****************************************************************************
'* LoadExcel.vbs
'*****************************************************************************
' Create a WshShell to get the current directory
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
' Create an Excel instance
Dim oExcel
Dim oWorkBook
Set oExcel = CreateObject("Excel.Application")
' Disable Excel UI elements
oExcel.DisplayAlerts = False
oExcel.AskToUpdateLinks = False
oExcel.AlertBeforeOverwriting = False
oExcel.FeatureInstall = msoFeatureInstallNone
' Tell Excel what the current working directory is
' (otherwise it can't find the files)
Dim strSaveDefaultPath
Dim strPath
strSaveDefaultPath = oExcel.DefaultFilePath
strPath = WshShell.CurrentDirectory
oExcel.DefaultFilePath = strPath
' Open the Workbook specified on the command-line
Set oWorkBook = oExcel.Workbooks.Open(strPath & "\US.TRACKING-FILE.NEW.xlsm")
' Build the macro name with the full path to the workbook
on error resume next
' Run the calculation macro
oExcel.Run "LoadCSV"
if err.number <> 0 Then
' Error occurred - just close it down.
End If
err.clear
on error goto 0
'oWorkBook.Save
'oExcel.DefaultFilePath = strSaveDefaultPath
' Clean up and shut down
Set oWorkBook = Nothing
' Don’t Quit() Excel if there are other Excel instances
' running, Quit() will
' shut those down also
if oExcel.Workbooks.Count = 0 Then
oExcel.Quit
End If
Set oExcel = Nothing
Set WshShell = Nothing
I hope that can help you to solve your problem.
In this example, I load a CSV file in Excel but if you want, you can run a SQL command in VBA and fill what you want using pure Excel macros.
I have an Excel file named "ABCD.xlsm" in three different folders.
When I open these files using VBScript one after the other I cannot run the macros and the addin.
Set fso = CreateObject("Scripting.FileSystemObject")
Set theFile = fso.OpenTextFile("D:\Temp\Excellocation.txt", 1, False)
Do While theFile.AtEndOfStream <> True
retstring = retstring & theFile.ReadLine
Loop
theFile.Close
lx_loc = retstring
fso.DeleteFile "D:\Temp\Excellocation.txt"
set objExcel = Createobject("Excel.Application")
objExcel.visible = True
objExcel.workbooks.open(lx_loc)
msgBox "RUN macro CallVSTOMethod and press ok",1, true
In the code the text file has the location of the Excel file.
If there is only one use of this code, then I have no issues. When I call this function more than once, I can't run the addin or the macro.
It is because when you open your file with a connection, it is locking this file, so you cannot open it for that reason. What you can do, is copy past a duplicate and open the copy. It will open right away! ;)
I have VBscript code which opens XLS file and export it to CSV, see below:
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open("test.xls")
oBook.SaveAs "test.csv", 6
oBook.Close False
oExcel.Quit
It works fine but I need to do the same - convert XLS to CSV with already opened file "test.xls" on my computer which is different (edited by me) versus saved version. Is that possible?
Yes, you just need to connect the existing instance of Excel and set workbook object variable equal to the already open workbook instead:
Set oExcel = GetObject(, "Excel.Application")
Set oBook = oExcel.Workbooks("test.xls")
So I am having a problem uploading files from RStudio to Excel for MATLAB processing.
I had this problem before with formulas not being populated, so I made a script to open, save, and close the Excel file which then worked fine for populating the formulas and loading the numerical values back into RStudio. However I can not figure out how to open multiple .csv files that have changing names depending on our sample ID.
Heres my script that I tried to have open up multiple files:
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\PCRdata\*.csv")
## Also tried Set objWorkbook = objExcel.Workbooks.Open("C:\PCRdata\"& "*.csv")
objExcel.Application.Visible = True
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
WScript.Echo "Your Excel Spreadsheet was Updated, Open these files in Matlab"
WScript.Quit
However the script doesnt like the * to call upon all files, is there another way I can do this? or a better way to have RStudios come out with data that is usable to matlab.
MATLAB Error:
Error using netest/testingBrowseButton_Callback (line 63)
Cannot concatenate the table variables 'AKAP8L' and 'ARAF', because their types are double and cell.
Error while evaluating UIControl Callback
Well did some digging on the website and found an alternative post that used a script to change change xls to xslx so I just made it so it went from csv to csv. Hope this helps anyone else that had this problem.
Set app = CreateObject("Excel.Application")
app.Visible = False
app.DisplayAlerts = False
Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("C:\PCRdata").Files
If LCase(fso.GetExtensionName(f)) = "csv" Then
Set wb = app.Workbooks.Open(f.Path)
wb.Save
wb.Close True
End if
Next
app.Quit
Set f = Nothing
Set app = Nothing
Set fso = Nothing
wScript.Quit
there is a need to save the workbooks in a spreadsheet into seperate txt files on my location.
but the code which i am using in the button click is ertreiving the data to the desired location and the files include special characters which iam not interested in.
can any one help me in getting the txt file as i get the details in the print preview screen.
code used:
Private Sub CommandButton1_Click()
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Dim strOutputFileName
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Documents and Settings\bera02a\Desktop\Arun_TAT_Testing_orig_14022013.xls")
For Each xlSheet In xlBook.Worksheets
strOutputFileName = "C:\Documents and Settings\bera02a\Desktop\" & xlSheet.Name & ".txt"
xlSheet.SaveAs strOutputFileName
Next
xlApp.Quit
End Sub
I think the cause of your incorrect output is NOT specifying the desired saving format. Try to replace your saving line of code with this:
xlSheet.SaveAs Filename:=strOutputFileName, FileFormat:=xlUnicodeText
This will define out put as Unicode text file - I suppose that's what you want.