标签: office-interop

尝试使用Excel 2007执行Office Automation,但继续使用Excel 2003

环境:

Windows XP计算机
安装了Excel 2007和Excel 2003(按此顺序,而不是按时间顺序).
C#3.5

问题:

当我使用PIA进行一些Office自动化时,我使用以下代码行:

var excel = new ApplicationClass();
Run Code Online (Sandbox Code Playgroud)

PIA的版本特别指它为Excel 12.
C:\ WINDOWS\assembly\GAC\Microsoft.Office.Interop.Excel\12.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll
但是:

 excel.Version;//this is 11.0 instead of 12.0
Run Code Online (Sandbox Code Playgroud)

因此,当我尝试打开一个扩展名为.xlsx的文件时,它会警告我文件转换中的功能丢失,并使用excel 2003打开它.我很确定它与安装顺序是2007 - > 2003,但我无法在我的机器上卸载2003 b/c我们在我们的网络服务器上有一些办公室自动化,用于使用excel 2003的无关项目.

我查看了Policy.11.0.Microsoft.Office.Interop.Excel.config的内容,但它说

<bindingRedirect oldVersion="11.0.0.0" newVersion="12.0.0.0"></bindingRedirect>
Run Code Online (Sandbox Code Playgroud)

所以,我不知所措.为什么我不能告诉COM Interop使用哪个版本的Excel?

c# com excel pia office-interop

12
推荐指数
1
解决办法
9621
查看次数

通过C#获取工作表名称的Excel工作表参考

我目前正在使用以下C#代码获取Excel工作表的句柄:

Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(15);
//Get the worksheet "SubSignOff" number
Run Code Online (Sandbox Code Playgroud)

有什么方法可以通过使用工作表名称获得相同的"SubSignOff"

c# excel office-interop

12
推荐指数
1
解决办法
4万
查看次数

如何在不显示Excel互操作的保存对话框的情况下保存工作簿?

我必须创建一个导出DataSet到Excel 的控制台应用程序.问题是它不应该弹出保存窗口,它应该自动创建Excel文件.到目前为止,我有以下代码,但我不知道如何自动保存.非常感谢任何帮助.

