How can I read excel custom document property using c# excel interop

后端 未结 1 1189
我寻月下人不归
我寻月下人不归 2021-01-29 07:34

I\'m trying to check if a custom document property has been set for an excel file or not. And if set then read the value.

Here is the code I\'m using but so far no luck.

相关标签:
1条回答
  • 2021-01-29 08:15

    The principle is the same. Some research into how to use PInvoke would help when it's required for working with the Office "interop". In order to use it, it's necessary to fully understand the part of the Office object model you need to address: the object, the property or method and exactly what arguments are required as there is no IntelliSense that can help. First testing in the VBA interface can make this easier.

    The following code snippet which I have in a test project demonstrates how to address a single Document Property and read, then write its value. Note that the sample code works with BuiltInDocumentProperties. This can be changed to CustomDocumentProperties if that's what is required.

        private void btnUpdateCustomDocProp_Click(object sender, EventArgs e)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            Excel.Application xlApp = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.application");
            Excel.Workbook wb = xlApp.ActiveWorkbook;
            object docProps = wb.BuiltinDocumentProperties;
    
            object prop = ExistsDocProp("Author", docProps);
            if (prop!=null)
            {
                object oProp = prop;
                Type oPropType = oProp.GetType();
                //read current value
                string propValue = oPropType.InvokeMember("Value",
                    BindingFlags.GetProperty | BindingFlags.Default,
                    null, oProp, new object[] { }).ToString();
    
                object oPropValue = "new test author";
                //write new value
                oPropType.InvokeMember("Value",
                    BindingFlags.SetProperty | BindingFlags.Default,
                    null, oProp, new object[] {oPropValue});
    
                MessageBox.Show(propValue + ", " + oPropValue.ToString());         
            }
        }
    
        private object ExistsDocProp(string propName, object props)
        {
            Office.DocumentProperty customDocProp = null;
            Type docPropsType = props.GetType();
            object nrProps;
            object itemProp = null;
            object oPropName;
    
            nrProps = docPropsType.InvokeMember("Count",
                BindingFlags.GetProperty | BindingFlags.Default,
                null, props, new object[] { });
            int iProps = (int)nrProps;
    
            for (int counter = 1; counter <= ((int)nrProps); counter++)
            {
                itemProp = docPropsType.InvokeMember("Item",
                    BindingFlags.GetProperty | BindingFlags.Default,
                    null, props, new object[] { counter });
    
                oPropName = docPropsType.InvokeMember("Name",
                    BindingFlags.GetProperty | BindingFlags.Default,
                    null, itemProp, new object[] { });
    
                if (propName == oPropName.ToString())
                {
                    break;
                }
            }
            return itemProp; 
        }
    
    0 讨论(0)
提交回复
热议问题