0

I am trying to access and modify DocumentProperties in Office (I try Word atm, but later on I want to expand to Excel, which shouldn't be a problem since the interop works quite similar), but at the moment I have the very concerning problem of not getting the type I would guess.

Here is a part of my code:

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;

This code does give me the builtin DocumentProperties like last author, revision number and so on and I can go through it with an foreach, but it is different from what it should be.

MSDN and other sources clearly cast the returned object into an collection of DocumentProperties whereas if I do so aswell get an InvalidCastException.

I am currently working with VS 2015 Express and Office 13, but I already tried VSTO in VS 2015 Community with the same result.

https://msdn.microsoft.com/en-us/library/dhxe2d75.aspx

Here is a question by an other user in SO, who does (more or less)the same thing:

Accessing Excel Custom Document Properties programatically

It does seem to work for him, I have references to the proper parts of the framework, those are:

Office.Core
Office.Interop.Word

The main problem that results out of this inconvenience of having to use

dynamic

result in not being able to add my own Properties, which I try like that:

testWordFile.CustomDocumentProperties.Add(
              Name: d.Name, 
              LinkToContent: false, 
              Type: 4, 
              Value: "Testtext aus Programm");

€: I also tried adding to test which turned out the same way.

This results in an exception:

HRESULT: 0x8000FFFF

This is after a short look in Google a pretty generic error.

What can I do to get the correct collection back? And am I doing a mistake while adding the property?

I looked among others (one of which is the linked above MSDN-page) at this sites for reference:

https://stackoverflow.com/a/12690798/3664953

€²:

For clearification:

I have to get every custom property that is set, even without knowing the name, so I didn't really find an approach other than to use the previously given approach in using dynamic and working with that.

As asked by Cindy Meister, I am currently NOT using VSTO but, also as previously stated, I already tried an approach with that, resulting in the same problems I am running into now, which can be linked in my inexperience with VSTO ...

Here is a more complete code from my class, just for the sake of it:

This is a prototype so all variables used are not named in a clearly understandable way, which shouldn't be a big problem, since the code isn't too complex atm.

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);
Community
  • 1
  • 1
ExNought
  • 193
  • 1
  • 11
  • Your question is poorly organized and you don't show us the code that doesn't work, which makes it difficult. One thing that's not clear at all is whether you're using VSTO, or not? That makes a difference. In a nutshell, however, the information in "Accessing Excel Custom Document Properties programatically" is correct when you're not able to use VSTO. There is no other way. – Cindy Meister Feb 09 '16 at 18:14
  • Possible duplicate of [Accessing Excel Custom Document Properties programatically](http://stackoverflow.com/questions/1137763/accessing-excel-custom-document-properties-programatically) – Cindy Meister Feb 09 '16 at 18:14

0 Answers0