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

ε祈祈猫儿з 提交于 2020-01-22 03:42:06

问题


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. It doesn't get into the foreach loop and comes out.

var propval = ReadDocumentProperty("TestProp");

private string ReadDocumentProperty(string propertyName)
{
    Office.DocumentProperties properties;
    Excel.Workbook Wb = Globals.ThisAddIn.Application.ActiveWorkbook;
    properties = (Office.DocumentProperties)Wb.CustomDocumentProperties;

    foreach (Office.DocumentProperty prop in properties)
    {
        if (prop.Name == propertyName)
        {
            return prop.Value.ToString();
        }
    }
    return null;
}

Update1:

I found this code for setting the custom property.

Excel.Workbook workBk = Globals.ThisAddIn.Application.ActiveWorkbook;

            object oDocCustomProps = workBk.CustomDocumentProperties;
            Type typeDocCustomProps = oDocCustomProps.GetType();

            object[] oArgs = {propertyName,false,
         Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString,
         propertyValue};

            typeDocCustomProps.InvokeMember("Add", BindingFlags.Default |
                                       BindingFlags.InvokeMethod, null,
                                       oDocCustomProps, oArgs);

This works fine to set the custom property. I have no clue how to modify it to read the property value.


回答1:


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; 
    }


来源:https://stackoverflow.com/questions/59683995/how-can-i-read-excel-custom-document-property-using-c-sharp-excel-interop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!