C# Office DocumentProperty返回动态而不是DocumentProperties集合

C# Office DocumentProperty返回动态而不是DocumentProperties集合,c#,.net,dynamic,ms-office,office-interop,C#,.net,Dynamic,Ms Office,Office Interop,我正在尝试访问和修改Office中的DocumentProperties(我尝试Word atm,但稍后我想扩展到Excel,这应该不会是一个问题,因为互操作的工作方式非常类似),但目前我遇到了一个非常令人担忧的问题,即无法获得我所猜测的类型 以下是我代码的一部分: var testWordApp = new Word.Application(); var testWordFile = testWordApp.Documents.Open( @"C:\Wor

我正在尝试访问和修改Office中的DocumentProperties(我尝试Word atm,但稍后我想扩展到Excel,这应该不会是一个问题,因为互操作的工作方式非常类似),但目前我遇到了一个非常令人担忧的问题,即无法获得我所猜测的类型

以下是我代码的一部分:

var testWordApp = new Word.Application();
var testWordFile = testWordApp.Documents.Open(
                   @"C:\Work\Intern\DocPropChanger_Projektarbeit\" + 
                   @"PrototypeVorlagen\Proj-Nr_QPP_VersionVorlage_endeu.docx",
                   ReadOnly: false, Visible: false);

dynamic test = testWordFile.BuiltInDocumentProperties;
这段代码确实给了我内置的DocumentProperties,比如last author、revision number等等,我可以用foreach检查它,但它与它应该是的不同

MSDN和其他来源清楚地将返回的对象强制转换为DocumentProperties集合,而如果我这样做,则会得到一个InvalidCastException

我目前正在VS 2015 Express和Office 13中工作,但我已经在VS 2015社区中尝试了VSTO,并获得了相同的结果

以下是SO中的其他用户提出的问题,他们(或多或少)做了相同的事情:

这似乎对他有效,我提到了框架的适当部分,这些部分是:

Office.Core
Office.Interop.Word
由于使用不便而导致的主要问题

dynamic
导致无法添加我自己的属性,我尝试这样做:

testWordFile.CustomDocumentProperties.Add(
              Name: d.Name, 
              LinkToContent: false, 
              Type: 4, 
              Value: "Testtext aus Programm");
欧元:我还尝试添加到测试中,结果也是这样

这将导致一个例外:

HRESULT:0x8000FFFF

这是在谷歌搜索了一小段时间后发现的一个相当普遍的错误

我该怎么做才能取回正确的收藏?我在添加属性时是否出错

我查看了其他网站(其中一个是上面链接的MSDN页面),以供参考:

欧元:

澄清:

我必须获得设置的每个自定义属性,即使不知道其名称,因此我没有找到一种方法,只能使用前面给出的方法来使用dynamic并处理它

正如Cindy Meister所问,我目前没有使用VSTO,但正如前面所述,我已经尝试了一种方法,导致了我现在遇到的相同问题,这可能与我缺乏VSTO经验有关

下面是我的类中更完整的代码,只是为了它:

这是一个原型,因此使用的所有变量都没有以一种清晰易懂的方式命名,这应该不是一个大问题,因为代码并不太复杂

var testWordApp = new Word.Application();
var testWordFile = testWordApp.Documents.Open(
                    @"C:\Work\Intern\DocPropChanger_Projektarbeit"+
                    @"\PrototypeVorlagen\Proj-Nr_QPP_VersionVorlage_endeu.docx",
                    ReadOnly: false, Visible: false);
dynamic test = testWordFile.BuiltInDocumentProperties;
Console.WriteLine(test.GetType());
foreach (dynamic d in test)
{
  //TryCatch due to the fact, that I also get some more stuff, that are not Properties...
  try
  {
    //I wanted to check the returned Types and if they have one at all
    //This was something someone in the internet stated 
    //(Props not having a valid Type ...)
    Console.WriteLine("\r\n---------\r\n");
    Console.WriteLine(d.GetType());
    Console.WriteLine(d.Name + " # " + d.Name.GetType());
    Console.WriteLine(d.Type + " # " + d.Type.GetType());
    Console.WriteLine(d.Value + " # " + d.Value.GetType());
  }
  catch
  { }
}

dynamic test2 = testWordFile.CustomDocumentProperties;
Console.WriteLine(test2.GetType());
foreach (dynamic d in test2)
{
  try
  {
    Console.WriteLine("\r\n---------\r\n");
    Console.WriteLine(d.GetType());
    Console.WriteLine(d.Name + " # " + d.Name.GetType());
    Console.WriteLine(d.Type + " # " + d.Type.GetType());
    Console.WriteLine(d.Value + " # " + d.Value.GetType());
    if(d.Name == "TestpropText")
    {
      //For highlighting
      Console.WriteLine("#+#+#+#+#+#+#+#+#+#+#");
      //This works like a charm
      testWordFile.CustomDocumentProperties[d.Name].Delete();
      //This results in the previously mentioned HRESULT: 0x8000FFFF 
      test.Add(Name: d.Name, LinkToContent: false, Type: 4, Value: "Testtext aus Programm");
    }

  }
  catch(Exception e)
  {
    Console.WriteLine(e.InnerException);
  }
}

testWordApp.Documents.Save(NoPrompt: true, OriginalFormat: true);
testWordApp.Application.Quit(SaveChanges: false, OriginalFormat: false, 
                             RouteDocument: false);

您的问题组织得很差,并且您没有向我们展示不起作用的代码,这使得问题变得很困难。有一件事根本不清楚,那就是你是否在使用VSTO?这就不同了。但是,简而言之,当您无法使用VSTO时,“以编程方式访问Excel自定义文档属性”中的信息是正确的。没有其他方法。可能是重复的