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.
Related
I'm having an VB6 application that opens an excel macro enabled file using workbooks.open method. Inside this file, there is an userform that gonna be automatically opened when the file is opened. The problem is the workbooks.open method in vb6 application keeps running and does not jump to next line even when I close my userform (I do have codes to close workbook, quit excel app when the userform is closed). I can only close the interface of my userform but Excel still running in background. As long as I dont close the Excel app in task manager, my vb6 does not terminate excel process and of course it will not jump to next line either.
Here is my code in vb6 to open excel file that contains the userform above
As I said, workbooks.open method works but it does not terminate and keeps the excel app runnning in background
P/s: don't mind the Japanese characters
Private Sub Form_Load()
Dim xlapp
Dim xlwkb
If Not App.PrevInstance Then
Set xlapp = CreateObject("excel.application")
Me.Hide
On Error Resume Next
Set xlwkb = xlapp.workbooks.open("C:\Users\david\Desktop\sale-system\fixedfile.xlsm", Readonly:= True)
Set xlwkb = Nothing
If Not xlapp Is Nothing Then
xlapp.Visible = True
xlapp.Application.quit
Set xlapp = Nothing
End If
Unload Me
Else
MsgBox "プログラムが既に起動されています!", vbInformation, "売上管理システム"
Unload Me
End If
End Sub
Here is my code in excel file when close the userform
Application.DisplayAlerts = False
ThisWorkbook.Close False
Application.Visible = True
Application.Quit
End
I really need to solve this problem in this week, any solution?
Thank you guys in advance and please pardon for my english
Here is some images. I hope they will help you guys to understand my problem
This happens when I use Thisworkbook.close False statement:
This happens when I dont use Thisworkbook.close False statement:
Inside this file, there is an userform that gonna be automatically opened when the file is opened.
when I close my userform (I do have codes to close workbook, quit excel app when the userform is closed).
You are doing this incorrectly. Do not close/quit it from the UserForm. Do it from VB6 form. This way, vb6 will be able to handle and clear the objects.
Here, try this (I already tried it and it works...). This will not leave an instance of Excel running. I have commented the code. But if you still have questions then simply ask.
Note: Before you try this (Just for testing purpose), close all Excel application. Even from the task manager.
Private Sub Form_Load()
Dim oXLApp As Object
Dim oXLWb As Object
Dim ICreateatedExcel As Boolean
'~~> Establish an EXCEL application object
On Error Resume Next
Set oXLApp = GetObject(, "Excel.Application")
'~~> If not found then create new instance
If Err.Number <> 0 Then
Set oXLApp = CreateObject("Excel.Application")
'~~> I created instance of Excel
ICreateatedExcel = True
End If
Err.Clear
On Error GoTo 0
'~~> Check if you have an instance of Excel
If oXLApp Is Nothing Then
MsgBox "Unable to get an instance of Excel.", vbCritical, "Excel is Installed?"
Exit Sub
End If
Me.Hide
'~~> Show Excel
oXLApp.Visible = True
'~~> Open file
Set oXLWb = oXLApp.Workbooks.Open("C:\Tester.xlsm")
'~~> This and other lines below will not run till the
'~~> time you close the userform in Excel
'~~> Close the workbook
oXLWb.Close (False) '<~~ Set this to True if you want to save changes
Set oXLWb = Nothing
'~~> If I created Excel then quit
If ICreateatedExcel = True Then oXLApp.Quit
Set oXLApp = Nothing
Unload Me
End Sub
Currently making a quick macro that opens a bunch of other workbooks in new instances:
Sub open_files()
Dim Path As String
Dim Fname As String
Dim xlApp As Object
MyFiles = Dir("C:\my_folder\*xls*")
Do While MyFiles <> ""
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Workbooks.Open ("C:\my_folder\" & MyFiles)
MyFiles = Dir
Loop
End Sub
This works fine on sheets that don't have any ExcelAnt functions in Workbook_Open, but for those that do, I get a popup that says: "Run-time error '1004': cannot run the macro 'Connect'. The macro may not be available in this workbook or all macros may be disabled. "
I've tried forcing in the add-in before running the "connect" part of the code but to no avail.
Public Sub Workbook_Open()
Dim TestWkbk As Workbook
Set TestWkbk = Nothing
On Error Resume Next
Set TestWkbk = Workbooks("ExcelAnt-AddIn64.xll")
On Error GoTo 0
If TestWkbk Is Nothing Then
Set TestWkbk = Workbooks.Open("C:\ExcelAnt\ExcelAnt-AddIn64.xll")
End If
Dim hostenv As String
hostenv = Left(Environ("computername"), 3)
Application.Run "Connect", "prd"
End Sub
To clarify, the sheet if opened manually works fine.
Any help would be appreciated. Thanks in advance.
Use RegisterXLL with the new instance of Excel (xlApp).
xlApp.RegisterXLL "C:\ExcelAnt\ExcelAnt-AddIn64.xll"
I am looking for a way for VBA in PowerPoint to automatically identify the only open Excel file on my computer and use this Excel file to read data from it. I'd like to avoid having to manually insert Excel file path in my code. Is it possible?
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("PATH\FILENAME.xlsm")
xlBook.Application.Visible = False
Thank you!
Try the next code, please:
Sub testOpenWorkbook()
Dim Ex As Object, wb As Object
On Error Resume Next
Set Ex = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Err.Clear: On Error GoTo 0
MsgBox "There is not any Excel session open...", vbInformation, "Ups...": Exit Sub
Else
On Error GoTo 0
If Ex.Workbooks.count = 1 Then
Set wb = Ex.Workbooks(1)
Else
MsgBox "There are more Excel Workbooks open...", vbInformation, "Ups...": Exit Sub
End If
End If
End Sub
I'm trying to find a way to open Excel using Outlook VBA, but only if it's not already open. I managed to find some code on the internet that opens Excel, does changes and then closes it, but it doesn't behave nicely if the Excel workbook is already open(it does apply the changes, but it no longer closes the Excel workbook, and it simply leaves it with a grey interior; also, sometimes it doesn't even show in the explorer anymore and I have to close it from the task manager).
I would also greatly appreciate if someone could explain what most of the code does.
Public xlApp As Object
Public xlWB As Object
Public xlSheet As Object
Sub ExportToExcel()
Dim enviro As String
Dim strPath As String
'Get Excel set up
enviro = CStr(Environ("USERPROFILE"))
'the path of the workbook
strPath = enviro & "\Documents\test2.xlsx"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
On Error GoTo 0
'Open the workbook to input the data
Set xlWB = xlApp.Workbooks.Open(strPath)
Set xlSheet = xlWB.Sheets("Sheet1")
' Process the message record
On Error Resume Next
xlWB.Close 1
If bXStarted Then
xlApp.Quit
End If
End Sub
I know what the xlWb and xlSheet objects do, and how they're declared, and I also understand what the environ function and strPath string do, but I don't undestand why we need the bXStarted boolean, what Set xlApp = GetObject does, why the Application.StatusBar message doesn't get displayed, the difference between GetObject and CreateObjectand why so many error tests are needed.
Thanks in advance.
The difference between get object and create object is in the title, one will get open excel.application, if there is an error err<>0 then it creates an excel.application. I think you'll be getting a saveas message, as the file may not be saving, but open, and you're instructing it to save, the error resume next is skipping it . Try saving before just a .close If you remove the on error resume next the error wont be skipped and will be shown.
Sub explaination()
Dim blnDidICreateExcel As Boolean ' Please read MSDN on boolean
Dim objToHoldExcelCreatedOrNot As Object ' Please read MSDN on objects create/get
' Does the user have Excel open, if i try to get it, then there will be an error logically if not
Set objToHoldExcelCreatedOrNot = GetObject(, "Excel.Application")
' Was there an error
If Err <> 0 Then
' There was, so i need to create one
Set objToHoldExcelCreatedOrNot = CreateObject("Excel.Application")
blnDidICreateExcel = True ' Yes, i created it
End If
' Do the neccessary
' CLose the workbook
' Did i create this Excel, if so tidy up
If blnDidICreateExcel Then objToHoldExcelCreatedOrNot.Quit
End Sub
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.