VBA在莲花笔记中不在办公室

时间:2015-12-14 10:57:09

标签: excel vba lotus-notes

我试图在excel中使用vba在Lotus Notes 8.5中离开办公室,不幸的是它不起作用。我在这个讨论Out of office service activation with an run on server agent中找到了代码,我得到了“OutOfOfficeProfile”,但是在我尝试设置第一天后,我得到了错误。我搜索了很多时间,但我找不到问题的解决方案。

我正在使用的代码是:

Private Function check(text)
    check = False
    If Err.Number <> 0 Then
        logError text & ": " & Err.Number & ", " & Err.Description & " has occurred at " & Err.Source
        Err.clear
        check = True
    End If
End Function

Private Function create_mail_database_name(user_name)
    create_mail_database_name = Left$(user_name, 1) & Right$(user_name, (Len(user_name) - InStr(1, user_name, " "))) & ".nsf"
End Function

Private Function activate_out_of_office()
    check "activate_out_of_office start"
    On Error Resume Next

    Dim notes_session As Object: Set notes_session = CreateObject("Notes.NotesSession")
Dim user_name As String: user_name = current_user_name
Dim mail_database_name As String: mail_database_name = create_mail_database_name(user_name)

Dim notes_database As Object: Set notes_database = notes_session.CurrentDatabase
check "after mail_database"

Dim profile_document As Object: Set profile_document = notes_database.GetProfileDocument("OutOfOfficeProfile")
check "after profile document"

profile_document.FirstDayOut = "17.12.2015"
check "after profile_document.firstdayout"
profile_document.StartTime = "00:00:00"
check "after start time"

profile_document.FirstDayBack = "27.12.2015"
check "after first day back"
profile_document.EndTime = "00:00:00"
check "after end time "

profile_document.CurrentStatus = "1"
check "after current status"

profile_document.GeneralSubject = "HE IS NOT AVAILABLE"
profile_document.GeneralMessage = "general message"
check "after general subject"

Call profile_document.computeWithForm(False, False)
Call profile_document.Save(True, False)
check "after save"

Dim DBOPT_OUTOFOFFICEENABLED As Integer: DBOPT_OUTOFOFFICEENABLED = 74
Call notes_database.SetOption(74, True)
check "after activate"

    check "activate_out_of_office end"
End Function

我得到的失败是:

  • 简介文件后:438,ObjektunterstütztdieseEigenschaft oder Methode nicht发生在VBAProject
  • 在profile_document.firstdayout:91之后,Objektvariable oder With-Block变量nicht festgelegt发生在VBAProject
  • activate_out_of_office_end_normal_58981,79:91,Objektvariable oder With-Block变量nicht festgelegt发生在VBAProject

1 个答案:

答案 0 :(得分:0)

FIRST:发布的代码不完整。缺少函数current_user_namecreate_mail_database_name

SECOND:在大多数情况下获取用户邮件文件的服务器名称不为空,您需要以某种方式填写它。

第三:这两个将导致mail_database成为本地计算机上的随机数据库&#34; (充其量,可能已经导致错误)。

没有&#34;对&#34;数据库,mail_database.Getprofiledocument("OutOfOfficeProfile")将失败,因此尝试读取该文档的属性将导致无效的事情。

尝试用这两行代替Dim mail_database As Object: Set mail_database = notes_session.Getdatabase("", mail_database_name),它会起作用:

Dim mail_database As Object
Set mail_database = notes_session.Getdatabase("", "")
Call mail_database.OpenMail()

但是,由于你不想使用适当的功能,让我们去找另一个&#34; Dos&#34;和#34; Donts&#34;:拥有sais On Error Resume Next的代码确实是最糟糕的做法:你需要捕获错误并停止执行。 将此行替换为On Error goto ErrorHandler,并在代码末尾添加以下代码行:

  Exit Function
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description & " in line " & Erl

这样你就可以发现第一个发生的错误,尽管不可能,但不要继续尝试。

现在您可以调试代码了。

如果您的代码确实返回了用户邮件文件(这可能,但取决于一些运气),您将发现的第一个错误将是Set profile_document.FirstDayOut = leaving_date

由于FirstDayOut不是Object,而是一个简单的数组,所以不应该使用Set here:profile_document.FirstDayOut = leaving_date就足够了。

现在继续调试,你可能会得到正常工作的代码......