Set custom document properties with Word interop

前端 未结 4 1303
春和景丽
春和景丽 2020-12-10 17:01

I want to set some custom document properties of a word document I\'m creating in my C# code. To do this, I followed this MSDN article and came up with this code:



        
4条回答
  •  既然无缘
    2020-12-10 17:45

    SliverNinja's answer above is correct. I forgot you had to .Add to the collection until I ported some old VB code. I had to set and read a bunch of doc properties, so here are a couple extension methods to read/write from either the BuiltInDocumentProperties or the CustomDocumentProperties in Word. This is NetOffice code, but you can convert it to VSTO by just changing the using statements.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NetOffice.OfficeApi;
    using NetOffice.OfficeApi.Enums;
    using NetOffice.WordApi;
    
    namespace PalabraAddin {
        public static class ExtDocument {
    
            public static T GetCustomProperty(this Document doc, string name, T defaultValue) {
                return doc.GetProperty(doc.CustomDocumentProperties, name, defaultValue);
            }
    
            public static T GetBuiltInProperty(this Document doc, string name, T defaultValue) {
                return doc.GetProperty(doc.BuiltInDocumentProperties, name, defaultValue);
            }
    
            public static T SetCustomProperty(this Document doc, string name, T value) {
                return doc.SetProperty(doc.CustomDocumentProperties, name, value);
            }
    
            public static T SetBuiltInProperty(this Document doc, string name, T value) {
                return doc.SetProperty(doc.BuiltInDocumentProperties, name, value);
            }
    
            public static T GetProperty(this Document doc, object collection, string name, T defaultValue) {
                var properties = (DocumentProperties) collection;
                foreach (var prop in properties.Where(prop => prop.Name == name))
                    return (T) prop.Value;
                return defaultValue;
            }
    
            public static T SetProperty(this Document doc, object collection, string name, T value) {
                var properties = (DocumentProperties) collection;
                foreach (var prop in properties.Where(prop => prop.Name == name)) {
                    if (!((T) prop.Value).Equals(value))
                        prop.Value = value;
                    return value;
                }
    
                MsoDocProperties propType;
                if (value is Boolean) 
                    propType = MsoDocProperties.msoPropertyTypeBoolean;
                else if (value is DateTime)
                    propType = MsoDocProperties.msoPropertyTypeDate;
                else if (value is double || value is Single)
                    propType = MsoDocProperties.msoPropertyTypeFloat;
                else if (value is int)
                    propType = MsoDocProperties.msoPropertyTypeNumber;
                else 
                    propType = MsoDocProperties.msoPropertyTypeString;
    
                properties.Add(name, false, propType, value);
                return value;
            }
        }
    }
    

提交回复
热议问题