Discussion:
VBScript to get most recent subject headings from specific (not inbox) folder
(too old to reply)
Dan Campbell
2020-05-28 00:25:18 UTC
Permalink
Hi,

I'm confused about how to get emails from a specific folder. Almost all of the examples on the web, are assuming that you're getting data from either inbox, or a subfolder of inbox.

The folder I'm trying to obtain the subject headings from, is called

ThisSpecificFolder


, in this example. There are several other folders, also on the same parent level as inbox. But I'm interested only in this specific folder.



Dim objOutlook
Dim objNamespace
Dim colFolders
Dim objFldr
Dim objItms


Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set colFolders = objNamespace.Folders


'// This doesn't seem to be accomplishing it,
'// but I'm not sure what to do next.

Set objFldr = objNamespace.Folders("ThisSpecificFolder")
Set objItms = objFldr.Items
Evertjan.
2020-05-28 10:48:55 UTC
Permalink
Post by Dan Campbell
Hi,
I'm confused about how to get emails from a specific folder. Almost all
of the examples on the web, are assuming that you're getting data from
either inbox, or a subfolder of inbox.
The folder I'm trying to obtain the subject headings from, is called
ThisSpecificFolder
, in this example. There are several other folders, also on the same
parent level as inbox. But I'm interested only in this specific folder.
Dim objOutlook
Dim objNamespace
Dim colFolders
Dim objFldr
Dim objItms
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set colFolders = objNamespace.Folders
'// This doesn't seem to be accomplishing it,
'// but I'm not sure what to do next.
Set objFldr = objNamespace.Folders("ThisSpecificFolder")
Set objItms = objFldr.Items
Methinks this is ment to be VBA, not VBS,
and this is a VBS-NG.

Look here:

<https://docs.microsoft.com/en-us/office/vba/outlook/concepts/getting-
started/automating-outlook-from-a-visual-basic-application>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mayayana
2020-05-28 12:13:49 UTC
Permalink
"Evertjan." <***@inter.nl.net> wrote

| Methinks this is ment to be VBA, not VBS,
| and this is a VBS-NG.
|

I just sent him here from the VB6 group. :)
The code looks like perfectly fine VBS to me.
But I don't have Outlook, so I can't test it.

It probably *could* be used as VBA, but it's
written as Dispatch/late-bound code, so it should
be fine as VBS.

There is an Outlook VBA newsgroup but I just took
a look and it's only getting about 1 post per year.
No one left but us old men.
Dan Campbell
2020-05-28 13:08:09 UTC
Permalink
Dan Campbell dc wrote on 28 May 2020 in
Post by Dan Campbell
Hi,
I'm confused about how to get emails from a specific folder. Almost all
of the examples on the web, are assuming that you're getting data from
either inbox, or a subfolder of inbox.
The folder I'm trying to obtain the subject headings from, is called
ThisSpecificFolder
, in this example. There are several other folders, also on the same
parent level as inbox. But I'm interested only in this specific folder.
Dim objOutlook
Dim objNamespace
Dim colFolders
Dim objFldr
Dim objItms
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set colFolders = objNamespace.Folders
'// This doesn't seem to be accomplishing it,
'// but I'm not sure what to do next.
Set objFldr = objNamespace.Folders("ThisSpecificFolder")
Set objItms = objFldr.Items
Methinks this is ment to be VBA, not VBS,
and this is a VBS-NG.
<https://docs.microsoft.com/en-us/office/vba/outlook/concepts/getting-
started/automating-outlook-from-a-visual-basic-application>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Hi Everjian,

No, I'm using VBScript. The editor I'm using is VBSedit. That's for VBScript, not VBA.

Anyway, thanks for the link, but I've been to that page. It isn't helpful, because some of the objects it references are defined.

Anyway, I posted the question in an official VBScript forum. We'll see what they say.
Evertjan.
2020-05-28 16:07:48 UTC
Permalink
Post by Dan Campbell
Post by Evertjan.
<https://docs.microsoft.com/en-us/office/vba/outlook/concepts/getting-
started/automating-outlook-from-a-visual-basic-application>
Hi Everjian,
No, I'm using VBScript. The editor I'm using is VBSedit. That's for VBScript, not VBA.
Anyway, thanks for the link, but I've been to that page. It isn't
helpful, because some of the objects it references are defined.
Anyway, I posted the question in an official VBScript forum. We'll see what they say.
I never use Outlook, probably only for legacy reasons,
but I read somewhere that you need Outlook to be running.

==================

<https://stackoverflow.com/questions/9391849/opening-outlook-2010-through-
vbscript>

<https://support.foxtrotalliance.com/hc/en-us/articles/360021711752-How-To-
Automate-Outlook-With-VBScript>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
JJ
2020-05-28 13:14:10 UTC
Permalink
Post by Dan Campbell
Set objFldr = objNamespace.Folders("ThisSpecificFolder")
If that code can't be used to get the needed folder, you may want to do it
like this.

Set colFolders = objNamespace.Folders
For Each aFolder in colFolders
If aFolder.Name = "ThisSpecificFolder" Then
'process the folder here...
End If
Next

If the folder isn't found, chances are that you're using the incorrect
folder name. In this case, you can simply call `MsgBox()` to display the
folder's `Name` property from within the loop, to find out the exact name of
the folders.

Do that for the folder items too, unless you only want to get a specific
item.
Dan Campbell
2020-05-28 21:02:17 UTC
Permalink
Post by JJ
Post by Dan Campbell
Set objFldr = objNamespace.Folders("ThisSpecificFolder")
If that code can't be used to get the needed folder, you may want to do it
like this.
Set colFolders = objNamespace.Folders
For Each aFolder in colFolders
If aFolder.Name = "ThisSpecificFolder" Then
'process the folder here...
End If
Next
If the folder isn't found, chances are that you're using the incorrect
folder name. In this case, you can simply call `MsgBox()` to display the
folder's `Name` property from within the loop, to find out the exact name of
the folders.
Do that for the folder items too, unless you only want to get a specific
item.
Thanks, JJ. Here's the solution, prior to cleanup:


Dim objOutlook
Dim TargetFolder_s
Dim objNamespace
Dim colFolders

TargetFolder_s = "ThisSpecificFolder"
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set colFolders = objNamespace.Folders

RecurseFolders colFolders, TargetFolder_s



Set objOutlook = Nothing
Set objNamespace = Nothing
Set colFolders = Nothing


WScript.Quit



Sub RecurseFolders(objTheseFolders, TargetFolder_s)
Dim objItems
Dim objItem
Dim Subject_s
Dim var_s
Dim FolderName_s
For Each ThisFolder In objTheseFolders
FolderName_s = ThisFolder.Name
If FolderName_s = "1_Responses_to_Me" Then
WScript.Echo FolderName_s
Set objItems = ThisFolder.Items

For Each objItem In objItems
Subject_s = objItem.Subject
WScript.Echo Subject_s
Next
End If
ReDim Preserve strPaths(i + 1)
strPaths(i) = ThisFolder.FolderPath
Set colFolders2 = ThisFolder.Folders
i = i + 1
RecurseFolders colFolders2, TargetFolder_s
Next
End Sub

Loading...