- PR -

C# Excelのプロパティ>ユーザ設定を扱う方法

投稿者投稿内容
あすか
ぬし
会議室デビュー日: 2006/07/12
投稿数: 309
投稿日時: 2006-07-18 09:45
お二方の知識の深さに感服です。

正直な話、理解が追いついていませんが
それはおいおい理解できるようになるはず?

ありがとうございました。
じゃんぬねっと
ぬし
会議室デビュー日: 2004/12/22
投稿数: 7811
お住まい・勤務地: 愛知県名古屋市
投稿日時: 2006-07-18 10:34
引用:

あすかさんの書き込み (2006-07-18 09:45) より:

お二方の知識の深さに感服です。
正直な話、理解が追いついていませんがそれはおいおい理解できるようになるはず?
ありがとうございました。


このままではあまり意義のないスレッドで終わってしまいますね。
1 つ前のレスで、

引用:

正しくは、InvokeMember ですね? (;^-^)
C# で 「遅延バインディング」 を行うには、System.Type.InvokeMember メソッドでしたね。


と書かせて頂いたとおりで、C# でやると以下のように置き換えることができます。

コード:

    private static void MosaMosaAA() {
        Excel.Application xlApplication = null;

        try {
            xlApplication = new Excel.Application();
            xlApplication.Visible = true;
            Excel.Workbooks xlWorkbooks = null;

            try {
                xlWorkbooks = xlApplication.Workbooks;
                Excel.Workbook xlWorkbook = null;

                try {
                    xlWorkbook = xlWorkbooks.Open (
                        @"C:\MakiMakiLove.xls",
                        System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                        System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                        System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                        System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                        System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                        System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                        System.Reflection.Missing.Value, System.Reflection.Missing.Value
                    );
                    object xlDocumentProperties = null;

                    try {
                        xlDocumentProperties = xlWorkbook.CustomDocumentProperties;
                        System.Type typeDocumentProperties = xlDocumentProperties.GetType();
                        object xlDocumentProperty = null;

                        try {
                            xlDocumentProperty = typeDocumentProperties.InvokeMember(
                                "Item",
                                System.Reflection.BindingFlags.GetProperty,
                                null,
                                xlDocumentProperties,
                                new object[] {"タイピスト"}
                            );

                            if (xlDocumentProperty == null) {
                                return;
                            }

                            System.Type typeDocumentProperty = xlDocumentProperty.GetType();
                            object propertyValue = typeDocumentProperty.InvokeMember(
                                "Value",
                                System.Reflection.BindingFlags.GetProperty,
                                null,
                                xlDocumentProperty,
                                null
                            );

                            if (propertyValue != null) {
                                MessageBox.Show("タイピストの値 == " + propertyValue.ToString());
                            }
                        } finally {
                            if (xlDocumentProperty != null) {
                                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlDocumentProperty);
                            }
                        }
                    } finally {
                        if (xlDocumentProperties != null) {
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlDocumentProperties);
                        }
                    }
                } finally {
                    if (xlWorkbook != null) {
                        try {
                            xlWorkbook.Close(
                                System.Reflection.Missing.Value,
                                System.Reflection.Missing.Value,
                                System.Reflection.Missing.Value
                            );
                        } finally {
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook);
                        }
                    }
                }
            } finally {
                if (xlWorkbooks != null) {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbooks);
                }
            }
        } finally {
            if (xlApplication != null) {
                try {
                    xlApplication.Quit();
                } finally {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApplication);
                }
            }
        }
    }


朝からイヤな感じのコードを書いてしまいました... (;^-^)

このコードでは、例として DocumentProperty である "タイピスト" の値を取得しているだけです。
値を設定する場合も、同じように System.Type.InvokeMember メソッドを使用することができます。
この場合、BindingFlags 列挙体には、SetProperty メンバを使用します。

_________________
C# と VB.NET の入門サイト
じゃんぬねっと日誌

スキルアップ/キャリアアップ(JOB@IT)