vba to vbs script

R

rossini73

I have this code to create new folder under the inbox that works fine
in the vb editor in outlook.

Sub AddNewFolder()
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.MAPIFolder
Dim myNewFolder As Outlook.MAPIFolder
Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myNewFolder = myFolder.Folders.Add("test")
End Sub

I want to run this code from the command line with a .vbs or anything
that can be added to a login script. I tried running the following as
a vbs, no errors are reported but no folder is created either.

Sub AddFolder()
Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = Application.GetNameSpace("MAPI")
Set myFolder = myNamespace.GetDefaultFolder(6)
Set myNewFolder = myFolder.Folders.Add("test")
MsgBox "Test Folder Created"
End Sub

Any help from script/outlook pros would be appreciated, thanks.
 
S

Sue Mosher [MVP-Outlook]

Try changing this statement

Set myNameSpace = Application.GetNameSpace("MAPI")

to

Set myNameSpace = myOlApp.GetNameSpace("MAPI")
myNameSpace.Logon

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
R

rossini73

Sue thanks a lot for replying,

I ran the script like you suggested.

Sub AddNewFolder()
Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNameSpace("MAPI")
myNameSpace.Logon
Set myFolder = myNamespace.GetDefaultFolder(6)
Set myNewFolder = myFolder.Folders.Add("test")
MsgBox "Test Folder Created"
End Sub

I get the same result, no errors, no folder, and the msgbox doesn't pop
up.

Can this even be done the way I am trying to do it? From what I have
read there is no way to add a folder to a bunch of users under their
inbox, without their knowing or interacting, unless you manually add it
to each one, or possibly have the click a button on a custom form. I
read that 2007 this can be done but not with 2003 which is what we are
running. I need a way to do it silently without the user interacting.
 
S

Sue Mosher [MVP-Outlook]

Try expanding the myNamespace.Logon to either log onto a particular Outlook mail profile whose name you know or prompt the user, e.g.:

myNamespace.Logon "profile name", "", False, True

or

myNamespace.Logon "", "", True, True

Also, VBScript does not support typed variable declarations (your As clauses).
--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
R

rossini73

Thanks again for the reply.

I tired both ways you suggested again and still same problem. No
errors, no change.

The as clauses was what i used for the vba scripts that worked, I am
trying to move it over to vbs and removed those clauses, as the second
section of code in the original post shows.

Have you ever been able to add a new outlook folder without user
interaction, through a form or some other method?
 
R

rvalstar

If you comment out the guts of your subprogram 'cept the msgbox,
statement, does the msgbox pop up correctly in the VBS?

Just trying to cover the obvious.

Rick Valstar
Star Consulting
r + last name + at + gmail + dot + com
 
R

rossini73

Yes after testing for a while with no success I added the msgbox to
make sure something was happening. If I comment out the other code and
leave the msgbox, I see the popup ok.
 
P

PapaDos

Try

myNameSpace.Logon,,true,true

Is a "Profile" dialog popping up ?

Outlook has to be your "Current default mail client".
 
R

rossini73

Outlooik is the default mail client, I still get nothing, no popup
profile. I have pretty much given up on it. Thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top