Why does workbook turns hidden after vba executes - excel

Here is the sub
Sub test()
With ThisWorkbook
.Worksheets("Sheet1").Range("A1").Value = 5
.Save
End With
End Sub
Here is the vbscript that starts the sub
dim eApp
set eApp = GetObject("C:\Users\Owner\Desktop\Book1.xlsm")
eApp.Application.Run "Book1.xlsm!test"
set eApp = nothing
It executes fine but the workbook gets hidden for some reason . I have to unhide it View - -> Unhide
Why does this happen and how to fix it

A clearer way of running your vbs would be as follows
Dim ObjExcel, ObjWB
Set ObjExcel = CreateObject("excel.application")
Set ObjWB = ObjExcel.Workbooks.Open("C:\Users\Owner\Desktop\Book1.xlsm")
'make it visible
ObjExcel.Visible = True
ObjExcel.Run "Book1.xlsm!test"
ObjWB.Close False
ObjExcel.Quit
Set ObjExcel = Nothing

Related

Creating a macro-enabled Excel file from within Word VBA

The following code, when used within Word VBA, successfully creates a standard Excel workbook in the chosen folder:
Const ExcelSourcePath = "C:\Users\Holge\_Universet i billeder\_ExcelDocs\"
Dim xlAppl As New Excel.Application
Dim xlBook As New Excel.Workbook
Private Sub TestCreateExcelFile()
Set xlAppl = CreateObject("Excel.Application")
Set xlBook = xlAppl.Workbooks.Add
Application.DisplayAlerts = False
xlBook.SaveAs FileName:=ExcelSourcePath & "TestFile.xlsx"
Application.DisplayAlerts = True
xlBook.Close False
Set xlBook = Nothing
xlAppl.Quit
Set xlAppl = Nothing
End Sub
However, what I really need is to create a macro-enabled file. But if I change ".xlsx" into ".xlsm" I get a RTE 1004, "Wrong file type name" (translated from Danish).
Perhaps CreateObject should be called with another argument, but which one? I have not been alble to find possible values for this argument.
In the following code:
Dim xlAppl As New Excel.Application
Dim xlBook As New Excel.Workbook
Private Sub TestCreateExcelFile()
Set xlAppl = CreateObject("Excel.Application")
Set xlBook = xlAppl.Workbooks.Add
A new Excel Application instance is created twice. You need to keep the one line of code for create a new instance, not doing that twice.
Moreover, an instance of the Workbook class can't be created by using the New operator like shown in your code:
Dim xlBook As New Excel.Workbook
Most probably the intention was to declare objects out of the sub and then instantiate them:
Const ExcelSourcePath = "C:\Users\Holge\_Universet i billeder\_ExcelDocs\"
Dim xlAppl As Excel.Application
Dim xlBook As Excel.Workbook
Private Sub TestCreateExcelFile()
Set xlAppl = CreateObject("Excel.Application")
Set xlBook = xlAppl.Workbooks.Add
Application.DisplayAlerts = False
xlBook.SaveAs FileName:=ExcelSourcePath & "TestFile.xlsx"
Application.DisplayAlerts = True
xlBook.Close False
Set xlBook = Nothing
xlAppl.Quit
Set xlAppl = Nothing
End Sub
Just need to remove New operators from the code with declarations.

Can't create Excel spreadsheet from Word

I'm trying to write a VBA macro from within Word to create an Excel spreadsheet and export data to it. I'm having trouble just creating the new spreadsheet. For the following macro, I get "Object doesn't support this property or method" on the line
Set myWb = myExcel.Workbooks.Add
in the code
Private Sub CreateExcel2()
Dim myExcel As Object
Dim myWb As Object
Set myExcel = CreateObject("Excel.Application")
Set myWb = myExcel.Workbooks.Add
Application.DisplayAlerts = False
myWb.SaveAs FileName:="D:\test\dump.xls"
Application.DisplayAlerts = True
myWb.Close False
Set myWb = Nothing
myExcel.Quit
Set myExcel = Nothing
End Sub
You can try:
On Error Resume Next
Set oExcel = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Err.Clear
Set oExcel = CreateObject("Excel.Application")
End If
On Error GoTo 0
oExcel.visible = true 'to check of excel opens
'try Set myWb = myExcel.Workbooks.Add()
'try Set myWb = oExcel.Workbooks.Open(sPathFile)

