名称'application'在当前上下文中不存在

时间:2012-12-11 11:43:37

标签: outlook vsto ribbon

您好我正在努力开发使用VSTO 2012和Microsoft Outlook 2010插件在Outlook 2010中创建工具栏的解决方案。简而言之,我能够创建Outlook功能区和按钮,但我无法获得打开.oft文件的按钮。在Visual Studio中,我收到以下错误“当前上下文中不存在名称'应用程序'”。我还添加了对Microsoft Office 14.0对象库的引用。以下是代码......

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Ribbon;

namespace OutlookAddIn8
{
public partial class Ribbon1
{
    private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
    {

    }
    private void CreateItemFromTemplate()
    {
        Outlook.Folder folder =
            Application.Session.GetDefaultFolder(
            Outlook.OlDefaultFolders.olFolderDrafts) as Outlook.Folder;
        Outlook.MailItem mail =
            Application.CreateItemFromTemplate(
            @"c:\ivy.oft", folder) as Outlook.MailItem;
        mail.Subject = "Congratulations";
        mail.Save();
    }

    private void button1_Click(object sender, RibbonControlEventArgs e)
    {

    }
}

}

感谢任何帮助,感谢它可能是一些简单的错过了。

2 个答案:

答案 0 :(得分:4)

可以使用Globals.ThisAddIn.Application访问应用程序实例。如果您将AddIn类重命名为不同的类别,例如MyAddIn然后命令将是:Globals.MyAddIn.Application

以下链接包含更多详情:http://msdn.microsoft.com/en-us/library/vstudio/bb157876(v=vs.100).aspx

答案 1 :(得分:2)

最后到达那里,继续代码......

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using Outlook = Microsoft.Office.Interop.Outlook;


namespace OutlookAddIn3
{
public partial class Ribbon1
{
    private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
    {

    }
    private void button1_Click(object sender, RibbonControlEventArgs e)
    {
        Outlook.Application Application = Globals.ThisAddIn.Application;
        Outlook.MailItem mail =
            Application.CreateItemFromTemplate(
            @"Z:\Transfer\Outlook 2010 Templates\testsubject.oft") as Outlook.MailItem;
        mail.Display(true);
    }