command to display Excel after saving - excel

This is the vba Command that I am using to make a spreadsheet and save it:
DoCmd.TransferSpreadsheet acExport, , "query_alldata1", "C:\All_Data_2014.xlsx", True
I also want it to open or display automatically on the screen. I would appreciate any guide.
This is how my subroutin looks like:
Private Sub btnAlldatagen_Click()
On Error GoTo Err_btnAlldatagen_Click
Dim stDocName As String
stDocName = "query_alldata1"
DoCmd.TransferSpreadsheet acExport, , "query_alldata1", "C:\All_Data_2014.xlsx", True
'Workbooks.Open "C:\All_Data_2014.xls"
MsgBox ("Preparing all Data Report")
Exit_btnAlldatagen_Click:
Exit Sub
Err_btnAlldatagen_Click:
MsgBox Err.Description
Resume Exit_btnAlldatagen_Click
End Sub
I am using Workbooks.open command but it does not open the worksheet.

Oh, this one is easy.
After
DoCmd.TransferSpreadsheet acExport, , "query_alldata1", "C:\All_Data_2014.xlsx", True
Place:
Dim xl As Object
Set xl = CreateObject("Excel.Application")
With xl
.Visible = True
.Workbooks.Open ("C:\All_Data_2014.xlsx")
'.Do other things with excel here
'.Workbooks("Test1.xlsm").Close , False '<-- Uncomment to close workbook
'.Quit '<-- Uncomment to close Excel
End With
'Set xl = Nothing <-- Uncomment to set Excel to Nothing

You can add a reference to the Excel runtime library (Microsoft Excel * Object Library), and work with Excel VBA.
Dim xlApp as Excel.Application
Set xlApp = New Excel.Application
xlApp.Workbooks.Open("C:\All_Data_2014.xlsx")
xlApp.Visible
Set xlApp = Nothing
IF you are planning on doing this multiple times, you should make sure to create the xlApp variable once at the beginning of your code, since opening Excel is slow.

Related

How to call Excel macro in xlam file from Outlook?

I want to call a macro from Outlook.
I can do it with an Excel .xlsm file.
How can I call a macro in an addin .xlam file?
Example for .xlsm
Sub trans_outlook()
Dim xlApp As Object, xlWkb As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False ' can be False if you do not wont see reaction, byt make sure is not fail
Set xlWkb = xlApp.Workbooks.Open("C:\Users\xyz.xlsm")
xlApp.Run "xyz.xlsm!Test"
xlWkb.Close savechanges:=False
xlApp.Quit
End Sub
You need to go to Tools -> References. Find Microsoft Excel 14.0 Data Objects Library in the list, and check the box next to it. Click OK
Dim ExApp As Excel.Application
Dim ExWbk As Workbook
Set ExApp = New Excel.Application
Set ExWbk = ExApp.Workbooks.Open("C:\Folder\Folder\File.xls")
ExApp.Visible = True
ExWbk.Application.Run "ModuleName.YourMacro"
ExWbk.Close SaveChanges:=True

Display Alerts in Excel from Access

I am opening an excel workbook from excel and delete a sheet inside. I want to do this without the message from Excel:
"Excel will permanently delete this sheet, do you want to continue"
However I cannot make the "DisplayAlerts = False" work correctly.
Public Sub xportQuery()
Dim appExcel As Excel.Application
Dim myWorkbook As Excel.Workbook
Dim PathDaily, FileName As String
PathDaily = Forms!Menu!Text69
FileName = Forms!Menu!Text84
Set appExcel = CreateObject("Excel.Application")
Set myWorkbook = appExcel.Workbooks.Open(PathDaily & FileName)
appExcel.Visible = True
'Set appExcel = Nothing
Set myWorkbook = Nothing
appExcel.DisplayAlerts = False
Workbooks(FileName).Sheets("Sheety").Delete
appExcel.DisplayAlerts = True
End Sub
Writing it this way I get a "Subscript out of range" on the Sheets("Sheety").delete
If I take of the two DisplayAlerts lines, the code works but with the alert.
How do I write my code correctly to work without any alert message?
Note: DoCmd.SetWarnings didn t work either as the message is displayed in Excel
Thanks To Sam's Comment:
it works either by changing
Workbooks(FileName).Sheets("Sheety").Delete
into
myWorkbook.Sheets("Sheety").Delete
or
AppExcel.Workbooks(FileName).Sheets("Sheety").Delete
However the rest of the macro can still use Workbooks(Filename) without the "AppExcel." Reference

Trouble exporting query to Excel from Access

