How can I automate an Excel file refresh and send via email? - excel

I have a few Excel files (Pivot Tables & Charts) that have a data connection to an Access database. What I'm hoping to do is have them automatically refresh and get emailed on the 1st of each month.
I do something similar in Access, where I have a database with a query that gets run and emailed. I accomplished this by creating a form in the database, having the form open automatically when the database is opened, and then used VBA within the form to run the query (DoCmd.OpenQuery) and then email it (DoCmd.SendObject)
Can I do something similar with an Excel file? I'm thinking I could set the Connection Properties under the Data tab to "Refresh data when opening the file", but that assumes the user has an active network connection as the database is on a server. Also, if the user saves the file and opens it again in the future, it will have current data and not the original data.
So in a nutshell I need to:
Open Excel File
Refresh Data
Email File

Here's what I ended up doing:
In Access, I have some Make Table queries that make tables (duh) which will be used by the Excel files. I created a form, have it open automatically when the database opens, and in the Form_Load() event I put the following:
With DoCmd
.SetWarnings False
.OpenQuery "My Query 1"
.OpenQuery "My Query 2"
.SetWarnings True
End With
Application.Quit
In each Excel file, I found some VBA code online to create a PDF and then email it. In the Workbook_Open() event, I have the following:
ActiveSheet.Unprotect
ActiveWorkbook.Connections("MyConnection").Refresh
ActiveSheet.Protect
Call FunctionThatCreatesPDFAndEmails
ActiveWorkbook.Close False
I then put some tasks in Windows Task Scheduler .. one to open the Access database (which causes the Make Table queries to run and then closes itself) and then one task for each Excel file that I need to update and email. So now I have Access updating the source data tables, and then Excel opening each file, refreshing the data, and emailing a PDF of itself. Mission Accomplished!

Related

How to Get Data on Close Workbook Table on Excel VBA without Openning It?

I have three Workbook,
First is for my Inventory System for Admin,
Second is my Inventory System for Employees, and
Third is my Database.
Does anyone know how to get Data Table on Database without Opening it and put it either in Admin or Employee's Inventory System?
All you need to do is go to "Data" tab in ribbon > Get Data > From File > From Workbook. Then open the "Database" excel file.
This will allows you to preview the data from the "Database" sheet. If you don't need to make any changes you can click load directly and the data will be copied.
You can check out this video for more examples on how to use Power query.
I also recommend checking out this video on how Power Query can help you.

VBScript to dimsiss Excel warning dialog about external data sources?

