2017-09-24 118 views
0

我试图从Excel中的userform设置Outlook约会。如果我引用单元格,代码将起作用。如何参考userform中的框?我还需要添加到我将从不同列表工作表中引用的会议的代码收件人。通过用户表单设置Outlook约会数据

下面是引用单元格在Excel中的代码工作通过单击工作表中的按钮:

Sub AddAppointments() 
    ' Create the Outlook session 
    Set myOutlook = CreateObject("Outlook.Application") 

    ' Start at row 2 
r = 2 

Do Until Trim(Cells(r, 1).Value) = "" 
    ' Create the AppointmentItem 
    Set myApt = myOutlook.CreateItem(1) 
    ' Set the appointment properties 
    myApt.Subject = Cells(r, 1).Value 
    myApt.Location = Cells(r, 2).Value 
    myApt.Start = Cells(r, 3).Value 
    myApt.Duration = Cells(r, 4).Value 
    ' If Busy Status is not specified, default to 2 (Busy) 
    If Trim(Cells(r, 5).Value) = "" Then 
     myApt.BusyStatus = 2 
    Else 
     myApt.BusyStatus = Cells(r, 5).Value 
    End If 
    If Cells(r, 6).Value > 0 Then 
     myApt.ReminderSet = True 
     myApt.ReminderMinutesBeforeStart = Cells(r, 6).Value 
    Else 
     myApt.ReminderSet = False 
    End If 
    myApt.Body = Cells(r, 7).Value 
    myApt.Display 
    r = r + 1 
Loop 
End Sub 

这是我尝试在改变引用框在一个窗体代码:

Private Sub Cmdappointment_Click() 

Dim outlookapp As Object 
'the mail item is the contents inside a mail 
Dim mitem As AppointmentItem 
'created outlook app 
Set outlookapp = CreateObject("outlook.Application") 
'it will open a new application 
Set outlookapp = New Outlook.Application 
'Set mail item 
Set mitem = outlookapp.CreateItem(olMailItem) 
Do Until userform2.TextBox4.Value = "" 

    ' Create the AppointmentItem 
    Set myApt = myOutlook.CreateItem(1) 

    ' Set the appointment properties 
    On Error Resume Next 

    mitem 

     myApt.Subject = Me.texbox4.Value 
     myApt.Location = Me.texbox3.Value 
     myApt.Start = Me.ComboBox1.Value 
     myApt.Duration = Me.ComboBox2.Value 
     ' If Busy Status is not specified, default to 2 (Busy) 
     If Me.ComboBox3.Value = "" Then 
      myApt.BusyStatus = 2 
     Else 
      myApt.BusyStatus = Me.ComboBox3.Value 
     End If 
     If Me.TextBox1.Value > 0 Then 
      myApt.ReminderSet = True 
      myApt.ReminderMinutesBeforeStart = Me.TextBox1.Value 
     Else 
      myApt.ReminderSet = False 
     End If 
     myApt.Body = Me.TextBox2.Value 
     myApt.Display 

    End With 
Loop 

End Sub 
+0

你得到的错误是什么?我会建议评论'On Error Resume Next'这一行以知道抛出了什么错误,那么你将能够更轻松地继续。 – kaza

+0

如果我在接下来的错误恢复发表评论,我得到的错误消息是:无效使用我的关键字。根据我的预约。主题 –

+0

你去哪里,用'UserForm'明确引用替换所有'me'。 – kaza

回答

0
Sub cmdappointment_Click() 
    ' Create the Outlook session 
    Set myOutlook = CreateObject("Outlook.Application") 


    Do Until userform2.TextBox4.Value = "" 
     ' Create the AppointmentItem 
     Set myApt = myOutlook.CreateItem(1) 
     ' Set the appointment properties 
     myApt.Subject = userform2.TextBox4.Value 
     myApt.Location = userform2.TextBox3.Value 
     myApt.Start = userform2.ComboBox1.Value 
     myApt.Duration = userform2.ComboBox2.Value 
     ' If Busy Status is not specified, default to 2 (Busy) 
     If userform2.ComboBox3.Value = "" Then 
      myApt.BusyStatus = 2 
     Else 
      myApt.BusyStatus = userform2.ComboBox3.Value 
     End If 
     If userform2.TextBox1.Value > 0 Then 
      myApt.ReminderSet = True 
      myApt.ReminderMinutesBeforeStart = userform2.TextBox1.Value 
     Else 
      myApt.ReminderSet = False 
     End If 
     myApt.Body = userform2.TextBox2.Value 
     myApt.Display 
    Exit Do 
    Loop 


End Sub 
+0

替换'设置myApt = myOutlook.CreateItem(1)'用'Set myApt = outlookapp.CreateItem(1)' 。请注意,即使这样做的代码是不正确的。 – kaza

+0

只有在除繁忙状态以外的所有框都填满的情况下,新代码才适用于我。我将不得不添加一个容易的消息框。我遇到的问题添加收件人的代码 –

0

对不起的意见可能不适合的代码,所以这里有几个问题...

您正在创建outlookapp并使用myOutlook对象。
而且您还正在创建两个邮件分别从outlookappmyOutlookmitemmyApt。最终只使用myApt。我不知道myOutlook的起源。但我会重写代码来使用一套。 一组Outlook和的MailItem的对象,就像在工作表中的应用

Set outlookapp = CreateObject("outlook.Application") 
'it will open a new application 
Set outlookapp = New Outlook.Application 
'Set mail item 
Set mitem = outlookapp.CreateItem(olMailItem) 
Do Until userform2.TextBox4.Value = "" 

    ' Create the AppointmentItem 
    Set myApt = myOutlook.CreateItem(1) 

要添加收件人做以下

myApt.Recipients.Add('j doe') 

为了让更多的安全我还会添加下面的行

Dim myApt As AppointmentItem 
+0

@jdoee有帮助吗?如果你喜欢,我可以更正这些代码吗?但是,从字面上看,你的ss代码中存在的是我粘贴的块。 – kaza

+0

更新答案以将收件人添加到您的预约。 – kaza

+0

Bulbus你是男人!非常感谢你的帮助。 –