I am attempting to export a query based on two combo boxes to excel. Two weeks ago, this code worked. I have moved the database, and updated the code. Now, when I run it, it will open the query, and open excel, but it will NOT rewrite the excel file. It just opens up the old data.
Access 2010
Code:
Private Sub Command14_Click()
DoCmd.OpenQuery "rtnbymonth_qry", acViewNormal, acEdit
DoCmd.OutputTo acOutputQuery, "rtnbymonth_qry", acFormatXLS, "S:\Sales & Use Tax\2016\export.xls"
DoCmd.Close acQuery, "rtnbymonth_qry", acSaveNo
'Open Excel
Call OpenSpecific_xlFile
End Sub
Also here is the code for the program to open excel:
'mini program to open excel
Sub OpenSpecific_xlFile()
' Late Binding (Needs no reference set)
Dim oXL As Object
Dim oExcel As Object
Dim sFullPath As String
Dim sPath As String
' Create a new Excel instance
Set oXL = CreateObject("Excel.Application")
' Only XL 97 supports UserControl Property
On Error Resume Next
oXL.UserControl = True
On Error GoTo 0
' Full path of excel file to open
On Error GoTo ErrHandle
sFullPath = CurrentProject.Path & "\export.xls"
' Open it
With oXL
.Visible = True
.Workbooks.Open (sFullPath)
End With
ErrExit:
Set oXL = Nothing
Exit Sub
ErrHandle:
oXL.Visible = False
MsgBox Err.Description
GoTo ErrExit
End Sub
Turns out that the location I was saving it to is different than the location where the database is stored. The correct file was saved, just not in the location I was opening it. Easy fix! Programmer error.

VBA Using Outlook to open excel not opening

Hi Thanks for your help.
I have the follow code in Outlook 2010 & 2007:
Sub Openexcel()
Dim xlApp As Object
Dim sourceWB As Workbook
Dim sourceSH As Worksheet
Dim strFile As String
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Visible = True
.EnableEvents = False
End With
strFile = "E:\All documents\susan\work\Excel projects\saving files to directory Clean.xls"
Set sourceWB = Workbooks.Open(strFile, , False, , , , , , , True)
Set sourceSH = sourceWB.Worksheets("Sheet2")
sourceWB.Activate
End Sub
This code works the first time I use it after opening outlook but if I then close the excel file I can not use it again. I need to reopen this work book about 3 times
The Question at
Outlook VBA open excel
seen to have the same problem but I did not understand the answer.
"I got it figured out. I was opening a different workbook and then closing it before I try to open the second one and that was interfering with it. To fix this I kept the excel app open and reset the workbook object to the new workbook i wanted"
If some someone could help with the additional code that would be great.
Great code found at Excel interactions don't work after Excel file opened.
why I could not find this last week who knows.
Sub Openexcel()
' change
Dim xlApp As Excel.Application
Dim sourceWB As Excel.Workbook
Dim sourceSH As Excel.Worksheet
'change
Set xlApp = New Excel.Application
With xlApp
.Visible = True
.EnableEvents = False
'.UserControl = False
'.DisplayAlerts = False
'.AskToUpdateLinks = False
End With
strFile = "E:\All documents\susan\work\Excel projects\saving files to directory Clean.xls"
'change
Set sourceWB = xlApp.Workbooks.Open(strFile, , False, , , , , , , True)
Set sourceSH = sourceWB.Worksheets("Sheet2")
sourceWB.Activate
End Sub
Thanks guys for all your thoughts.
You need to declare the Excel Applicaton at the global scope and use it to open another workbooks. Not to create a new Excel instance for opening new files. You may find the How to automate Microsoft Excel from Visual Basic article helpful.
For example, declare the Application object outside the event handler:
Dim oXL As Excel.Application
Private Sub Command1_Click()
Dim oWB As Excel.Workbook
Thus, you will be able to re-use it for closing and opening new workbooks.

VBScript code to keep Excel file open

I have a VBScript code to open an excel file, run a macro and close it. Fine.
Now, the only thing I want to change is to leave the file open.
If I remove the 2 lines of code xlApp.activewindow.close and xlApp.Quit, then the workbook and the application are closed anyway, but they remain open in the background (Excel process still active in Task Manager). Hence, it is impossible to re-run the macro later on the same file by calling the script again (which is exactly what I want to do).
Why?
Here is the code:
Option Explicit
On Error Resume Next
MyTest
Sub MyTest()
Dim xlApp
Dim xlBook
Dim fpath
Dim fname
' Excel application running? if not, open Excel
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If xlApp <> "Microsoft Excel" Then
Set xlApp = CreateObject("Excel.Application")
End If
Err.Clear
' correct Excel file open? if not, open it
fpath = "D:\Desktop\"
fname = "MyTest.xls"
xlApp.Workbooks(fname).Activate
If Err = 0 Then
' no error, so it has been possible to activate the workbook
Set xlBook = xlApp.Workbooks(fname)
Else
' unable to activate, so workbook was not open -> open it now
Set xlBook = xlApp.Workbooks.Open(fpath & fname, 0, True)
End If
Err.Clear
' now run the desired macro in the excel file
xlApp.Run "HelloWorld"
' WANT TO CHANGE THIS
xlBook.saved = True
xlApp.activewindow.close
' AND THIS
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
You just need to make your new instance of Excel visible. Do this right after creating it:
xlApp.Visible = True
This line of code will close your current activated workbook (by now, it is D:\Destop\MyTest.xls);
xlApp.activewindow.close
This line will quit Excel application;
xlApp.Quit

Resources