Save and close workbook without prompting after running a macro? - vba

I want to save and close the Excel file in which I am executing a macro (Module : modBex, Sub: Test) via VBScript.
The following code is working fine, except the Excel file (macro_file.xlsm) doesn't get saved.
Here is my code:
Option Explicit
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\macro_file.xlsm", 0, True)
xlApp.Run "modBex.Test"
xlApp.DisplayAlerts = False
xlBook.Save
xlApp.Quit
Set xlApp = Nothing
Set xlAddin = Nothing
Set xlBook = Nothing
WScript.Quit

Change the line
Set xlBook = xlApp.Workbooks.Open("C:\macro_file.xlsm", 0, True)
to
Set xlBook = xlApp.Workbooks.Open("C:\macro_file.xlsm", 0, False)
as I believe that flag is setting the file to open as Read Only, leading to an error when saving the document.
In addition to this, if you leave any and all alert suppressing (ie .DisplayAlerts = False)until you know you have working code, then it makes things like this easier to spot.

Related

Excel become unresponsive after running macro using vbs

From VBS, I am running a macro "Inc" in an already opened excel workbook. The macro is getting executed (this is confirmed after seeing the updated cell value after reopening the workbook), but the excel becoming soon after I run the code. Can you please let me know what I need to change in my code.
Macro code :
Sub Inc()
Dim bs As Worksheet
Set bs = Workbooks("Main.xlsm").Sheets("TimeStampWork")
bs.Range("K11").Value = bs.Range("K11").Value + 1
bs.Range("L11").Value = Now
bs.Range("L11").NumberFormat = "dd-mm-yyyy, hh:mm:ss"
End Sub
VBS code :
Option Explicit
Dim xlApp, xlBook
Set xlBook = GetObject("E:\Main.xlsm")
Set xlApp = xlBook.Application
xlApp.Visible = True
xlApp.Run "Inc"
xlBook.Save
Set xlBook = Nothing
Set xlApp = Nothing
Update:
Instead of the macro "Inc" I tried to run a macro with just a simple message box, and it works without any issues. I can't see any problems with excel.
This issue has been resolved now. Changed my macro with screenupdate option which resolved the issue
Sub Inc()
Application.ScreenUpdating = False
Dim bs As Worksheet
Set bs = Workbooks("Main.xlsm").Sheets("TimeStampWork")
bs.Range("K11").Value = bs.Range("K11").Value + 1
bs.Range("L11").Value = Now
bs.Range("L11").NumberFormat = "dd-mm-yyyy, hh:mm:ss"
Application.ScreenUpdating = True
End Sub

Run a macro using VBS on already opened excel

I would like to run a macro on an Excel using VBS code. My Excel will be always open. I am using below code for that.
Excel: main.xlsm, macro name: Inc
Option Explicit
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("E:\Main.xlsm", 0, False)
xlApp.Run "Inc"
xlBook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
WScript.Quit
The problem is whenever I run the code, it looks like the Excel trying to open in read only mode. As soon as the VBS runs, I am getting "save changes window" and saving the sheet as a copy.
I would like to save the changes on the same sheet rather than saving it as a new copy.
Excel lets you save a copy because you create a new instance of Application object, so you open the workbook again in read-only mode. To get a reference to Application object of the particular running instance of Excel with already opened workbook you should use the following lines:
Set xlBook = GetObject("E:\Main.xlsm")
Set xlApp = xlBook.Application
instead of
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("E:\Main.xlsm", 0, False)
Then you just need to save that workbook with .Save.
You are creating a new Instance with Set xlApp = CreateObject("Excel.Application"), also the WB is then opened twice. Easiest fix, if you just close the WB before executing your code, then it should work fine. Also when closing use xlBook.Close SaveChanges:=True, no prompt should appear then.

Using other workbook without opening it

