MS Outlook VB编程

时间:2014-03-31 15:37:13

标签: sql vba outlook outlook-vba

我在Outlook 2010 VB编辑器中运行以下代码时出现错误的语法错误,似乎正在发生连接& myNamespace.CurrentUser& “

以下是代码:

Sub ConnectSqlServer()
  Dim myNamespace As Outlook.NameSpace
  Dim conn As ADODB.Connection
  Dim rs As ADODB.Recordset
  Dim sConnString As String

  ' Create the connection string.
  sConnString = "Provider=SQLOLEDB;Data Source=WIN-NBST3PHVFV4\ECLIPSE;" & _
                "Initial Catalog=OBlive;" & _
                "User ID=outlook;Password=password123;"

  ' Create the Connection and Recordset objects.
 Set conn = New ADODB.Connection
 Set rs = New ADODB.Recordset
Set myNamespace = Application.GetNamespace("MAPI")

 ' Open the connection and execute.
 conn.Open sConnString
 Set rs = conn.Execute("INSERT INTO dbo.Submissions (CV, Consultant, Timestamp) VALUES ('1', " & myNamespace.CurrentUser & ", CURRENT_TIMESTAMP )")
 ' Clean up
 If CBool(conn.State And adStateOpen) Then conn.Close
 Set conn = Nothing
 Set rs = Nothing
End Sub

非常感谢任何帮助

2 个答案:

答案 0 :(得分:0)

您需要引用值:

...VALUES ('1', '" & myNamespace.CurrentUser & "', CURRENT_TIMESTAMP...

答案 1 :(得分:0)

Namespace.CurrentUser返回Recipient对象。你的意思是myNamespace.CurrentUser。名称