WindowsFormsApplication1.Form1'不包含'Application'的定义,也没有扩展方法 [英] WindowsFormsApplication1.Form1' does not contain a definition for 'Application' and no extension method

查看:107
本文介绍了WindowsFormsApplication1.Form1'不包含'Application'的定义,也没有扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用示例代码为日期创建日历,但上述错误即将到来



使用系统.Runtime.InteropServices;

使用Outlook = Microsoft.Office.Interop.Outlook;


private void AddAppointment()

        {

           试试
            {

                

                Outlook.AppointmentItem newAppointment =

                    (Outlook.AppointmentItem)

                this.Application.CreateItem(Outlook.OlItemType.olAppointmentItem);

                newAppointment.Start = DateTime.Now.AddHours(2);

                newAppointment.End = DateTime.Now.AddHours(3);

                newAppointment.Location =" ConferenceRoom#2345";

                newAppointment.Body =

                    "我们将讨论小组项目的进展情况。"; $
                newAppointment.AllDayEvent = false;

                newAppointment.Subject =" Group Project"; $
                newAppointment.Recipients.Add(" Roger Harui");

                Outlook.Recipients sentTo = newAppointment.Recipients;

                Outlook.Recipient sentInvite = null;

                sentInvite = sentTo.Add("Holly Holt");

                sentInvite.Type =(int)Outlook.OlMeetingRecipientType

                    .olRequired;

                sentInvite = sentTo.Add(" David Junca");

                sentInvite.Type =(int)Outlook.OlMeetingRecipientType

                    .olOptional;

                sentTo.ResolveAll();

                newAppointment.Save();

                newAppointment.Display(true);

            }¥b $ b            catch(例外情况)

            {

                MessageBox.Show("发生以下错误:" + ex.Message);

            }¥b $ b        }


以下错误即将发生 


'WindowsFormsApplication1.Form1'不包含'Application'的定义并且没有扩展方法'Application'接受类型为'WindowsFormsApplication1.Form1'的第一个参数'(您是否缺少using指令或汇编引用?)
 


请帮助




polachan

解决方案

你好,


看起来以下代码行触发异常:

 Outlook.AppointmentItem newAppointment = (Outlook.AppointmentItem)this.Application.CreateItem(Outlook.OlItemType.olAppointmentItem); 




您需要将Outlook应用程序的实例传递给Windows窗体实例。例如,您可以为接受Outlook.Application类实例的表单创建一个ctor,并在代码中使用它:


类Form1 
{
private Outlook.Application _application;

public Form1(Outlook.Application app)
{
_application = app;
}

public Outlook.Application OutlookApp
{
get
{
return _application;
}
}
}


所以,在代码中你可以参考OutlookApp属性。


 private void AddAppointment()
{
try
{

Outlook.AppointmentItem newAppointment =
(Outlook.AppointmentItem)
this.OutlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem);
newAppointment.Start = DateTime.Now.AddHours(2);
newAppointment.End = DateTime.Now.AddHours(3);
newAppointment.Location =" ConferenceRoom#2345" ;;
newAppointment.Body =
"我们将讨论小组项目的进展情况。";
newAppointment.AllDayEvent = false;
newAppointment.Subject =" Group Project" ;;
newAppointment.Recipients.Add(" Roger Harui");
Outlook.Recipients sentTo = newAppointment.Recipients;
Outlook.Recipient sentInvite = null;
sentInvite = sentTo.Add(" Holly Holt");
sentInvite.Type =(int)Outlook.OlMeetingRecipientType
.olRequired;
sentInvite = sentTo.Add(" David Junca");
sentInvite.Type =(int)Outlook.OlMeetingRecipientType
.olOptional;
sentTo.ResolveAll();
newAppointment.Save();
newAppointment.Display(true);
}
catch(Exception ex)
{
MessageBox.Show("发生以下错误:" + ex.Message);
}
}



I have used the sample code to create a calendar for the date but the above mentioned error is coming

using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;

private void AddAppointment()
        {
            try
            {
                
                Outlook.AppointmentItem newAppointment =
                    (Outlook.AppointmentItem)
                this.Application.CreateItem(Outlook.OlItemType.olAppointmentItem);
                newAppointment.Start = DateTime.Now.AddHours(2);
                newAppointment.End = DateTime.Now.AddHours(3);
                newAppointment.Location = "ConferenceRoom #2345";
                newAppointment.Body =
                    "We will discuss progress on the group project.";
                newAppointment.AllDayEvent = false;
                newAppointment.Subject = "Group Project";
                newAppointment.Recipients.Add("Roger Harui");
                Outlook.Recipients sentTo = newAppointment.Recipients;
                Outlook.Recipient sentInvite = null;
                sentInvite = sentTo.Add("Holly Holt");
                sentInvite.Type = (int)Outlook.OlMeetingRecipientType
                    .olRequired;
                sentInvite = sentTo.Add("David Junca ");
                sentInvite.Type = (int)Outlook.OlMeetingRecipientType
                    .olOptional;
                sentTo.ResolveAll();
                newAppointment.Save();
                newAppointment.Display(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show("The following error occurred: " + ex.Message);
            }
        }

The following error is coming 

'WindowsFormsApplication1.Form1' does not contain a definition for 'Application' and no extension method 'Application' accepting a first argument of type 'WindowsFormsApplication1.Form1' could be found (are you missing a using directive or an assembly reference?)  

Please help


polachan

解决方案

Hello,

Looks like the following line of code fires an exception:

Outlook.AppointmentItem newAppointment = (Outlook.AppointmentItem)this.Application.CreateItem(Outlook.OlItemType.olAppointmentItem);


You need to pass an instance of the Outlook Application to your Windows form instance. For example, you can create a ctor for the form which accepts an instance of the Outlook.Application class and use it in the code:

class Form1
{
    private Outlook.Application _application;

    public Form1(Outlook.Application app)
    {
       _application = app;
    }   

    public Outlook.Application OutlookApp
    {
       get
       {
           return _application;
       }
    } 
}

So, in the code you could refer to the OutlookApp property.

private void AddAppointment()
        {
            try
            {
                
                Outlook.AppointmentItem newAppointment =
                    (Outlook.AppointmentItem)
                this.OutlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem);
                newAppointment.Start = DateTime.Now.AddHours(2);
                newAppointment.End = DateTime.Now.AddHours(3);
                newAppointment.Location = "ConferenceRoom #2345";
                newAppointment.Body =
                    "We will discuss progress on the group project.";
                newAppointment.AllDayEvent = false;
                newAppointment.Subject = "Group Project";
                newAppointment.Recipients.Add("Roger Harui");
                Outlook.Recipients sentTo = newAppointment.Recipients;
                Outlook.Recipient sentInvite = null;
                sentInvite = sentTo.Add("Holly Holt");
                sentInvite.Type = (int)Outlook.OlMeetingRecipientType
                    .olRequired;
                sentInvite = sentTo.Add("David Junca ");
                sentInvite.Type = (int)Outlook.OlMeetingRecipientType
                    .olOptional;
                sentTo.ResolveAll();
                newAppointment.Save();
                newAppointment.Display(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show("The following error occurred: " + ex.Message);
            }
        }


这篇关于WindowsFormsApplication1.Form1'不包含'Application'的定义,也没有扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