VBA Outlook Macro - get mail info in excel
I'm trying to extract information from unread e-mails in a specific folder and paste it in a structured excel file. I need the subject, the body, the sender, received time, conversation ID and the attachment names. I have no experience with VBA macros so please help :). Important: it has to be an outlook macro and not an excel macro.
I have the following code but it is giving me error
"runtime error '9': subscript out of range.
Sub WriteTextFile()
Dim wkb As Workbook
Set wkb = Workbooks.Open("C:UsersbebxadvypatDesktopTest VBA Macros.xlsx")
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set Workbook = objExcel.Workbooks.Open("C:UsersbebxadvypatDesktopTest VBA Macros.xlsx")
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim row As Integer
row = 2
objExcel.Worksheets("Sheet1").Cells(1, 1).Value = "Sender"
objExcel.Worksheets("Sheet1").Cells(1, 2).Value = "Subject"
objExcel.Worksheets("Sheet1").Cells(1, 3).Value = "Date"
objExcel.Worksheets("Sheet1").Cells(1, 4).Value = "ID"
objExcel.Worksheets("Sheet1").Cells(1, 5).Value = "Body"
Set objNS = GetNamespace("MAPI")
Set objFolder = objNS.Folders.GetLast
Set objFolder = objFolder.Folders("Deleted Items")
For Each item In objFolder.Items
objExcel.Worksheets("Sheet1").Cells(row, 1).Value = item.sender
objExcel.Worksheets("Sheet1").Cells(row, 2).Value = item.Subject
objExcel.Worksheets("Sheet1").Cells(row, 3).Value = item.ReceivedTime
objExcel.Worksheets("Sheet1").Cells(row, 4).Value = item.ConversationID
objExcel.Worksheets("Sheet1").Cells(row, 5).Value = item.Body
Next
Workbook.Save
Workbook.Saved = True
Workbook.Close
objExcel.Quit
Set Workbook = Nothing
Set objExcel = Nothing
End Sub
vba outlook outlook-vba
|
show 2 more comments
I'm trying to extract information from unread e-mails in a specific folder and paste it in a structured excel file. I need the subject, the body, the sender, received time, conversation ID and the attachment names. I have no experience with VBA macros so please help :). Important: it has to be an outlook macro and not an excel macro.
I have the following code but it is giving me error
"runtime error '9': subscript out of range.
Sub WriteTextFile()
Dim wkb As Workbook
Set wkb = Workbooks.Open("C:UsersbebxadvypatDesktopTest VBA Macros.xlsx")
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set Workbook = objExcel.Workbooks.Open("C:UsersbebxadvypatDesktopTest VBA Macros.xlsx")
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim row As Integer
row = 2
objExcel.Worksheets("Sheet1").Cells(1, 1).Value = "Sender"
objExcel.Worksheets("Sheet1").Cells(1, 2).Value = "Subject"
objExcel.Worksheets("Sheet1").Cells(1, 3).Value = "Date"
objExcel.Worksheets("Sheet1").Cells(1, 4).Value = "ID"
objExcel.Worksheets("Sheet1").Cells(1, 5).Value = "Body"
Set objNS = GetNamespace("MAPI")
Set objFolder = objNS.Folders.GetLast
Set objFolder = objFolder.Folders("Deleted Items")
For Each item In objFolder.Items
objExcel.Worksheets("Sheet1").Cells(row, 1).Value = item.sender
objExcel.Worksheets("Sheet1").Cells(row, 2).Value = item.Subject
objExcel.Worksheets("Sheet1").Cells(row, 3).Value = item.ReceivedTime
objExcel.Worksheets("Sheet1").Cells(row, 4).Value = item.ConversationID
objExcel.Worksheets("Sheet1").Cells(row, 5).Value = item.Body
Next
Workbook.Save
Workbook.Saved = True
Workbook.Close
objExcel.Quit
Set Workbook = Nothing
Set objExcel = Nothing
End Sub
vba outlook outlook-vba
1
What line throws the error?
– BigBen
Nov 22 at 15:29
There are only a few things I can see that can throw error 9 - one is the hard-coded sheet name. "Sheet1" is possibly not the actual name of the sheet you want to work with, esp. if Excel isn't installed in English. For example the default sheet name will be "Feuil1" in French. Consider pullingobjExcel.Worksheets(1)
into its ownWorksheet
variable, instead of dereferencing it every single time. The other is the hard-coded "Deleted Items" folder name. If the specified hard-coded string isn't a key in the collection you're pulling from, error 9 is thrown.
– Mathieu Guindon
Nov 22 at 16:02
Hi @MathieuGuindon, indeed the issue was related to the non-english version! thanks a lot. However: a) what line of code should I get to get the attachment names as well? b) how can I download the attachments to a specific folder? c) to actually only do all of this for unread e-mails
– Yanni Pattas
Nov 22 at 22:07
Hit F2 to bring up the Object Browser, then find theMailItem
class and search for something that looks like a collection of attachments. Then look for a class that could conceivably be an attachment - that class likely has aName
property.
– Mathieu Guindon
Nov 22 at 22:11
You setobjFolder
toobjNS.Folders.GetLast
and then reset it toobjFolder.Folders("Deleted Items")
. Does the folder returned by the first statement contain folder "Deleted Items"? If it does not, that would explain the error 9. Try addingDebug.Print objFolder.Name
after eachSet
and see what it gives you.
– Tony Dallimore
Nov 22 at 22:39
|
show 2 more comments
I'm trying to extract information from unread e-mails in a specific folder and paste it in a structured excel file. I need the subject, the body, the sender, received time, conversation ID and the attachment names. I have no experience with VBA macros so please help :). Important: it has to be an outlook macro and not an excel macro.
I have the following code but it is giving me error
"runtime error '9': subscript out of range.
Sub WriteTextFile()
Dim wkb As Workbook
Set wkb = Workbooks.Open("C:UsersbebxadvypatDesktopTest VBA Macros.xlsx")
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set Workbook = objExcel.Workbooks.Open("C:UsersbebxadvypatDesktopTest VBA Macros.xlsx")
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim row As Integer
row = 2
objExcel.Worksheets("Sheet1").Cells(1, 1).Value = "Sender"
objExcel.Worksheets("Sheet1").Cells(1, 2).Value = "Subject"
objExcel.Worksheets("Sheet1").Cells(1, 3).Value = "Date"
objExcel.Worksheets("Sheet1").Cells(1, 4).Value = "ID"
objExcel.Worksheets("Sheet1").Cells(1, 5).Value = "Body"
Set objNS = GetNamespace("MAPI")
Set objFolder = objNS.Folders.GetLast
Set objFolder = objFolder.Folders("Deleted Items")
For Each item In objFolder.Items
objExcel.Worksheets("Sheet1").Cells(row, 1).Value = item.sender
objExcel.Worksheets("Sheet1").Cells(row, 2).Value = item.Subject
objExcel.Worksheets("Sheet1").Cells(row, 3).Value = item.ReceivedTime
objExcel.Worksheets("Sheet1").Cells(row, 4).Value = item.ConversationID
objExcel.Worksheets("Sheet1").Cells(row, 5).Value = item.Body
Next
Workbook.Save
Workbook.Saved = True
Workbook.Close
objExcel.Quit
Set Workbook = Nothing
Set objExcel = Nothing
End Sub
vba outlook outlook-vba
I'm trying to extract information from unread e-mails in a specific folder and paste it in a structured excel file. I need the subject, the body, the sender, received time, conversation ID and the attachment names. I have no experience with VBA macros so please help :). Important: it has to be an outlook macro and not an excel macro.
I have the following code but it is giving me error
"runtime error '9': subscript out of range.
Sub WriteTextFile()
Dim wkb As Workbook
Set wkb = Workbooks.Open("C:UsersbebxadvypatDesktopTest VBA Macros.xlsx")
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set Workbook = objExcel.Workbooks.Open("C:UsersbebxadvypatDesktopTest VBA Macros.xlsx")
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim row As Integer
row = 2
objExcel.Worksheets("Sheet1").Cells(1, 1).Value = "Sender"
objExcel.Worksheets("Sheet1").Cells(1, 2).Value = "Subject"
objExcel.Worksheets("Sheet1").Cells(1, 3).Value = "Date"
objExcel.Worksheets("Sheet1").Cells(1, 4).Value = "ID"
objExcel.Worksheets("Sheet1").Cells(1, 5).Value = "Body"
Set objNS = GetNamespace("MAPI")
Set objFolder = objNS.Folders.GetLast
Set objFolder = objFolder.Folders("Deleted Items")
For Each item In objFolder.Items
objExcel.Worksheets("Sheet1").Cells(row, 1).Value = item.sender
objExcel.Worksheets("Sheet1").Cells(row, 2).Value = item.Subject
objExcel.Worksheets("Sheet1").Cells(row, 3).Value = item.ReceivedTime
objExcel.Worksheets("Sheet1").Cells(row, 4).Value = item.ConversationID
objExcel.Worksheets("Sheet1").Cells(row, 5).Value = item.Body
Next
Workbook.Save
Workbook.Saved = True
Workbook.Close
objExcel.Quit
Set Workbook = Nothing
Set objExcel = Nothing
End Sub
vba outlook outlook-vba
vba outlook outlook-vba
edited Nov 22 at 15:29
BigBen
5,5292419
5,5292419
asked Nov 22 at 15:28
Yanni Pattas
1
1
1
What line throws the error?
– BigBen
Nov 22 at 15:29
There are only a few things I can see that can throw error 9 - one is the hard-coded sheet name. "Sheet1" is possibly not the actual name of the sheet you want to work with, esp. if Excel isn't installed in English. For example the default sheet name will be "Feuil1" in French. Consider pullingobjExcel.Worksheets(1)
into its ownWorksheet
variable, instead of dereferencing it every single time. The other is the hard-coded "Deleted Items" folder name. If the specified hard-coded string isn't a key in the collection you're pulling from, error 9 is thrown.
– Mathieu Guindon
Nov 22 at 16:02
Hi @MathieuGuindon, indeed the issue was related to the non-english version! thanks a lot. However: a) what line of code should I get to get the attachment names as well? b) how can I download the attachments to a specific folder? c) to actually only do all of this for unread e-mails
– Yanni Pattas
Nov 22 at 22:07
Hit F2 to bring up the Object Browser, then find theMailItem
class and search for something that looks like a collection of attachments. Then look for a class that could conceivably be an attachment - that class likely has aName
property.
– Mathieu Guindon
Nov 22 at 22:11
You setobjFolder
toobjNS.Folders.GetLast
and then reset it toobjFolder.Folders("Deleted Items")
. Does the folder returned by the first statement contain folder "Deleted Items"? If it does not, that would explain the error 9. Try addingDebug.Print objFolder.Name
after eachSet
and see what it gives you.
– Tony Dallimore
Nov 22 at 22:39
|
show 2 more comments
1
What line throws the error?
– BigBen
Nov 22 at 15:29
There are only a few things I can see that can throw error 9 - one is the hard-coded sheet name. "Sheet1" is possibly not the actual name of the sheet you want to work with, esp. if Excel isn't installed in English. For example the default sheet name will be "Feuil1" in French. Consider pullingobjExcel.Worksheets(1)
into its ownWorksheet
variable, instead of dereferencing it every single time. The other is the hard-coded "Deleted Items" folder name. If the specified hard-coded string isn't a key in the collection you're pulling from, error 9 is thrown.
– Mathieu Guindon
Nov 22 at 16:02
Hi @MathieuGuindon, indeed the issue was related to the non-english version! thanks a lot. However: a) what line of code should I get to get the attachment names as well? b) how can I download the attachments to a specific folder? c) to actually only do all of this for unread e-mails
– Yanni Pattas
Nov 22 at 22:07
Hit F2 to bring up the Object Browser, then find theMailItem
class and search for something that looks like a collection of attachments. Then look for a class that could conceivably be an attachment - that class likely has aName
property.
– Mathieu Guindon
Nov 22 at 22:11
You setobjFolder
toobjNS.Folders.GetLast
and then reset it toobjFolder.Folders("Deleted Items")
. Does the folder returned by the first statement contain folder "Deleted Items"? If it does not, that would explain the error 9. Try addingDebug.Print objFolder.Name
after eachSet
and see what it gives you.
– Tony Dallimore
Nov 22 at 22:39
1
1
What line throws the error?
– BigBen
Nov 22 at 15:29
What line throws the error?
– BigBen
Nov 22 at 15:29
There are only a few things I can see that can throw error 9 - one is the hard-coded sheet name. "Sheet1" is possibly not the actual name of the sheet you want to work with, esp. if Excel isn't installed in English. For example the default sheet name will be "Feuil1" in French. Consider pulling
objExcel.Worksheets(1)
into its own Worksheet
variable, instead of dereferencing it every single time. The other is the hard-coded "Deleted Items" folder name. If the specified hard-coded string isn't a key in the collection you're pulling from, error 9 is thrown.– Mathieu Guindon
Nov 22 at 16:02
There are only a few things I can see that can throw error 9 - one is the hard-coded sheet name. "Sheet1" is possibly not the actual name of the sheet you want to work with, esp. if Excel isn't installed in English. For example the default sheet name will be "Feuil1" in French. Consider pulling
objExcel.Worksheets(1)
into its own Worksheet
variable, instead of dereferencing it every single time. The other is the hard-coded "Deleted Items" folder name. If the specified hard-coded string isn't a key in the collection you're pulling from, error 9 is thrown.– Mathieu Guindon
Nov 22 at 16:02
Hi @MathieuGuindon, indeed the issue was related to the non-english version! thanks a lot. However: a) what line of code should I get to get the attachment names as well? b) how can I download the attachments to a specific folder? c) to actually only do all of this for unread e-mails
– Yanni Pattas
Nov 22 at 22:07
Hi @MathieuGuindon, indeed the issue was related to the non-english version! thanks a lot. However: a) what line of code should I get to get the attachment names as well? b) how can I download the attachments to a specific folder? c) to actually only do all of this for unread e-mails
– Yanni Pattas
Nov 22 at 22:07
Hit F2 to bring up the Object Browser, then find the
MailItem
class and search for something that looks like a collection of attachments. Then look for a class that could conceivably be an attachment - that class likely has a Name
property.– Mathieu Guindon
Nov 22 at 22:11
Hit F2 to bring up the Object Browser, then find the
MailItem
class and search for something that looks like a collection of attachments. Then look for a class that could conceivably be an attachment - that class likely has a Name
property.– Mathieu Guindon
Nov 22 at 22:11
You set
objFolder
to objNS.Folders.GetLast
and then reset it to objFolder.Folders("Deleted Items")
. Does the folder returned by the first statement contain folder "Deleted Items"? If it does not, that would explain the error 9. Try adding Debug.Print objFolder.Name
after each Set
and see what it gives you.– Tony Dallimore
Nov 22 at 22:39
You set
objFolder
to objNS.Folders.GetLast
and then reset it to objFolder.Folders("Deleted Items")
. Does the folder returned by the first statement contain folder "Deleted Items"? If it does not, that would explain the error 9. Try adding Debug.Print objFolder.Name
after each Set
and see what it gives you.– Tony Dallimore
Nov 22 at 22:39
|
show 2 more comments
1 Answer
1
active
oldest
votes
According to test your code, you could try to add row = row+1
and use this method to get Delete Items:
Set olNs = Application.GetNamespace("MAPI")
Set DeletedFolder = olNs.GetDefaultFolder(olFolderDeletedItems)
For Each Item In objFolder.Items
objExcel.Worksheets("Sheet1").Cells(row, 1).Value = Item.Sender
objExcel.Worksheets("Sheet1").Cells(row, 2).Value = Item.Subject
objExcel.Worksheets("Sheet1").Cells(row, 3).Value = Item.ReceivedTime
objExcel.Worksheets("Sheet1").Cells(row, 4).Value = Item.ConversationID
objExcel.Worksheets("Sheet1").Cells(row, 5).Value = Item.Body
row = row + 1
Next
Incrementing rows would be useful in the final product but the "subscript out of range", according to the OP's comment, was due to a non-English version not recognizing "Sheet1".
– niton
Nov 23 at 14:26
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434121%2fvba-outlook-macro-get-mail-info-in-excel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
According to test your code, you could try to add row = row+1
and use this method to get Delete Items:
Set olNs = Application.GetNamespace("MAPI")
Set DeletedFolder = olNs.GetDefaultFolder(olFolderDeletedItems)
For Each Item In objFolder.Items
objExcel.Worksheets("Sheet1").Cells(row, 1).Value = Item.Sender
objExcel.Worksheets("Sheet1").Cells(row, 2).Value = Item.Subject
objExcel.Worksheets("Sheet1").Cells(row, 3).Value = Item.ReceivedTime
objExcel.Worksheets("Sheet1").Cells(row, 4).Value = Item.ConversationID
objExcel.Worksheets("Sheet1").Cells(row, 5).Value = Item.Body
row = row + 1
Next
Incrementing rows would be useful in the final product but the "subscript out of range", according to the OP's comment, was due to a non-English version not recognizing "Sheet1".
– niton
Nov 23 at 14:26
add a comment |
According to test your code, you could try to add row = row+1
and use this method to get Delete Items:
Set olNs = Application.GetNamespace("MAPI")
Set DeletedFolder = olNs.GetDefaultFolder(olFolderDeletedItems)
For Each Item In objFolder.Items
objExcel.Worksheets("Sheet1").Cells(row, 1).Value = Item.Sender
objExcel.Worksheets("Sheet1").Cells(row, 2).Value = Item.Subject
objExcel.Worksheets("Sheet1").Cells(row, 3).Value = Item.ReceivedTime
objExcel.Worksheets("Sheet1").Cells(row, 4).Value = Item.ConversationID
objExcel.Worksheets("Sheet1").Cells(row, 5).Value = Item.Body
row = row + 1
Next
Incrementing rows would be useful in the final product but the "subscript out of range", according to the OP's comment, was due to a non-English version not recognizing "Sheet1".
– niton
Nov 23 at 14:26
add a comment |
According to test your code, you could try to add row = row+1
and use this method to get Delete Items:
Set olNs = Application.GetNamespace("MAPI")
Set DeletedFolder = olNs.GetDefaultFolder(olFolderDeletedItems)
For Each Item In objFolder.Items
objExcel.Worksheets("Sheet1").Cells(row, 1).Value = Item.Sender
objExcel.Worksheets("Sheet1").Cells(row, 2).Value = Item.Subject
objExcel.Worksheets("Sheet1").Cells(row, 3).Value = Item.ReceivedTime
objExcel.Worksheets("Sheet1").Cells(row, 4).Value = Item.ConversationID
objExcel.Worksheets("Sheet1").Cells(row, 5).Value = Item.Body
row = row + 1
Next
According to test your code, you could try to add row = row+1
and use this method to get Delete Items:
Set olNs = Application.GetNamespace("MAPI")
Set DeletedFolder = olNs.GetDefaultFolder(olFolderDeletedItems)
For Each Item In objFolder.Items
objExcel.Worksheets("Sheet1").Cells(row, 1).Value = Item.Sender
objExcel.Worksheets("Sheet1").Cells(row, 2).Value = Item.Subject
objExcel.Worksheets("Sheet1").Cells(row, 3).Value = Item.ReceivedTime
objExcel.Worksheets("Sheet1").Cells(row, 4).Value = Item.ConversationID
objExcel.Worksheets("Sheet1").Cells(row, 5).Value = Item.Body
row = row + 1
Next
edited Nov 23 at 3:15
answered Nov 23 at 3:09
Alina Li
576125
576125
Incrementing rows would be useful in the final product but the "subscript out of range", according to the OP's comment, was due to a non-English version not recognizing "Sheet1".
– niton
Nov 23 at 14:26
add a comment |
Incrementing rows would be useful in the final product but the "subscript out of range", according to the OP's comment, was due to a non-English version not recognizing "Sheet1".
– niton
Nov 23 at 14:26
Incrementing rows would be useful in the final product but the "subscript out of range", according to the OP's comment, was due to a non-English version not recognizing "Sheet1".
– niton
Nov 23 at 14:26
Incrementing rows would be useful in the final product but the "subscript out of range", according to the OP's comment, was due to a non-English version not recognizing "Sheet1".
– niton
Nov 23 at 14:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434121%2fvba-outlook-macro-get-mail-info-in-excel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What line throws the error?
– BigBen
Nov 22 at 15:29
There are only a few things I can see that can throw error 9 - one is the hard-coded sheet name. "Sheet1" is possibly not the actual name of the sheet you want to work with, esp. if Excel isn't installed in English. For example the default sheet name will be "Feuil1" in French. Consider pulling
objExcel.Worksheets(1)
into its ownWorksheet
variable, instead of dereferencing it every single time. The other is the hard-coded "Deleted Items" folder name. If the specified hard-coded string isn't a key in the collection you're pulling from, error 9 is thrown.– Mathieu Guindon
Nov 22 at 16:02
Hi @MathieuGuindon, indeed the issue was related to the non-english version! thanks a lot. However: a) what line of code should I get to get the attachment names as well? b) how can I download the attachments to a specific folder? c) to actually only do all of this for unread e-mails
– Yanni Pattas
Nov 22 at 22:07
Hit F2 to bring up the Object Browser, then find the
MailItem
class and search for something that looks like a collection of attachments. Then look for a class that could conceivably be an attachment - that class likely has aName
property.– Mathieu Guindon
Nov 22 at 22:11
You set
objFolder
toobjNS.Folders.GetLast
and then reset it toobjFolder.Folders("Deleted Items")
. Does the folder returned by the first statement contain folder "Deleted Items"? If it does not, that would explain the error 9. Try addingDebug.Print objFolder.Name
after eachSet
and see what it gives you.– Tony Dallimore
Nov 22 at 22:39