Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
576 views
in Technique[技术] by (71.8m points)

vbscript - Create a folder in Outlook: error 800A0401 - Expected End of Statement

I created a .vbs file to create a folder in Outlook.

I copied most of the script out of MSDN and get

"Expected End of Statement" error code 800A0401

Option Explicit
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.Folder
Dim myNewFolder As Outlook.Folder

Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myNewFolder = myFolder.Folders.Add("Postini")

Wscript.Echo "Folder created"
Wscript.Quit

Never created a .vbs script before.

Windows 7 64-bit and Outlook 2010. Running as local administrator.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This error is because you cannot dim variables as something in particular in VBS. Said more explicitly the "Dim" statement is used without defining the variable type in VBScript because all variables in VBScript are automatically of type Variant. If you attempt to Dim a variable as anything, it will throw an error.

Instead, you want:

Dim myNameSpace
Dim myFolder
Dim myNewFolder

Additionally, you seem to have just copied some VBA from Outlook and tried to run it as VBS.

VBscript does not know how to interpret Application.GetNameSpace("MAPI").

You will need to also create an Outlook Application.

dim myOutlook
set myOUtlook = CreateObject("Outlook.Application")

Also, since you cannot provide references in VBS, you have to use late binding for any objects (which is why I used CreateObject.) So re-written your code is as follows:

Option Explicit
Dim myOutlook
Dim myNameSpace
Dim myFolder
Dim myNewFolder

set myOUtlook = CreateObject("Outlook.Application")
Set myNameSpace = myOutlook.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(6) '6 is the value of olFolderInbox
Set myNewFolder = myFolder.Folders.Add("Postini")  
Wscript.Echo "Folder created"
Wscript.Quit

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...