获取MS VB编译错误"对象不支持此属性或方法:ObjNamespace.GetDefaultFolder"

时间:2018-03-12 14:11:10

标签: vba excel-vba vbscript hp-uft excel

我的问题陈述是这样的: 我想通过Outlook触发自动电子邮件给用户。邮件触发后,脚本将等待2分钟。如果收件人没有回复。我需要向同一个用户发送提醒自动邮件。 这是我的VB代码:

Option Explicit 'Option Explicit forces variable declaration
Dim StartTime
Dim Elapsed
Dim Subject, Subject1
Dim Body
Dim sAttachment1
Dim oMailobj, oSendmail
Dim objOutlook
Dim objNamespace, objfolder
Dim fold,getsubfolders
Dim Inbox


Subject= "Test Run Results"
Body = "<Html><Body><h3><font face = " & "Callibri" & ">"  & "Hi," & "<br></br><br></br>" & "This is an automated Email for Test Run Results" & "<br></br>" & "<br></br>" &"Domain: Test"& "<br></br>" &"Project: Automation" & "<br></br><br></br>" &"Thanks,"&  "<br></br>" &"Testing Team"  &  "<br></br>" & "</font></h3>" & "<H5  align =" & "center" & ">" &"<font color = " & "Gray" & ">" & "This is an auto generated e-mail." & "</font></H5></Body></HTML>"
Sendemail "abc@xyz.com",Subject,Body,sAttachment1

StartTime = Timer 'Start the Timer
Wscript.Echo Time 'Print the current time



Elapsed = Timer - StartTime 'Initialize Elapsed variable (not critical)
Wscript.Echo Elapsed

Do While Elapsed < 60  

    WScript.Sleep(60000)  'Pause for 1 minute
    Elapsed = Timer - StartTime
    WScript.Echo (Elapsed)

Loop

Wscript.Echo "Hi"
Subject1 = "RE: " & Subject
Body = "<Html><Body><h3><font face = " & "Callibri" & ">"  & "Hi," & "<br></br><br></br>" & "This is a Reminder Mail for Test Run Results" & "<br></br>" & "<br></br>" &"Domain: Test"& "<br></br>" &"Project: Automation" & "<br></br><br></br>" &"Thanks,"&  "<br></br>" &"Testing Team"  &  "<br></br>" & "</font></h3>" & "<H5  align =" & "center" & ">" &"<font color = " & "Gray" & ">" & "This is an auto generated e-mail." & "</font></H5></Body></HTML>"

ReadInbox Inbox

'Function to send Email 
Function Sendemail (sMailto,sSubject,sBody,sAttachment)
    set oMailobj=CreateObject("Outlook.Application")
    set oSendmail=oMailobj.CreateItem(0)
        oSendmail.To=sMailto
        oSendmail.Subject=sSubject
        oSendmail.BodyFormat = 1
        oSendmail.HTMLBody=sBody

        oSendmail.Send
    set oSendmail=Nothing
    set oMailobj=Nothing
End Function

Function ReadInbox(foldername)
    'Dim objOutlook as Object
    Set objOutlook  = CreateObject("Outlook.Application")
    Set objNamespace = objOutlook.GetNamespace("MAPI")

    Set objfolder = objNamespace.GetDefaultFolder(6) 'Inbox
    Wscript.Echo objfolder.Name

    Set colItems = objfolder.Items
    For each mail in colItems
        If mail.subject = "RE: " & Subject then
            Wscript.Echo "User has already replied within Time Limit"
            Exit For
        Else
            Sendemail "abc@xyz.com",Subject1,Body,sAttachment1
            Exit For
        End If
    Next

    'Set colFilteredItems = colItems.Restrict("[Unread] = true")
    'msgbox colFilteredItems(1).subject
End Function

我在第59行收到错误消息: 设置objfolder = objNamespace.GetDefaultFolder(foldername)

附上错误屏幕截图。我也在UFT中运行相同的代码。但也有同样的错误。

enter image description here

1 个答案:

答案 0 :(得分:2)

这一行错了:

Set objNamespace = objOutlook.GetNamespace("MAPI").folders

应该只是:

Set objNamespace = objOutlook.GetNamespace("MAPI")