I have tried to use Task Scheduler to run my VBScript which will open Excel and run its Macros. However, the Task Scheduler status only shows Running. When I opened the Excel file manually it shows:
Microsoft Excel cannot access the file «. «. There are several possible reasons:
The file name or path does not exist.
The file is being used by another program.
The workbook you are trying to save has the same name as a currently open workbook.
I have tried:
Create directory "C:\Windows\SysWOW64\config\systemprofile\Desktop" (for 64 bit Windows) or "C:\Windows\System32\config\systemprofile\Desktop" (for 32 bit Windows).
Changed the DCOM config for the Microsoft Excel application
Enabled all macros in Excel and set the Trust Center.
But it still doesn't work.
This is my VBScript
'Create Excel App Instance & Open Xlsm File
Set objExcelApp = CreateObject("Excel.Application")
objExcelApp.Visible = True
objExcelApp.DisplayAlerts = False
'Define Macro File & Path
sFilePathXlsm = "C:\Users\roeyj\OneDrive\Desktop\Intern\Monthly PM\Service Contracts as of 2022 (macro).xlsm"
Set iWb = objExcelApp.Workbooks.Open(sFilePathXlsm)
'1. Run 1st Macro in another Excel
objExcelApp.Run "'" & iWb.Name & "'!Sheet3.contract_vba"
'Save & Close file
iWb.Close
iwb.Save
objExcelApp.DisplayAlerts = True
objExcelApp.Quit
objExcelApp.Run "'" & iWb.Name & "'!Sheet3.contract_vba"
This is the last few lines of my Macros. Not sure what the problem is.
Sheets("Monthly PM").Delete
Workbook.Save
Application.Quit
End Function
Related
I have a VB Script or Excel Macro which I created and it works fine when I run the code from within Excel as an excel macro. But when I want to run it from Windows Explorer (i.e. save the text as a VBS script), I keep getting the Expected Statement error.
The code below essentially deletes two tabs from a template that I want to use, and then copies two tabs from another workbook to the template, and then saves that file. The code below was created using the record button in Excel. I am a total newbie to VBA, and so I do not know why the error is occurring. I do not also want to run the file from within Excel, because I want to be able to change the text what is in the macro. Thank you for your help.
Sub mapping_specs_comp_ss()
Set OrigMapTemplate = Workbooks.Open("D:\Kriss\Mapping Specs\Mapping Specs Template Version 3 - Copy Final.xlsx")
Application.DisplayAlerts = False
OrigMapTemplate.Sheets("QSTEST Mapping").Delete
OrigMapTemplate.Sheets("QSORRES Mapping").Delete
Set closedBook = Workbooks.Open("D:\Docs\Mapping Specifications\Automation\Mapping_Create - QSTEST QSORRES.xlsx")
closedBook.Sheets("QSTEST Mapping").Copy After:=OrigMapTemplate.Sheets("Visit Mapping")
closedBook.Sheets("QSORRES Mapping").Copy After:=OrigMapTemplate.Sheets("QSTEST Mapping")
closedBook.Close SaveChanges:=False
ActiveWorkbook.SaveAs Filename:= _
"D:\Docs\Mapping Specifications\Automation\Automated_Mapping_Specifications.xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End Sub
So I work for a company that's a bit slow with the times and manually requires workers to input sales data into a spreadsheet at end of day. I was tired of that so I created a separate VBA Excel program to automate it.
It has been working fine for the past two months but for seemingly no reason it seems to of corrupted the spreadsheet it was altering when I ran my program recently. Attempting to open the spreadsheet and enabling macros on the work PC causes Excel (2013) to crash. Opening other worksheets and enabling macros on them works fine though.
I tried emailing it to myself to figure out why but it opens correctly and with no issues with my version of Excel (Office 365)
I know that the corruption must of occurred either before my program ran or just after it started running because it performs a backup of the spreadsheet before altering/inputting any data and the backup is also corrupted on the work pc.
To try and simplify a hundred lines of code and 5 helper functions or so; it opens the files needed with Workbooks.Open (ThisWorkbook.path & "\" & file) where file is just the name of the workbook to be altered; it then performs a backup and deletes backups older than 7 days.
Here's the generic backup code:
Private Sub deleteBackup(folderName As String)
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
For Each fcount In FSO.GetFolder(ThisWorkbook.path & folderName).Files
If DateDiff("d", fcount.DateLastModified, Now()) > 7 Then
Kill fcount
End If
Next fcount
End Sub
Private Sub backup(fileName As String)
Dim Monthly As Workbook
Dim dateStamp As String
dateStamp = Format(returnDailyDate(), "dd.mm.yyyy")
' Check if folders exist, if they don't, create them
If Dir(ThisWorkbook.path & BackupPath, vbDirectory) = vbNullString Then
MkDir (ThisWorkbook.path & BackupPath)
End If
Set Monthly = Workbooks(MonthlyPath)
Monthly.Save
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
FileSystemObject.CopyFile ThisWorkbook.path & "\" & MonthlyPath, ThisWorkbook.path & BackupPath & fileName & dateStamp & FileExtension
End Sub
Is there a logical reason for why this would happen or why it wouldn't work in 2013 but does in 365?
idk if it was a random inevitable hiccup involving running out of memory or something (let's just say the works dev department isn't the greatest) or whether my program caused the corruption and could potentially do it again in the future.
Thanks for your time.
Update:
So sending the workbook that was opened by office 365 and worked in 365; back to the work PC and it works fine now. So I assume 365 has better error correction than 2013, but I still have no idea what caused the error in the first place.
I have an inventory xls file, that includes a diagram of the office. When the xls is opened, the VBA automatically makes a backup of the file before any changes are made. That has worked for the past year. Today it has broken.
Yesterday, the file was working fine.
1) I added some info to the xls sheet - nothing unusual; the same sort of info I've added over the past year. I did NOT edit the VBA.
2) I added some objects (shapes-circle, square, etc) to the tab with the office diagram.
Today, the file is not working.
1) The VBA debugger gives an error on open: "Compile Error: Cannot find file or library."
2) Numerous text boxes/shapes have vanished off the tab with the diagram. I did not remove them.
UPDATE 1: I moved a shape on the diagram, and all the text boxes reappeared.
Any suggestions to fix this are appreciated.
'Saves an exact copy of the file upon opening it to the \Back_Tracker location and added today's date to the filename.
Private Sub Workbook_Open()
Dim WBPath As String, WBName As String, TimeStampStr As String, PassW As String
WBPath = ThisWorkbook.Path
WBName = ThisWorkbook.Name
'PassW = "something"
Const cstrBACKUP As String = "Backup_Tracker"
If InStr(1, WBPath, cstrBACKUP) = 0 Then 'prevent backups from making backups of themselves.
TimeStampStr = Format(Now(), "YYYY-MM-DD_hh-mm_")
'Application.StatusBar = "Saving backup..."
ActiveWorkbook.SaveCopyAs Filename:=WBPath & Application.PathSeparator & cstrBACKUP & Application.PathSeparator & TimeStampStr & WBName
'Application.DisplayStatusBar = False
'Application.DisplayStatusBar = True
End If
End Sub
My experience with Excel VBA is that the code can sometimes corrupt when opening. It used to be the case with Access as well, but I'm not sure if that was eventually fixed.
My solution was to either make backup copies after each edit, or use source control for the Excel file. Its the only way to be sure.
Found the problem.
Unchecked this, and it's working now.
Reference
Open the database or application.
Open a module in Design view or press ALT+F11 to switch to the Visual Basic Editor.
On the Tools menu, click References.
Clear the check box for the type library or object library marked as "Missing:"
Currently I'm writting VB functions and save them as an Excel addin .xlam file.
I want to have a .bat script so as to quickly deploy those addins.
Currently, to activate my .xlam addins, I have to Open Excel - File - Option - Addins - Browse to addin files... as below screenshot. This is absolutely manual, repeated & tiring thing to do.
So my need is to automate the activation process.
I was looking for exactly the same sort of thing this morning. I will eventually try something like this out, but I haven't yet. So, here is what I have come to so far:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.addins2.add.aspx
This is an example about how to use Excel automation from C#. From what I see, all these automation interfaces are really COM interfaces, so you are not restricted to C# or Visual Basic (maybe you can use some fancy scripting of Windows to work with them? what I will try is to use python with pywin32, but that's only because it suits my taste).
Then, for registering the addin(s), check this method:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.addins2.add.aspx
I actually saw an example somewhere about how to use it, but I can't find it right now.
Anyway, these are just ideas. I'm very interested on knowing how it all ends ;-)
you can insert this code in your *.xlam in the sheet "ThisWorkBook" this code install and activate the current AddIns, just by opening
Private Sub Workbook_Open()
Dim oXL As Object, oAddin As Object
URL = Me.Path & "\"
normalUrl = Application.UserLibraryPath ' Environ("AppData") & "\Microsoft\AddIns"
AddinTitle = Mid(Me.Name, 1, Len(Me.Name) - 5)
If URL <> normalUrl Then
If MsgBox("Can you Install AddIns ?", vbYesNo) = vbYes Then
Set oXL = Application ' CreateObject("Excel.Application")
oXL.Workbooks.Add
Me.SaveCopyAs normalUrl & Me.Name
Set oAddin = oXL.AddIns.Add(normalUrl & Me.Name, True)
oAddin.Installed = True
oXL.Quit
Set oXL = Nothing
End If
End If
End Sub
After one manually added time, we can update the addin by copy the addin file to Excel addin lair. Here is the .bat script to do it.
set fipAddin=".\FIPphase2.xlam"
set excelAddinLair="%APPDATA%\Microsoft\AddIns"
copy %fipAddin% %excelAddinLair%
Hope it helps!
What is a nice way to install Excel addins?
I want to easily push out Excel addins and it's dependent files that can all be stored in one directory. I want the users to get a never version on request or I want to push it out to their PCs.
This is inside of a company where I have full control over the environment.
you can insert this code in your *.xlam in the "ThisWorkBook" module
this code install and activate the current AddIns
Private Sub Workbook_Open()
Dim oXL As Object, oAddin As Object
URL = Me.Path & "\"
normalUrl = Application.UserLibraryPath ' Environ("AppData") & "\Microsoft\AddIns"
AddinTitle = Mid(Me.Name, 1, Len(Me.Name) - 5)
If URL <> normalUrl Then
If MsgBox("Can you Install AddIns ?", vbYesNo) = vbYes Then
Set oXL = Application ' CreateObject("Excel.Application")
oXL.Workbooks.Add
Me.SaveCopyAs normalUrl & Me.Name
Set oAddin = oXL.AddIns.Add(normalUrl & Me.Name, True)
oAddin.Installed = True
oXL.Quit
Set oXL = Nothing
End If
End If
End Sub
Assuming you are trying to deploy an addin solution which you have created. You would take the set of excel macros you want in the addin and save it to an xlsm excel file in 2007/2010 or simply an xls file for 2003. From there go to your VBA Project Window (Alt + F11) and click once on the "ThisWorkBook" object as shown below and change the workbook properties to become an excel addin by changing the IsAddin to True.
Keep in mind this will remove the excel sheets from view as it becomes an xla or xlam extension. DOn't panic, you just made your excel workbook into an addin. Save it in the top left corner save icon and you have created your addin. You can follow the below procedures to link your addin to other workbooks.
After that, you must be aware of where you saved the excel file because that will the location of the addin file (since they are one and the same).
Now, open an excel file you want this addin connected to.
From there go to the vba project window again and go to tools and reference and select the excel addin you just made.
The approach I use in my company is to store the addin files (and any other dependant files) in an svn repository. Users check out the relavent folder to their machine to a consistent location (can be a bit like herding cats to get everyone to use the same folder structure).
When an update is available simply advise users to update their svn checkout folder.