I have few shared mailbox in outlook. I created VBA in excel to extract out the email subject. But I have to click "Get Outlook" button in excel follow by click selected Inbox one by one. Is there any way that I able to schedule auto extract every each inbox instead of clicking it manually one by one?
Create a macro enabled workbook unless you have a suitable one available.
From the VBA Editor, click [Tools] then [References]. Check that "Microsoft Outlook nn.n Object Library" is listed near the top and ticked. If listed but not ticked, click the box to tick it. If not listed near the top, scroll down (the list is in alphabetic order) until you find this library and tick it.
Copy the code below to ThisWorkbook. Save the workbook and close it.
Open the workbook by clicking it or via Windows Scheduler. A list of the accessible stores is displayed as a demonstration. Click [OK] to exit Excel or [Cancel] to terminate the macro so you can work on the code. Warning 1: if you click [OK] all open workbooks will be closed without saving any changes. Warning 2: If you do not have a Cancel or equivalent option so the macro closes the workbook automatically, you will never be able to amend the macro.
Note: this code is a demonstration of using the Workbook Open event to run a macro and a demonstration of accessing Outlook from Excel. However, it does nothing with Outlook except list the accessible stores. If you need a further demonstration, I will try to help.
Option Explicit
Sub Workbook_Open()
' Needs reference to "Microsoft Outlook nn.n Object Library" where
' nn.n depends on the version of Outlook being used.
Dim Answer As Long
Dim Dspl As String
Dim InxS As Long
Dim OutApp As New Outlook.Application
Dim OutNs As Outlook.Namespace
Set OutNs = OutApp.Session
' Build list of all accessible stores
With OutNs
Dspl = .Folders(1).Name
For InxS = 2 To .Folders.Count
Dspl = Dspl & vbLf & .Folders(InxS).Name
Next
End With
Answer = MsgBox(Dspl, vbOKCancel)
If Answer = vbCancel Then
Exit Sub
End If
Application.DisplayAlerts = False
Application.Quit
End Sub
Related
On outlook application I created a new folder with name “Template”.
Inside that folder, I manually put some emails, with inside it my excel macro workbooks.
Macro settings on Excel are Disable all macros with notification, as a result when I open any macro workbook, I got a security warning that macros have been disabled, and I need to click Enable Content each time.
I could not find any way to add these files to a trusted location or even make as trusted document.
So, as a work around, I need to change excel macro settings (temporarily) from outlook when opening any email on that folder “Template” to Enable all macros and after close this email or sent it, then revert macro settings again to “Disable all macros with notification”.
Note 1, Sure, I mean change these macro settings # my machine only and not at the recipient PC.
Note 2, I already using some macros globally inside outlook (auto zoom), and signed these macros by creating a Digital Signature using SelfCert.exe، therefore I do not get any security warning when using that macros.
Note 3, I found this question Link, But I could not adapt the provided answer to my needs ,and I use office 2016 32Bit on windows 10 64Bit.
In advance grateful for useful comments and answer.
Public WithEvents myItem As Outlook.MailItem
Public EventsDisable as Boolean
Private Sub Application_ItemLoad(ByVal Item As Object)
If EventsDisable = True Then Exit Sub
If Item.Class = olMail Then
Set myItem = Item
End If
End Sub
Private Sub myItem_Open(Cancel As Boolean)
EventsDisable=True
'Your code
EventsDisable=False
End Sub
You'd be better off creating a VSTO COM addin in VB.Net - it will be able to work without prompts.
I have VBA code in Excel that calls a Word file in a specified local folder.
For some users it turns the following error:
The macros in this project are disabled. Please refer to the online help or documentation of the host application to determine how to enable macros
The error appears as the code is supposed to move from Excel to the Word file.
What I've tried so far:
Excel Trust Center:
The entire location (including subfolders are trusted).
"Allow documents on a network to be trusted" is checked.
Disable all macros with notification is "checked".
I cannot change this as it is greyed out. However, this setting is the same for all users.
Protected view is disabled.
Word Trust Center
Protected view is disabled.
Opening the Word file doesn't produce any "enable macros" notifications.
The normal way to grey that out on purpose (maybe your users' IT sets this through gpo?) is a registry key, where 16.0 is the version you have installed
[HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Excel\Security]
"VBAWarnings"=dword:00000001
Some other things to troubleshoot:
Can they create and run their own macro?
Can they run a different macro in a different document?
Does anyone have a different version of Excel?
Are the excel and word file both local to the user PC?
Do the users have Developer settings enabled?
Does anyone have different settings in Macro Settings > Developer Macro Settings?
Is Windows blocking the excel file? Right-click the file > properties > General:
There's also this specific GPO that only blocks macros from the loosely-defined "Internet"
I had a similar issue some time ago. When opening a word document from within an Excel macro, everything worked fine for me. But on another PC, the macro simply stopped with a message indication that macros where disabled.
The issue could be solved by changing the Application.AutomationSecurity property for the word app to msoAutomationSecurityLow.
It is important to set that property back to its original value after code execution.
You can try the following code example.
Option Explicit
Sub OpenWordsFilePathWithLowSecuritySettings()
Dim sFilePath As String
Dim wrdApp As Object
Dim wrdDoc As Object
Dim lAutomationSetting As Long
'The path to your word file
sFilePath = "C:\Users\micha\Desktop\example file.docx"
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
'Save word app automation security so we can restore it afterwards
lAutomationSetting = wrdApp.AutomationSecurity
'Error handling to make sure the automation security is reset even if an error occurs
On Error GoTo ErrorHandler
'Change the automation setting to low security
wrdApp.AutomationSecurity = msoAutomationSecurityLow
'Open word document
Set wrdDoc = wrdApp.Documents.Open(sFilePath)
'Your code - do something with the word file
'
'
'
ErrorExit:
On Error Resume Next
'Close the word document
wrdDoc.Close
'Reset the word automation security
wrdApp.AutomationSecurity = lAutomationSetting
wrdApp.Quit
Set wrdDoc = Nothing
Set wrdApp = Nothing
Exit Sub
ErrorHandler:
MsgBox "An error occured: (Code: " & Err.Number & ", Description: " & Err.Description & ")", vbCritical, "Error"
Resume ErrorExit
End Sub
I have encountered a fun (I think) problem.
Let's say I have an .xlam Excel Add-In file which contains a macro
(any macro), even let it be Msgbox "Hello World!".
Let's say that I run this Add-In on new Excel Workbooks each time
(received from outside).
I would like the Add-In to insert a hyperlink into the Excel Workbook that would be opened.
I would also like that the inserted hyperlink would call the macro in the Add-In.
Now what I know/what I've tried:
1) If I was opening the same Excel Workbook each time I could just add the 'Follow Hyperlink' event: Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink) into the Worksheet where I would like my hyperlink to be ran,
however,
Since I open a new file each time it's not going to have this piece of code in the Worksheet so the 'Follow Hyperlink' even won't work.
2) Now to solve the problem in point "1" I know that I could probably create another macro, which would insert the Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink) code into the newly opened Excel Workbook i.e. a VBA code which writes a VBA code, however I don't think that's a very elegant way of solving this problem.
To sum up: I would like the (macro book) to insert a hyperlink into another workbook, and when that hyperlink would be clicked I would like it to run a macro from the 'macro book' WITHOUT using the Worksheet_FollowHyperlink event.
Any ideas? Thanks!
Try using the next simple way of accomplishing what you need, in fact:
Private Sub AddSheetEventCallProc()
'It needs a reference to 'Microsoft Visual Basic for Applications Extensibility x.x'
Dim wb As Workbook, wProj As VBIDE.VBProject, wCom As VBIDE.VBComponent
Dim wMod As VBIDE.CodeModule, Hrng As Range, LineI As Long
Set wb = Workbooks.Add 'any existing workbook can be used here
Set Hrng = wb.Worksheets(1).Range("A1") 'use here the cell you need
'Add the hyperlink:
Hrng.Hyperlinks.Add Anchor:=Hrng, _
Address:="", SubAddress:=Hrng.Address, TextToDisplay:="Call Macro"
With wb
Set wProj = .VBProject
Set wCom = wProj.VBComponents(Worksheets(1).codename)
Set wMod = wCom.CodeModule
With wMod
On Error Resume Next
LineI = .CreateEventProc("FollowHyperlink", "Worksheet"): LineI = LineI + 1
.InsertLines LineI, " If Target.Range.Address = """ & Hrng.Address & """ Then": LineI = LineI + 1
.InsertLines LineI, " Application.Run ""ADDINPRESA.xlam!testMsgbox""": LineI = LineI + 1
.InsertLines LineI, " End If"
On Error GoTo 0
End With
End With
End Sub
The second (more elaborate) way will be to make your add-in create a new Ribbon Tab:
Having the addin, it is good to create a new standard module naming it 'modRibbon', or similar. It is not mandatory, but it is good to separate the ribbon specific module from the one doing the add-in job. So, in this one where you must create the Subs able to be called by controls of the newly Tab to be created:
Option Explicit
Sub Test1(control As IRibbonControl)
appTest1 'use here your Sub name
End Sub
Sub Test2(control As IRibbonControl)
appTest2
End Sub
Sub Test3(control As IRibbonControl)
appTest3
End Sub
It is useful to copy this code in a Notepad window, in order to use it when the xml file will be created.
Then, create the module where the Subs called by the above ones will exist:
Sub appTest1()
MsgBox "It's coming...", , "Test1"
End Sub
Sub appTest2()
MsgBox "It's coming...", , "Test2"
End Sub
Sub appTest3()
MsgBox "It's coming...", , "Test3"
End Sub
Now, you need a, so named, OfficeRibbonEditor application. I use OfficeRibbonXEditor downloaded from here. Please, downoad 'OfficeRibbonXEditor-NETFramework.zip'. After downloading it, extract the file from archive and just use it. No installation need.
Now, run it and press Open button and browse for your add-in workbook and press OK. Select the open add-in and press 'Insert' menu. Select "Office 2010+ Custom UI Part" for Office 2010 or above. For 2007, select the other option. If you need the add-in to be compatible with both cases, both xml files will be inserted. I will show how to create a ribbon Tab only for first variant.
Double click 'customUI14.xml`, which has been created and copy the next code. Now, it is assorted with the above subs, but, in order to make it call your specific subs, use the Notepad window where you copied the Subs name:
Now, close the add-in from Excel and press Validate Button. If you made a mistake in the xml code, the line with the mistake will be highlighted. If everything OK, press 'Save' button and open your add-in. It will create a new Ribbon Tab (TestTab) having three buttons....
There are on the internet ways to find all imageMSO types. It is also possible to use your own icons. I did it some years before. I do not remember exactly how, but you need this option I can check my files archive.
For clarifying issues, a very good resource would be the Ron de Bruin site, which clarifies the way of doing such a Ribbon Tab, from more then ten years. You can find it here.
And the add-in does not 'import' the whole Ribbon. It only creates a new Tab adding it two the existing one and make it disappear when closed.
It is also good to know that when you modify the xml, all modifications done in the VBA add-in code will be lost and viceversa... That's why it is good to take care of closing the other one in the moment you modify something. If necessary, I can post a link to such an add-in done now, but, I would like to believe that it is easy to create one by yourself.
This is the way I create a new Ribbon Tab, but there are some other ways, too. When I will find some time, I will try doing it only in VBA. Practically, you have to read all subs you need to be called by your new Tab, then unzip the Excel file, create or modify the XML file in order to call your subs and rezip the file...
I know it's a general question.
I am trying to create an application/extension that reads the contents of an email before it is sent out. So basically if the email contains a 8 character numerical for example, it will prompt an alert box whether the employee is sure before it is sent out.
I have worked with some Python outlook modules for myself. But my task would for it to be able to run on most employee desktops which wouldn't have Python installed.
I am looking at tools such as Visual Studio but am unsure where I should start.
You can develop a VBA macro or Outlook COM add-in (for example, VSTO based one) for that. See Walkthrough: Creating Your First VSTO Add-In for Outlook to get started quickly.
Basically you need to handle the ItemSend event of the Application class which is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program.
For example, here is a VBA sample code:
Public WithEvents myOlApp As Outlook.Application
Public Sub Initialize_handler()
Set myOlApp = Outlook.Application
End Sub
Private Sub myOlApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim prompt As String
prompt = "Are you sure you want to send " & Item.Subject & "?"
If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
Cancel = True
End If
End Sub
Finally, you may find the Getting Started with VBA in Outlook 2010 article helpful.
Basically wondering if there's a way for me to create a VB application in Excel and have it run without a full version of MS Office. The VB application would load an Excel sheet that would import a CSV onload, then print a PDF of the sheet and close.
If you have any questions, let me know.
No. Not without converting to a standalone application.
If you had were familiar with VB6 (and had access to it; it's no longer for sale), you could create a VB6 app. that references the excel COM components (still need to be installed on each target PC).
Otherwise, build an app. using VB.NET and use Office VSTO 2010 (need to reference the Office PIAs)
How to: Target Office Applications Through Primary Interop Assemblies
Just a little conflict. In office, you code with VBA, which is different than VB. What you would need to do is create a VB app that uses excel libraries or something to do some meaningful work.
The short answer is no.
You could write an external visual basic script that calls in to office and opens excel using some excel libraries, if memory serves me correctly however - you'd still require office installed on this machine. (Unfortunately I can't find a link at the moment to back this up)
Your best bet is to parse the CSV data yourself and generate a PDF from that.
There is some information here: http://www.rlmueller.net/ReadCSV.htm on how to Read CSV data using VBS (to get the examples to run, you should simply have to rename the .txt to .vbs and double click it.)
I'll leave you to find out how you'd then generate the PDF.
I don't think however this is the best solution to your problem - a full .NET application or perhaps some Python would likely serve you better.
Code will go in several different places, "ThisWorkbook" object and the "UserForm" code.
"ThisWorkbook" contains code that will determine if the UserForm is the only Excel thing (workbook) open, and if it is it will hide the Excel application and hide the workbook itself. and if other workbooks are open it simply hides the workbook. I have it set to hide the application and the workbook in both cases so that a new instance of Excel can be opened after the UserForm is running without pulling up the workbook associated with the UserForm. The code for this is below (goes into the "ThisWorkbook" object):
Private Sub WorkBook_Open()
Dim wb As Workbook
Set wb = Workbooks("MyBook.xlsm")
If Workbooks.Count > 1 Then
wb.Windows(1).Visible = False
Else
wb.Windows(1).Visible = False
Application.Visible = False
End If
UserForm1.Show vbModeless
'Put defaults and populations here
End Sub
The UserForm1.Show vbModelessallows for Excel to be used while the UserForm is active.
A couple of notes on this section:
"UserForm1" is the name of my UserForm, change this to the name of yours
Where I Set wb = Workbooks("") change inside the quotes to the name of the
workbook the UserForm is in
The IfElse statement could be eliminated and moved to the If, if you don't need any other action on the opening with no other workbooks open
The next section of code goes in the UserForm Code. I have a button set up to show the Excel workbook in order to edit it and whatnot, you could have a region you click if you don't want a button to show up. When you want to activate the Excel sheet and the application will need to be activated. I unload (deactivate) the active thing (the UserForm). This bit of code isn't necessary if the user doesn't need access to the spreadsheet:
Private Sub See_Excel_Click()
Dim wb As Workbook
Set wb = Workbooks("MyBook.xlsm")
wb.Windows(1).Visible = True
Application.Visible = True
wb.Sheets("Sheet1").Activate
Unload Me
End Sub
Within the userform there should be a way to handle what happens when the userform is closed, as the excel application and workbook will stay open hidden in the background. I have the action close the workbook and the application. A quick note, if you set the Cancel = True then the red x button won't close the userform. The code I use for this is:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then
Cancel = False
Dim wb As Workbook
Set wb = Workbooks("MyBook.xlsm")
wb.Windows(1).Visible = True
Application.Visible = True
ThisWorkbook.Saved = True
ThisWorkbook.Activate
If Workbooks.Count > 1 Then
ActiveWorkbook.Close
Else
Application.Quit
End If
End If
End Sub
That is it for the code that goes inside the UserForm. And the code that is necessary to have the UserForm in VBA act as it's own application while allowing for Excel to operate normally at the same time as the UserForm.
To summarize what happens:
When the Workbook is launched the workbook is hidden, and if no other workbook is open the the Excel application is hidden.
The UserForm is initiated to allow for Excel to be used at the same time
When the spreadsheet is activated again excel is re-enabled and the application and un-hide the worksheet
When the user form is closed, the workbook is closed, and if there are no other workbooks the excel application is closed
If you set defaults or populate ComboBoxes put them in the "WorkBook" object code.