Word ファイル (.docx) を開いてそのファイルのプロパティを設定する簡単なコードを作成しました。このコードは、それ自体で正確に機能します。しかし、C# で Microsoft Word を開くと、すべてのフィールドを更新して、コードから更新された実際の値を確認する必要があります。
Microsoft Word が開いているときにコードで何を行うかを知りたいのですが、すべてのプロパティには実際の値があり、mysself でプロパティを更新する必要はありません。
これは私のコードです:
ヒント:辞書のプロパティには、プロパティ名と値が含まれます。
public void SetWordFile(string FilePath, Dictionary<string, object> properties)
{
Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word._Document oDoc;
object originalFormat = Missing.Value;
object routeDocument = Missing.Value;
object oMissing = Missing.Value;
object saveChanges = false;
object oDocBuiltInProps;
object oDocAuthorProp;
Type typeDocAuthorProp;
oWord.Visible = true;
object oFalse = false;
object filePath = FilePath;
oDoc = oWord.Documents.Open(ref filePath, ref oMissing, ref oFalse, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oDocBuiltInProps = oDoc.CustomDocumentProperties;
Type typeDocBuiltInProps = oDocBuiltInProps.GetType();
foreach (string item in properties.Keys)
{
oDocAuthorProp = typeDocBuiltInProps.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.GetProperty,
null, oDocBuiltInProps,
new object[] { item });
typeDocAuthorProp = oDocAuthorProp.GetType();
typeDocAuthorProp.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.SetProperty,
null, oDocBuiltInProps,
new object[] { item, properties[item] });
Thread.Sleep(10);
}
}