I have an outlook procedure that auto saves an excel attachment to a certain location on my hard drive.
I also have a VBA script in Excel that is designed to use the data from that file and send off emails to particular parties. Normally I would have to open the file and run the VBA script. Is there a way to have this VBA script auto-execute once the file is saved from outlook?
The macro in the excel file won't run just because you have saved it.
However, you can run the macro by opening the workbook and calling the macro at Workbook_Open event
1.Open the workbook from outlook programatically
2.Let's say your macro procedure is Sub runMe() then you could write following function in Workbook
Private Sub Workbook_Open()
Call runMe
End Sub
3.After the macro is completed, close the workbook programatically.
Related
I have an Excel macro-enabled workbook that is created from a template. Although the template contains no sensitive data, once the user opens the workbook and is authenticated, some sensitive data is added to the workbook from a remote server. I need to prevent the workbook from being saved anywhere (the vba code communicates with a server to save the needed data). I can write code on the "before save" event to prevent the workbook from being saved to the local file system, but if an Office 365 user turns on "autosave" a copy of the workbook will be saved to OneDrive and I can't find a way to prevent this. By periodically checking the workbook's path, I can notice that the file has been saved to OneDrive, and delete the sensitive data but by then it's too late because the originally saved file will be available in the file's version history.
Any help and suggestions are appreciated.
In response to a question by Cyril below, here is the code on the workbook's before_save event:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
autoSaveOff ' a sub procedure to turn off autosave
Cancel = True ' prevents Excel from saving
frmMessage.lbllMessage.Caption = "Attempting to save to the Server."
frmMessage.Show False
message_to_addin "save" ' this code cooperates with an Office.js addin to interact with the sercver
Application.OnTime DateAdd("s", 1, Now), "read_message_from_addin"
End Sub
Currently, I export data daily from software to excel files.
There's a lot of repetitive tasks so I created a macro.
I open the new exported excel file and then save it as "Macro-Enabled worksheet"
I open the Macro-enabled worksheet
I import the macros into the excel file
I run the macros
Is there a way to run the macro without doing all the steps above using VBS or any other way?
I don't know if there's a solution out there, but I would prefer if an external VBA operator would ask for the location of the exported file and then does the rest
You can easily open any other workbook and run any commands on that workbook. So you can have the following macro in an Excel file MyMacroFile.xlsm and manipulate data in C:\Temp\WorkbookToRunMacroOn.xlsx for example.
Option Explicit
Public Sub DoTasksOnOtherWorkbook()
'open another workbook
Dim OpenWorkbook As Workbook
Set OpenWorkbook = Application.Workbooks.Open(Filename:="C:\Temp\WorkbookToRunMacroOn.xlsx")
OpenWorkbook.Worksheets("Sheet1").Range("A1").Value = "Changed A1 in another workbook"
'don't forget to close the workbook and save or not
OpenWorkbook.Close SaveChanges:=True
End Sub
If you want to ask the user to select a file to open you can use the Application.FileDialog property it returns a file name that you can then use in the Application.Workbooks.Open to open it.
When I open macro enabled excel file, the macro becomes disabled in protected workbook (VBA code does not run). But when protection is removed from the workbook, the macro file becomes enabled and the VBA code runs fine.
Is there is any way to protect the workbook and also to run VBA code in macro file simultaneously.
Sub test()
ActiveWorkbook.Unprotect ("mypassward")
' my vba code
ActiveWorkbook.Protect ("mypassward")
End Sub
I have a Worksheet that requires a macro to run every day at 10AM. The code only works when I have a specific worksheet opened, but doesn’t when another worksheet is open. Can somebody please help me?
Code in Module:
Sub SendC()
Application.OnTime TimeValue("10:00:00"), "SendC"
**Rest of my code **
End Sub
Code in ThisWorkbook:
Private Sub Workbook_Open()
Application.OnTime TimeValue("10:00:00"), "SendC"
End Sub
VBA code lives embedded in a host document - in the case of Excel, a macro-enabled Workbook file.
By closing the workbook that contains the code, you terminate whatever VBA code was running in that workbook.
You can make VBA code run at application-level, by making an Excel add-in: the code still lives embedded in a host document, but that host document is loaded along with Excel, and remains loaded as long as that Excel instance is running.
Save your VBA project as an Excel add-in, then close everything, bring up Excel again and load the add-in: Workbook_Open will run once as the add-in is loaded, and then your SendC macro will be invoked at 10AM for as long as you leave Excel running.
I have the following peace of code to exec a xlam file when I open the excel file:
Sub Auto_Open()
Application.OnTime Now + TimeValue("00:00:05"), "readCsv"
End Sub
Sub readCsv()
....
End Sub
I add it as Add-ins so every file I open has the xlam file on it. If there is no other excel file opened, it works perfectly but after that without closing the excel file I open a second one it does nothing. I need to exec the code even if there is another excel file opened. Is that possible?
I also try to do it writing the code in ThisWorkbook but the result is the same, If there is another excel file opened it does nothing.
To use your macro in any of the worksheet opened (irrespective of name) you can only possible do it by using a personal workbook Here is the Link
If the above doesnt suits you can prepare Add-ins and install it for users (Google for it if this is the case)
EDIT:
How to get add in in all opened files....
Goto File - Option - Quick Access Toolboar - From the dropdown "Choose Command from" - select Macros then select add-in macro - Add it, Below there would be a modify button Select the icon you like from it - OK
Now you will be able to see the icon with the addin function linked on top of the excel - click it for functioning, it will remain there forever ( if missed you can reapply the settings)....
The procedure are for 2010, for 2007 it should be similar....for 2003 there is a different way to achieve it...
Copy the .xlam to C:\Users[user]\AppData\Roaming\Microsoft\Excel\XLSTART. It will load every time you open Excel.