Input box for search subject

josros60

Well-known Member
Joined
Jun 27, 2010
Messages
534
Hi,

can the code below can be modify for input box to ask for subject to search instead of stating subject in code:

VBA Code:
[IMG alt="Question"]https://i1.social.s-msft.com/globalresources/Images/trans.gif?cver=0001[/IMG]
[URL='https://social.msdn.microsoft.com/Forums/Account/Login?ReturnUrl=https%3a%2f%2fsocial.msdn.microsoft.com%3a443%2fForums%2foffice%2fen-US%2f12779a03-4a25-4768-8266-82116ea8eae4%2fsearch-outlook-email-with-excel-vba%3fforum%3doutlookdev%26prof%3drequired'][IMG alt="Sign in to vote"]https://i1.social.s-msft.com/globalresources/Images/trans.gif?cver=0001[/IMG]
1
Sign in to vote[/URL]
A big thank you to Ken, Damian and Dmitry for helping me sort out this matter. For future reference I am posting the code for copying the contents of an Outlook email into an Excel sheet based on email subject (Macro controlled from Excel)
Sub Work_with_Outlook()
 Set olApp = CreateObject("Outlook.Application")

Dim olNs As Outlook.Namespace
Dim Fldr As Outlook.MAPIFolder
Dim olMail As Variant
Dim sir() As String

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
    Set myTasks = Fldr.Items
 [B][COLOR=rgb(226, 80, 65)] Set olMail = myTasks.Find("[Subject] = ""*desired subject*""")[/COLOR][/B]
If Not (olMail Is Nothing) Then
sir = Split(olMail.Body, vbCrLf)
For i = 1 To UBound(sir)
ActiveWorkbook.Sheets("Sheet1").Cells(i, 1).Value = sir(i)
Next i
olMail.Delete
  End If
End Sub

thank you
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
just add a variable to your declarations then use that instantiate that variable with the inputbox value and use that in the Find statement.
VBA Code:
Dim srch As String
srch = InputBox("Enter search string", "ITEM TO SEARCH')
  Set olMail = myTasks.Find(srch, , xlValues)
 

Watch MrExcel Video

Forum statistics

Threads
1,130,214
Messages
5,640,910
Members
417,178
Latest member
HelpMeExcelExperts

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top