public static void CreateWorkbook(DataSet ds, String path)
{
    int rowindex = 0;
    int columnindex = 0;

    Microsoft.Office.Interop.Excel.Application wapp = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Worksheet wsheet;
    Microsoft.Office.Interop.Excel.Workbook wbook;

    wapp.Visible = false;

    wbook = wapp.Workbooks.Add(true);
    wsheet = (Worksheet)wbook.ActiveSheet;

    try
    {
        for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
        {
            wsheet.Cells[1, i + 1] = ds.Tables[0].Columns[i].ColumnName;

        }

        foreach (DataRow row in ds.Tables[0].Rows)
        {
            rowindex++;
            columnindex = 0;
            foreach (DataColumn col in ds.Tables[0].Columns)
            {
                columnindex++;
                wsheet.Cells[rowindex + 1, columnindex] = row[col.ColumnName]; …
Run Code Online (Sandbox Code Playgroud)

.net c# excel save office-interop

12
推荐指数
3
解决办法
6万
查看次数

使用C#读取Outlook邮件

我尝试连接到Outlook邮件时使用以下代码.现在,我必须做错事,因为我试图获取收件箱邮件,我总是得到0邮件(当情况不是这样).这是我的代码

 Microsoft.Office.Interop.Outlook.NameSpace nameSpace = application.GetNamespace("MAPI");
 nameSpace.Logon("", "", Missing.Value, Missing.Value);

 inboxFolder = nameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
 Console.WriteLine("Folders: {0}", inboxFolder.Folders.Count);
Run Code Online (Sandbox Code Playgroud)

我的Outlook个人资料中有几个电子邮件帐户.当我写下面的内容时

Console.WriteLine("Accounts: {0}",nameSpace.Accounts.Count);
Console.WriteLine("Name: {0}", nameSpace.Accounts[1].DisplayName);
Run Code Online (Sandbox Code Playgroud)

帐户总数正确显示,我真正想要访问的帐户的名称也是如此(索引1).现在,问题是我需要访问该帐户中的特定文件夹.我该怎么做呢?

c# outlook office-interop

12
推荐指数
1
解决办法
3万
查看次数

以编程方式设置Outlook mailitem的类别?

似乎没有太多信息或任何良好的代码示例用于以编程方式设置Outlook 2007 MailItem的类别.

MSDN有一个有限的页面,并提到使用VB的Split函数,或多或少说" 你自己从这里开始,所以你自己解决 ".

据我所知,我们将类别操作为mailitem的逗号分隔字符串属性.它似乎有点原始,是不是都有它呢?

是否每个人都只是挖掘出自己的字符串函数库,并解析类别属性,相信没有一个懵懵懂懂地得到当多个类别单个的MailItem设置和(但愿)类别更名?

outlook office-interop

12
推荐指数
1
解决办法
8017
查看次数

get_SynonymInfo会为某些单词抛出不足的内存异常

我有一个简单的测试程序,使用Microsoft Word Interop从Thesaurus字典中获取单词含义:

using System;
using Word = Microsoft.Office.Interop.Word;

class Program
{
    static void Main(string[] args)
    {
        string word = "elite"; // = "common";

        var app = new Word.Application();
        var synInfo = app.SynonymInfo[word, Word.WdLanguageID.wdEnglishUS];

        if (synInfo.Found && synInfo.MeaningCount > 0)
        {
            foreach (var meaning in synInfo.MeaningList as Array)
                Console.WriteLine(meaning.ToString());
        }

        // release memory and quit Word app... (see below)
    }
}
Run Code Online (Sandbox Code Playgroud)

使用Microsoft Office 2010和Microsoft Office 2013 Preview在Visual Studio 2010中使用.Net 4.0,引用Office 12 PIA.对于超过15万个不同的词,这就像一个魅力.但我注意到,对于某些词,方法get_SynonymInfo抛出异常:

Unhandled Exception: System.Runtime.InteropServices.COMException: Insufficient memory or disk space. …
Run Code Online (Sandbox Code Playgroud)

c# ms-word office-interop

12
推荐指数
1
解决办法
1270
查看次数

使用Interop替换Word中的页眉和页脚中的字段

如何替换页眉/页脚中的"FIELD"?

例如:带有文件名和日期的Word doc文件.代替文件路径 - [FilePath]而不是C://Documents/Location/Filename.doc, [Date]而不是18/07/2013.

我可以用范围替换任何文本.

foreach (Microsoft.Office.Interop.Word.Section section in wordDocument.Sections)
{
   section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text.Replace(sourceDocPath, "[File Path]");

   section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text.Replace(sourceDocPath, "[File Path]"); 
}
Run Code Online (Sandbox Code Playgroud)

这适用于文件名,但是对于Date,无法猜测要替换的格式.这都是因为我无法捕获要替换的确切字段信息.

以下代码也是我无法使用的

wordApp.Selection.Find.Execute(ref textToReplace, ref typeMissing, 
        ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, 
        ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, 
        ref replaceTextWith, ref replaceAll, ref typeMissing, ref typeMissing, 
        ref typeMissing, ref typeMissing);
Run Code Online (Sandbox Code Playgroud)

我现在看到的唯一方法是处理所有可能的日期格式并替换,但这对我来说似乎不是一个好方法.

根据使用Storyrange给出的评论进行更新.

没有给我确切的字段信息说[日期].当我遍历故事范围时,我得到的类型信息是wdstorytype,这是关于部分信息,关于字段信息.

foreach (Microsoft.Office.Interop.Word.Range tmpRange in wordDocument.StoryRanges)
                    {
                        string strtype = tmpRange.StoryType.ToString();
                        tmpRange.Find.Text = "18/07/2013";
                        tmpRange.Find.Replacement.Text = "";
                        tmpRange.Find.Replacement.ParagraphFormat.Alignment =
                            Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;

                        tmpRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
                        object …
Run Code Online (Sandbox Code Playgroud)

ms-word office-interop c#-4.0

12
推荐指数
1
解决办法
1万
查看次数

使用c#和Microsoft Word Interop填充Word中的字段

我尝试使用C#Interop程序集使用以下代码填写Microsoft Word中的表单字段

string filename = @"N:\mehler\Vorlage2.dotx";

Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();

doc = word.Documents.Open(filename);
doc.Activate();

foreach (Microsoft.Office.Interop.Word.FormField field in doc.FormFields)
{
    switch (field.Name)
    {
        case "Text2":
            field.Range.Text = "1";
            break;

        default:
            break;
    }
}

doc.SaveAs2(@"N:\mehler\Ausgefuellt.docx");
doc.Close();
word.Quit();
System.Diagnostics.Process.Start(@"N:\mehler\Ausgefuellt.docx");
Run Code Online (Sandbox Code Playgroud)

Microsoft Word无法打开文件Ausgefuellt.docx并显示一条消息,指出发生了未知错误.

我创建了一个简单的Word文档,其中包含一些未格式化的文本和2个Text-Form-Fields

任何人都可以告诉我,出了什么问题,或者我的源代码中是否有错误

编辑:我设法指定问题.我创建了一个只包含一个文本表单字段的文档.在Word 2013中,这是主题"Formulare aus Vorversionen"(我将其转换为"来自以前版本的Formfields").如果我注释掉整个foreach Block以便我只打开并保存文档,我会得到相同的结果.

如果我直接在Word中打开源文件,则没有问题.我还尝试加载文档并使Word可见.结果看起来像一个没有加载文档的空Word实例.

c# ms-word office-interop

12
推荐指数
1
解决办法
2万
查看次数

为什么不能通过customUI.xml将第三方控件添加到Office PowerPoint中的自定义功能区

我们正尝试通过加载项将第三方加载项控件放到自定义功能区上.

1)通过修改PowerPoint.officeUI进行自定义工作:

<mso:customUI
    xmlns:x1="ThirdPartyAddin"
    xmlns:mso="http://schemas.microsoft.com/office/2009/07/customui">
    <mso:ribbon>
        <mso:qat/>
        <mso:tabs>
            <mso:tab id="my_new_tab" label="my_new_tab">
                <mso:group id="Test" label="New Group" autoScale="true">
                    <mso:gallery idQ="x1:TheirFunction1" visible="true"/>
                </mso:group>
            </mso:tab>
        </mso:tabs>
    </mso:ribbon>
</mso:customUI>
Run Code Online (Sandbox Code Playgroud)

我们使用"文件">"选项">"功能区自定义"提取此xml,并检查生成的PowerPoint.OfficeUI文件.

2)将以下内容放入我们从IRibbonExtensibility.GetCustomUI返回的加载项代码中, 也称为RibbonXML:

<mso:customUI
    xmlns:x1="ThirdPartyAddin"
    xmlns:mso="http://schemas.microsoft.com/office/2009/07/customui">
    <mso:ribbon>
        <mso:tabs>
            <mso:tab id="my_new_tab" label="my_new_tab">
                <mso:group id="Test" label="New Group" autoScale="true">
                    <mso:gallery idQ="x1:TheirFunction1" visible="true"/>
                </mso:group>
            </mso:tab>
        </mso:tabs>
    </mso:ribbon>
</mso:customUI>
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 使用mso:gallery在PowerPoint中没有UI XML错误报告.
  • 我们已删除,因为无法以这种方式修改快速访问工具栏.
  • 而不是在这里显示的画廊我也尝试了一个按钮,在PowerPoint.officeUI中列为mso:控件,但我必须更改为mso:GetCustomUI的按钮,否则PowerPoint会抱怨"0x80004005自定义控件不能指定为<control> .ID x1:theirFunction2需要控制类型

问题

该项目确实出现但它无法正常工作.它有画廊的小箭头,但没有图标.

意见

  • 具有讽刺意味的是,可以右键单击它并将其添加到快速访问工具栏中,然后再次运行.
  • 在加载GetCustomUI加载项的同时卸载第三方加载项将导致控件的次要更新

    看截图

相关文档

  • 根据Cindy的评论,我更倾向于调查"控制"有一个非常相似的问题,遗憾的是答案未经证实:可以控制克隆
  • 这也是关于"控制" MSDN的文档

powerpoint ribbon ms-office office-interop

12
推荐指数
0
解决办法
383
查看次数

多DPI系统上的VSTO自定义任务窗口显示内容两次

我正在使用VSTO构建办公室插件.在具有不同DPI设置的多个监视器的系统上,我的自定义任务窗格的内容在具有较高DPI设置的监视器上绘制两次:

在此输入图像描述

只有较小的版本实际上响应用户输入.较大的版本似乎只是一个放大的图像.

我尝试过使用各种DPI相关设置,例如:

  • AutoScaleMode在我的用户控制上.我尝试了所有选项,没有变化.
  • 将过程设置为DPI可识别 - 或不 - 使用SetProcessDpiAwareness.我尝试了所有选项,没有变化.
  • 使用app.manifest并设置dpiAwaretruefalse.没变.

新的Web Addins没有这个问题.此外,内部任务窗格没有此问题.

这是一个已知的问题吗?我怎样才能解决这个问题?

c# powerpoint vsto ms-office office-interop

12
推荐指数
2
解决办法
878
查看次数

标签 统计

office-interop ×10

c# ×7

excel ×3

ms-word ×3

ms-office ×2

outlook ×2

powerpoint ×2

.net ×1

c#-4.0 ×1

com ×1

pia ×1

ribbon ×1

save ×1

vsto ×1