I have an Excel file that's linked to a SQL database view. The Excel file needs some monthly massaging that I am trying to do with a VBScript but this dialog box keeps coming up and interrupting my VBScript:
This workbook contains links to one or more external sources that could be unsafe.
If you trust the links, update them to get the latest data. Otherwise, you can keep working with the data you have.
And buttons for Update, Don't Update and Help
I do trust the data (it's our on-site SQL server), I don't want to refresh the data with this specific script (it takes about 15 minutes and it is refreshed via another process). But the dialog box is holding up my whole VBScript, how do I dismiss it? I've tried ExcelObj.DisplayAlerts = False and ExcelObj.EnableEvents = False but neither is making this dialog box go away. Please help!

Workbook_Open event erroneously triggered when linking to xlsm workbook

I am running a [Edit] read-only (3rd party!) [/Edit] SQL database, an xlsm, an xls (saved copy of the xlsm, just without macros), and an mdb (Access database).
They are linked as follows:
The xlsm gets data from the SQL database every 5 minutes by Application.OnTime code, triggered on Workbook_Open.
The xlsm then saves itself as a 2003 xls.
Since the code modules still exist at that moment (the workbook is not yet closed), I can open the Access mdb and refresh the Excel link to a table in the 2003 data, then close Access.
I then reopen the xlsm, triggering a new Application.Ontime instance.
I finally close the 2003 xls workbook.
This sequence runs during office hours and at the first run after 5pm sets a timer to start the process again in the morning.
My problem is whenever I open a file with a connection or link to the xlsm, the Workbook_Open event seems to trigger and I end up with the 2003 xls open on the 'client' computers. I know it's not just a leftover Application.OnTime on mine (the PC I originally created the files on) because now it's in use and other clients who have never opened the xlsm get the 2003 file randomly popping up when they're using Excel.
If that's not enough to trigger someone to know what's going on, I'll happily post code - but I am hoping someone has seen this before or knows of this rookie mistake and can simply give me a line I'm missing.
Thanks so much for your help!
I would think the reason the XLS pops up on your clients is because Excel has to open the linked XLSM in order to access the data, and as such, Workbook_Open gets triggered.
A few alternatives to the suggestions you have in the comments;
Check Environ("Username") before executing Application.OnTime so that it will only run if the file is opened by specific users.
Use a launcher-file to open the XLSM in "admin mode" - you can do this by using the launcher-file to set a Global-scope variable, and then have the XLSM check this variable before running Application.OnTime.
Make the XLSM test for some dummy-file on the local computer - one that you easily can create by hand on the computer you need regular access to the XLSM on. For example the XLSM can look for C:\Data\xlsm-enabler.txt or whatever works, and only run the relevant macro if the file exists.
Similar to the above, use SaveSetting to set a registry key on the "admin" machine(s) and check against this inside the XLSM.
Set your XLSM up to accept parameters, and use such parameters to open the file in "admin mode". This method is outlined several other places # SO, like here.
This is more of a hunch - you might be able to leverage the Workbook.ReadOnly property in your XLSM as a conditional. This assumes that Excel opens the XLSM in readonly-mode when accessing it via data connections, which I don't know whether is actually the case.
An easy solution would be to have all other files link to the .xls file, instead of the .xlsm macro enabled file. Downside to this approach would be that you have to (manually) update all your references.
It would be way easier to do everything in Access. Then if you need to represent the data in Excel you can have a Workbook with a link to the Access table. This removes a step and it uses the right tool for the job. Access is for linking to database tables. Excel is for processing data views.
SQL > Access > Excel

Stop updating PowerPivot when opening Worksheet

I have an excel workbook that is to be used as a report, that is distributed to different people. The workbook gets data from an input file (another Excel file) that is not available to all the people who are receiving the report.
My problem is, that when the people who does not have access to the input file opens the report, the workbook tries to update the data, but fails, as the user does not have access to the input file. Is there a way to stop the sheet from auto-updating when it is opened?
Based on the versions of excel I have used, Excel 2013 and 2016 that does not sound like the standard behavior I have seen.
There would be a couple things to check:
1) Is there macros in the file, which run on open and force refresh. VBA command to refresh a data model being the following ThisWorkbook.Model.Refresh
2) Right click on the pivot tables --> Pivot table settings --> Data, check if refresh data when opening the file is selected. Not sure on this one but it might be forcing a refresh of the model.
3) If you are using power query, open the queries & connection tab, right click each query and select properties. There is an option for Refresh data when opening the file.

Export data back to existing csv file

I have created an application already in excel where one sheet (Sheet 1) acts as a database that stores data for thousands of records. Another sheet (Sheet 2) acts as a form where users selects a record > the form gets populated with data from Sheet 1 > user updates fields > and then save it back to Sheet 1.
This has been working great, except now we need to be able to have multiple users working in the same worksheet making updates at the same time. I cannot use the Excel Share Workbook functionality because many parts of my VBA code require unprotecting and reprotecting certain sheets and that functionality doesn't work on shared workbooks.
So now, I have split the workbook into two separate workbooks. Workbook 1 has the data in it and is saved as a .csv file. Workbook 2 is just a form that has a connection to workbook 1 via the get External Data functionality already in Excel. I can import data from the .csv file without a problem, but the only way I am finding to export the data back into the .csv file is through doing a save as. This causes a problem because it turns my multi-sheet macro enabled workbook into a csv file after the save.
Is there any other way I can update the csv file without saving my workbook as a .csv?
I don't need any code done, I just need ideas more than anything. This is already a fairly large application and due to security restrictions I have already been told I cannot use a SQL server connection and we do not have access to Access to use an Access database. So solution has to stay inside excel/text files?

Resources