Excel don't close in background

I am opening Excel from MsAccess and when I finished the task I close it, but Excel remain opened in backgroud until I close MsAccess
this is the code
Public Sub closeExcel()
Dim xlApp
Dim wkb As Object
Dim wks As Object
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If err <> 0 Then
Set xlApp = CreateObject("Excel.Application")
End If
With xlApp
Set wkb = .Workbooks.Add
Set wks = wkb.Worksheets(1)
wkb.Close True
Set wks = Nothing
Set wkb = Nothing
.Quit
End With
End Sub
I need to remain Access runing, but I want that Excel be closed.
How can I do this?
You are cutting off the worksheet. Try this:
With xlApp
Set wkb = .Workbooks.Add
Set wks = wkb.Worksheets(1)
' Do stuff.
Set wks = Nothing
wkb.Close True
Set wkb = Nothing
.Quit
End With
Set xlApp = Nothing

Run Excel Macro using VBS

Trying to run excel macro using command prompt through VBS. Excel file open ups but not able to run macro. The name of Macro as well as module is "Wallet"
While tryig to view macro, it reflects as "PERSONAL.XLSB!WALLET"
Pls help
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("excel.application")
Set xlBook = xlApp.Workbooks.Open("C:\Output\abc.xlsb")
xlApp.Visible = True
xlApp.Run "Wallet"
xlApp.Save
xlApp.ActiveWorkbook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
Change your code like that
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("excel.application")
Set xlBook = xlApp.Workbooks.Open("C:\Output\abc.xlsb")
xlApp.Visible = True
xlApp.Workbooks.Open ("Path to your Personal.XLSB")
xlApp.Run "Personal.XLSB!Macro"
'xlApp.Save <= This line would not work, xlApp does not have a Save Method
'xlApp.ActiveWorkbook.Close <= You already have the reference to the workbook. It is xlBook
xlBook.Close True ' <= This will save and close the workbook you opened
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
If you do not know where your Personal.xlsb is try the standard location.
xlApp.Workbooks.Open (xlApp.StartupPath & xlApp.PathSeparator & "Personal.XLSB")

Access VBA How to add new sheets to excel?

I am running a few modules of code in access and am writing data into
Excel. When I write the first time, data gets written properly. But again
when I try, the new data is written on top of the old data. What should I do to
insert a new sheet?
My existing code is
Dim objexcel As Excel.Application
Dim wbexcel As Excel.Workbook
Dim wbExists As Boolean
Dim objSht As Excel.Worksheet
Dim objRange As Excel.Range
Set objexcel = CreateObject("excel.Application")
On Error GoTo Openwb
wbExists = False
Set wbexcel = objexcel.Workbooks.Open("C:\REPORT1.xls")
Set objSht = wbexcel.Worksheets("Sheet1")
objSht.Activate
wbExists = True
Openwb:
On Error GoTo 0
If Not wbExists Then
objexcel.Workbooks.Add
Set wbexcel = objexcel.ActiveWorkbook
Set objSht = wbexcel.Worksheets("Sheet1")
End If
I think that the following code should do what you want. It's very similar to yours, except it uses the return values from the .Add methods to get the objects you want.
Public Sub YourSub()
Dim objexcel As Excel.Application
Dim wbexcel As Excel.Workbook
Dim wbExists As Boolean
Set objexcel = CreateObject("excel.Application")
'This is a bad way of handling errors. We should'
'instead check for the file existing, having correct'
'permissions, and so on, and actually stop the process'
'if an unexpected error occurs.'
On Error GoTo Openwb
wbExists = False
Set wbexcel = objexcel.Workbooks.Open("C:\REPORT1.xls")
wbExists = True
Openwb:
On Error GoTo 0
If Not wbExists Then
Set wbexcel = objexcel.Workbooks.Add()
End If
CopyToWorkbook wbexcel
EndSub
Private Sub CopyToWorkbook(objWorkbook As Excel.Workbook)
Dim newWorksheet As Excel.Worksheet
set newWorksheet = objWorkbook.Worksheets.Add()
'Copy stuff to the worksheet here'
End Sub

Resources