2016-06-20 1 views
0

Outlookに接続するスクリプトを作成して、既に作成されたフォルダに移動します。フォルダが存在しない場合はOutlookの特定のフォルダを検索し、電子メールをそのフォルダに移動するためのルールを作成するVBScriptを作成します。

'--> Create some constants 
Const olRuleReceive = 0 

'--> Create some variables 
Dim olApp, olSession, olRuleDirectory, newRule, ruleConditions, ruleAction, folderDirectory, newFolder, moveFolder, index, ruleActiona 
Dim folderexists 


'--> Connect to Outlook 
Set olApp = CreateObject("Outlook.Application") 
Set olSession = olApp.GetNamespace("MAPI") 
olSession.Logon olApp.DefaultProfileName 

' --> Get the rules/folder collection 
Set folderDirectory = olSession.GetDefaultFolder(6) 
Set moveFolder = folderDirectory.Parent 

For i = 1 To moveFolder.Folders.Count 

    If moveFolder.Folders.item(i).Name = "One" Then 
     index = i 
     Exit For 
    End If 
Next 

' set moveFolder = moveFolder.Folders.item(index) 


Set olRuleDirectory = olSession.DefaultStore.GetRules() 

'--> Create a new receive rule 
Set newRule = olRuleDirectory.Create("Test", olRuleReceive) 

'--> Set the rule's condition to look for a specific word in the subject 

Set ruleConditions = newRule.Conditions.Body 
With ruleConditions 
    .Text = Array("test") 
    .Enabled = True 
End With 

' --> Set the rule's action to move to folder 

Set ruleAction = newRule.Actions.MoveToFolder 
With ruleAction 
.Enabled = True 
.Folder = moveFolder.Folders(index) ' this is the part that needs work, a variable needs to be delcared earlier and set to a folder directory. 
End With 


'--> Save the rule 
olRuleDirectory.Save False 

'--> Disconnect from Outlook 
olSession.Logoff 
Set ruleConditions = Nothing 
Set ruleAction = Nothing 
Set newRule = Nothing 
Set olRuleDirectory = Nothing 
Set olSession = Nothing 
Set olApp = Nothing 

'--> Terminate the script 
WScript.Quit 
+0

したがって、どのラインで正確にエラーがスローされますか? –

+0

行47 - '.Folder = moveFolder.Folders(index)' –

答えて

0

は、インデックス変数が初期化されていないようになります。現在、私はここで

ライン47の「サーバーが例外を投げた」エラーを取得しています理由を把握することができませんすることは私のコードです。 指定された名前のフォルダを見つけるために、すべてのサブフォルダをループする必要はありません。ちょうどset folder = moveFolder.Folders.Item("One")を使用してください。

+0

私は47行目でその変更を加えましたが、それでも同じ例外がスローされます。そのセクションは今読んでいます。 'ルールアクトで' '.Enabled = True' ' .Folder = moveFolder.Folders.Item( "One") ' –

関連する問題