bneigher
bneigher

Reputation: 848

VBA Script hard code email address

(update) - The way this script currently runs is when you are in an active email folder (...@.com/inbox) or (...@.com/deleted), it runs the script on that email folder. So it outlook automatically knows which email if selected. I want it to do [email protected] always. And this is being run through Outlook's VBA

So I have this neat VBA script that exports my inbox folder from a selected email address in outlook to Microsoft Excel. The thing I would like is to automate this even further so that the email address is hard coded into the script. Basically I don't want to have to select an email address to run the script, I just want the script to run on one specific email address always. Here is my code:

Sub Extract()


On Error Resume Next
Set myOlApp = Outlook.Application
Set myfolder = myOlApp.ActiveExplorer.CurrentFolder


Set xlobj = CreateObject("excel.application.14")
Set xlobjWbk = xlobj.Workbooks.Open("c:\Users\(my username)\Desktop\Example.xlsx")
xlobj.Visible = True
xlobj.EnableEvents = True

'Set Heading
xlobj.Range("a" & 1).Value = "Recieved Time"
xlobj.Range("b" & 1).Value = "Sender Email"
xlobj.Range("c" & 1).Value = "Subject"
xlobj.Range("d" & 1).Value = "Sender Name"
xlobj.Range("e" & 1).Value = "Body"

For i = 1 To myfolder.Items.Count
 Set myItem = myfolder.Items(i)
 msgtext = myItem.body

 xlobj.Range("a" & i + 1).Value = myItem.ReceivedTime
 xlobj.Range("b" & i + 1).Value = myItem.SenderEmailAddress
 xlobj.Range("C" & i + 1).Value = myItem.Subject
 xlobj.Range("d" & i + 1).Value = myItem.SenderName
 xlobj.Range("e" & i + 1).Value = msgtext

 Next
End Sub

Upvotes: 0

Views: 513

Answers (1)

Tim van Zonneveld
Tim van Zonneveld

Reputation: 518

As far as i can see from you example i take it that you run your routine against your active outlook instance and selected folder (myOlApp.ActiveExplorer.CurrentFolder).

Outlookfolders have id attributes. You could use that to meet your goal. You can get it with -> ActiveExplorer.CurrentFolder.EntryID. Then use it by -> Set targetFolder = GetNamespace("MAPI").GetFolderFromID(folderID)

Upvotes: 1

Related Questions