I created Excel macro, in which I'm searching current date in other, closed workbook and getting the number of the row where this date is. Everything works perfectly, but only if I'm opening this other workbook.
Is there a possibility to make the code works without opening this workbook? Or maybe it's possible to make this workbook not to appear, because I don't want it to appear on the screen?
I've tried many methods, e.g. Application.ScreenUpdating = false or ActiveWorkbook.Windows(1).Visible = False but it doesn't work the way I want it.
You need to create a new Excel Application to be able to hide the Window:
Dim xlApp As New Excel.Application 'New Excel Application
Dim xlWB As Excel.Workbook 'Workbook Object
xlApp.Visible = False 'Hide Application
Set xlWB = xlApp.Workbooks.Open("PATH TO FILE") 'Open File in new App
'do something here
xlWB.Sheets(1).Cells(1, 1).Value = "Hello, this is a Test"
xlWB.Close 'Close Workbook (Use SaveChanges:=True to save changes)
Set xlWB = Nothing 'Clear Object
xlApp.Quit 'Quit Excel App
Set xlApp = Nothing 'Clear Object

Macro gets disabled if called using a vb script

I am trying to call my excel macro using vbs. Here is a snippet of my code.
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Folder\Test_PO.xls")
objExcel.Application.Visible = True
objExcel.Application.Run "C:\Folder\Test_PO.xls!Data_Analysis"
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
WScript.Echo "Finished."
WScript.Quit
Now the problem here is that i am able to open the file but the macro somehow gets disabled here and shows me 'macro may not be present or may be disabled'. I am sure i am calling correct macro name but as soon as the file is opened the Add-ins tab where i had configured the macro to run from gets dissapeared.This does not open if i open the file manually , i can see the tab and run the macro from the tab itself. Any suggestions how i could overcome this problem and get the macro to run ?
Try this
Dim objExcel, objWorkbook
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Folder\Test_PO.xls")
objExcel.Visible = True
objExcel.Run "Data_Analysis"
objWorkbook.Close
objExcel.Quit
Set objWorkbook = Nothing
Set objExcel = Nothing
WScript.Echo "Finished."
WScript.Quit
EDIT
If the macro is in a module then the above will help. If the macro is in a sheet say, Sheet1 then replace the line
objExcel.Run "Data_Analysis"
with
objExcel.Run "sheet1.Data_Analysis"
FOLLOWUP
Try this code.
Dim objExcel, objWorkbook, ad, FilePath
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
For Each ad In objExcel.AddIns
If ad.Name = "Converteam.xla" Then
FilePath = ad.Path & "\Converteam.xla"
Exit For
End If
Next
objExcel.Workbooks.Open (FilePath)
Set objWorkbook = objExcel.Workbooks.Open("C:\Folder\Test_PO.xls")
objExcel.Run "Data_Analysis_Converteam"
objWorkbook.Close
objExcel.Quit
Set objWorkbook = Nothing
Set objExcel = Nothing
WScript.Echo "Finished."
WScript.Quit
EXPLANATION:
When you use CreateObject, the Add-Ins are not installed by default. Please see this link.
Topic: Add-ins do not load when using the CreateObject command in Excel
Link: http://support.microsoft.com/kb/213489/
You have to load the Add-In and then call the relevant macro. Also the name of your macro is not Data_Analysis but Data_Analysis_Converteam
HTH
To add to Siddhart's answer - you can load the addins you require in a VBScript like this:
objExcel.RegisterXLL("analys32.xll") 'For XLL addins
objExcel.Workbooks.Open(objExcel.LibraryPath & "\analysis\atpvbaen.xla") 'For standard XLA addins
objExcel.Workbooks.Open("C:\Program Files\MyAddins\MyAddin.xla") 'for custom XLA addins

Writing values to already existing worksheet

I already have an Excel worksheet. How do I write some values to that worksheet from my VB application?
Note: This is an Excel-VBA solution; however, someone has changed the question tag to VB.NET since.
Sub MySub()
Set xlApp = CreateObject("Excel.application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Open("C:\MyBook.xls")
Set xlSheet = xlBook.ActiveSheet
xlSheet.Range("A1").Value = "Hello, world!"
End Sub