VBScript to get most recent subject headings from specific (not inbox) folder

9 views
Skip to first unread message

Dan Campbell

unread,
May 27, 2020, 8:25:22 PM5/27/20
to
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.

unread,
May 28, 2020, 6:49:00 AM5/28/20
to
Dan Campbell <dcwh...@gmail.com> wrote on 28 May 2020 in
microsoft.public.scripting.vbscript:
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

unread,
May 28, 2020, 8:14:20 AM5/28/20
to
"Evertjan." <exxjxw.h...@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

unread,
May 28, 2020, 9:08:11 AM5/28/20
to
On Thursday, May 28, 2020 at 6:49:00 AM UTC-4, Evertjan. wrote:
> Dan Campbell dc wrote on 28 May 2020 in
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.

JJ

unread,
May 28, 2020, 9:14:17 AM5/28/20
to
On Wed, 27 May 2020 17:25:18 -0700 (PDT), Dan Campbell wrote:
>
> 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.

Evertjan.

unread,
May 28, 2020, 12:07:51 PM5/28/20
to
Dan Campbell <dcwh...@gmail.com> wrote on 28 May 2020 in
microsoft.public.scripting.vbscript:

>> Look here:
>>
>> <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>

Dan Campbell

unread,
May 28, 2020, 5:02:19 PM5/28/20
to
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
Reply all
Reply to author
Forward
0 new messages