I hope you can help me. I am new to Outlook 2010 VBA, but need a macro to :-
Save a group of highlighted e-mails :-
a) As .msg files;
b) In a given folder;
c) Where the name of each is the attachment name.
To give you an example, say there are 20 e-mails in my sent items folder, I want to highlight 10 of them and run this macro, winding-up with 10 flat files in a given folder which bear the name of the attachment for each e-mail.
Every e-mail subjected to this macro will have just one attachment, so to be crystal-clear, if we have an e-mail with a subject line "Random Text goes here doesn't matter what", and an attachment "GO.XLSX", I want the extracted file to be called GO.msg and, to confirm, this is Outlook 2010 I am running.
I have looked through loads of VBA sites and macro snippets, but I am getting nowhere =[
Not sure if I understand your problem, but here is a quick example:
Sub QuickExample()
For Each Item In Application.ActiveExplorer.Selection
If Item.Attachments.Count > 0 Then
Debug.Print Item.Attachments(1).DisplayName
End If
Next
End Sub
Related
I need some help with VBA to count emails from 7 different subfolders of Outlook.
The result must show the number of emails in these subfolders and the date of the last email.
This subfolder is added as an extension to my Outlook only for processing data and is not my actual Outlook email. It has further subfolders inside of it which needs to be counted.
I hope someone can help me with this.
Thanks in advance
It seems you need to iterate over all folder recursively because the nesting level is unknown. The following code iterates over all folders in Outlook recursively:
sub ProcessFolders(Folders)
for each folder in Folders
if Folder.DefaultItemType = olMailItem Then
Debug.Print "--------- " & folder.Name
End If
ProcessFolders(folder.Folders)
next
end sub
Here is how you could invoke it:
ProcessFolders(Application.Session.Folders)
The result must show the number of emails in these subfolders and the date of the last email.
The Items.Count property returns a long indicating the count of objects in the specified collection of folder. For example:
folderInstance.Items.Count
To find the date of last email (I suppose the last received one) you need to sort the collection using the Items.Sort method which orts the collection of items by the specified property. The index for the collection is reset to 1 upon completion of this method.
Set myItems = myFolder.Items
myItems.Sort "[ReceivedTime]", True
MsgBox myItems(1).ReceivedTime
I have an Excel macro that performs a few functions on a document (Creates as form, and a few emails) all from MS-Word documents. If the macro is executed from the main spreadsheet (where the macro is), everything works normally. I want to place this macro on the ribbon allowing a user to launch it without having (or knowing where the main excel document is located or having it open). I created a sub to check to see if the spreadsheet was open and modified the ribbon to include an icon for the macro.
Which works. However, when launched from the ribbon while the main Excel spreadsheet is not open, it opens the workbook and runs the macro in entirety (Without executing the open workbook line of the macro). I assume the spreadsheet is being open because the macro that is being called resides with it (makes sense). Since the macro is dependent on the data contains in the spreadsheet, I need to allow the users to modify it and then re-running the macros from the ribbon again.
Does anyone have a recommended approach or best practices? Thank you in advance.
Sub MainForm()
Dim WorkingFolder As String
Dim File01 As String 'Main Excel Data File, where all data is
Dim File02 As String 'Preliminary Email to send to user
Dim File03 As String 'Final Email to Send to user when production is complete
Dim wb As Workbook
WorkingFolder = "C:\Temp\"
File01 = "01-MainData.xlsm"
File02 = "02-PreProductionEmail.docx"
File03 = "03-FinalProductionEmail.docx"
If wbIsOpen(File01) = True Then
MsgBox "Workbook Is Open"
Run ("'C:\Users\Guest\Nextcloud\Documents\Excel Forms\02-TEST-Production Request-Data.xlsm'!CreateProductionForm")
Else
MsgBox "The Main Datafile is not open, verify the last row before re-runing", vbOKOnly, "Not Open"
Set wk = Workbooks.Open(WorkingFolder & DataFile)
End If
End Sub
The Guide mentioned by Ricardo was not quite what I needed but it was helpful and did put me on the right track. Thank you again Ricardo. To get this working, I needed to do the following.
1 - Make the procedure above its own separate *.xlam file (saved as an add-in).
2 - Add the add-in to start automatically via the developers tab
3 - Add the Macro to the Ribbon.
I have modified the sample above to included the open statement along with the statement that executes a macros from another workbook.
Appreciate the guidance.
I just wrote a simple macro where some user can enter some data, and then the macro replaces this input data into bookmarks of a Microsoft Word document, and then the "Save As" window is opened. As the file is saved, a log is created in another worksheet.
It works perfects so far, but I'd like to know if there is a way to store the path of the just-saved file?
I'm asking this because I'd like to add a new column called "file", for example, which contains a hyperlink to just-saved file. (I dont know the path apriori, because users can choose the path they want)
You'll need Application.GetSaveAsFilename which returns the path of the saved file, and returns False if the user cancels it.
varResult = Application.GetSaveAsFilename
'checks to make sure the user hasn't canceled the dialog
If varResult <> False Then
'Do something
End If
I wrote a VBA script to send emails to an arbitrary number of contacts from an excel file. The excel file basically has a column of email address's and attachment's, where attachment's is the name of the file to attach to the email. What I want to do is be able to add multiple attachments, by separating each attachment by ; in the attachments column and making the script go on to add the next attachment. The trouble I am having is I don't know how to do it without setting a fixed number of attachments for contacts. The scenario I am trying to capture is, one contact can have 3 attachments, another one could have 2 and another 0 attachments.
You can split a text in a cell to an array, then just loop through the array.
Const DELIMITER = ";"
Dim strCellText as String, strAttachment as String
Dim strAttachments() As String
strCellText = 'load your cell text here
strAttachments = Split(strCellText, DELIMITER)
For Each strAttachment In strAttachments
'attach an attachment to a mail
Next
I use Outlook (MS Exchange) and have an individual as well as two group inboxes (I'm working logged in with the individual profile through which I also have access to the group inboxes).
When I send an email, I chose either my individual or one of the two group email addresses in the From field. When the email is sent, I want a copy saved in the inbox of myIndividualMailbox, groupAMailbox, or groupBMailbox depending on which From email address I used.
Example: If I send an email From groupA#myCompany.com, I want a copy of the email saved in the inbox of the groupAMailbox (and not in my individual inbox).
I have understood that this is not possible by setting up a rule in Outlook but that it could be done with a VBA macro. I don't now how to write the VBA macro and don't know if this is a just a short script or more complicated. In fact I have never written a macro in Outlook so I don't even know how to begin. Can anyone show how to do this?
I started looking for a solution with this question: Outlook send-rule that filter on the 'From' field
I made this for you as far as I can tell, it works. You should put this in the Microsoft Outlook Objects - ThisOutlookSession Module.
Note that the myolApp_ItemSend event will never trigger unless you run enableEvents first. And you will need to make sure it is enabled every time you close an re-open Outlook. This will take some customization, but it should give you the general idea.
Option Explicit
Public WithEvents myolApp As Outlook.Application
Sub enableEvents()
Set myolApp = Outlook.Application
End Sub
Private Sub myolApp_ItemSend(ByVal item As Object, Cancel As Boolean)
Dim items As MailItem
Dim copyFolder As Outlook.Folder
Dim sentWith As String
'Identify sender address
If item.Sender Is Nothing Then
sentWith = item.SendUsingAccount.SmtpAddress
Else
sentWith = item.Sender.Address
End If
'Determin copy folder based on sendAddress
Select Case sentWith
Case "groupA#myCompany.com"
'get groupAMailbox's inbox
Set copyFolder = Application.GetNamespace("MAPI").folders("groupAMailbox").folders("Inbox")
Case "myE-mailAddress"
'get My inbox
Set copyFolder = Application.GetNamespace("MAPI").folders("myE-mailAddress").folders("Inbox")
End Select
'copy the Item
Dim copy As Object
Set copy = item.copy
'move copy to folder
copy.Move copyFolder
End Sub
EDIT: It looks like they've actually built the event functionality into the Application object for Outlook directly now, but it from testing you still have to do what I outlined above.
Outlook stores all sent items in default sent items folders. however you can apply a patch to save sent items in its own folder.
http://support.microsoft.com/kb/2181579