C#转换Word VBA宏BuiltInDocumentProperties("页数")为null

时间:2016-05-13 04:39:01

标签: c# vba ms-word word-vba office-interop

我有一个Word VBA宏,我正在使用Office.Interop转换为C#。

代码运行良好,我能够转换所有内容,但我仍然试图阅读BuiltInDocumentProperties中的页数。

无论我使用的是什么cast,它仍然无效并返回null

以下是转换后的代码:

using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office;
using Microsoft.Office.Interop;
using System.Diagnostics;
using System.Reflection;

Word.Application oWord = new Word.Application();
Word.Document oTgtDoc = new Word.Document();

var PgNum = oTgtDoc.BuiltInDocumentProperties["Number of Pages"];

float intWidthCount = engColWidth;

while (true)
{
    oTgtDoc.Tables[1].Columns[1].SetWidth(intWidthCount, Word.WdRulerStyle.wdAdjustProportional);      

    intWidthCount += 5;
    oTgtDoc.Repaginate();
    oWord.Application.ScreenRefresh();

    if (oTgtDoc.BuiltInDocumentProperties["Number of Pages"] > PgNum && intWidthCount > engColWidth)
    {
        while (oTgtDoc.BuiltInDocumentProperties["Number of Pages"] > PgNum)
        {
            intWidthCount--;
            oTgtDoc.Tables[1].Columns[1].SetWidth(intWidthCount, Word.WdRulerStyle.wdAdjustProportional); 

            oTgtDoc.Repaginate();
            oWord.Application.ScreenRefresh();
        }
        break;
    } 
    else
    {
      PgNum = oTgtDoc.BuiltInDocumentProperties["Number of Pages"];
    }

我已经查看了其他几篇帖子和msdn,但还没有得到正确的解决方案。例如,这一个:Accessing document properties - Excel Workbook/CSV in VB

或者Dictionary的这个,因为我需要在while循环中多次访问它,因此无法正常工作:Read BuiltInDocumentProperties/CustomDocumentProperties alway null with Word 2010?

有关如何从我的C#代码访问此BuiltInDocumentProperties["Number of Pages"]的任何建议吗?

2 个答案:

答案 0 :(得分:1)

是否必须来自BuiltInDocumentProperties?

你可以试试这个:

// get number of pages
Microsoft.Office.Interop.Word.WdStatistic stat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages;
int pages = doc.ComputeStatistics(stat, Type.Missing);

从这个答案复制: How to get page number?

More info on Word statistics from MSDN

答案 1 :(得分:1)

已修改:在“反思”示例之前添加了使用var PgNum = oTgtDoc.BuiltInDocumentProperties(Word.WdBuiltInProperty.wdPropertyPages).Value.ToString();的示例

已编辑2 :考虑OP需要将PgNum用作int

你必须使用

 var PgNum = oTgtDoc.BuiltInDocumentProperties(Word.WdBuiltInProperty.wdPropertyPages).Value;

 int PgNum = oTgtDoc.BuiltInDocumentProperties(Word.WdBuiltInProperty.wdPropertyPages).Value;

和BTW

Word.Document oTgtDoc = new Word.Document();

不会返回任何新的Word文档(即Word中的“真实”新文档),而只会返回班级中的新word文档对象

你真的想要一个新的空白UI Word文档而不是你使用的:

    object oMissing = Missing.Value;
    Word.Document oTgtDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

这是我用控制台应用程序测试var PgNum = oTgtDoc.BuiltInDocumentProperties(Word.WdBuiltInProperty.wdPropertyPages).Value.ToString();的方法

using System;
using System.Reflection;
using Word = Microsoft.Office.Interop.Word;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {    
            RunWordExample();
        } 


        static void RunWordExample()
        {
            Word.Application oWord = new Word.Application(); //Create an instance of Microsoft Word

            //Create a new Document
            object oMissing = Missing.Value;
            Word.Document oTgtDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            //make Word visible
            oWord.Visible = true;

            // get number of pages #1
            var PgNum = oTgtDoc.BuiltInDocumentProperties(Word.WdBuiltInProperty.wdPropertyPages).Value.ToString();
            Console.WriteLine("doc {0} has {1} page(s)", oTgtDoc.Name, PgNum);
            Console.ReadLine();

        }

    }
}

最后,通过深入查看您提供的链接,我认为通过利用BuiltInDocumentProperties对象

来构建帮助程序类并了解有关Reflection的工作方式可能会有所帮助。
using System;
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;

namespace WordHelpers
{
    class MyWordHelpers
    {   
        public static string GetBuiltInDocumentProperty(Word.Document oDoc, string propertyName)
        {
            object oDocBuiltInProps;

            oDocBuiltInProps = oDoc.BuiltInDocumentProperties;
            Type typeDocBuiltInProps = oDocBuiltInProps.GetType();

            //get the property
            object oDocAuthorProp = typeDocBuiltInProps.InvokeMember("Item",
                                       BindingFlags.Default |
                                       BindingFlags.GetProperty,
                                       null, oDocBuiltInProps,
                                       new object[] { propertyName }); // <-- here you exploit the passed property 

            //get the property type
            Type typeDocAuthorProp = oDocAuthorProp.GetType();

            // return the property value
            return typeDocAuthorProp.InvokeMember("Value",
                                       BindingFlags.Default |
                                       BindingFlags.GetProperty,
                                       null, oDocAuthorProp,
                                       new object[] { }).ToString(); ;
        }
    }

}

使用如下(来自控制台应用程序)

static void RunWordExample()
{
    Word.Application oWord = new Word.Application(); //Create an instance of Microsoft Word

    //Create a new Document
    object oMissing = Missing.Value;
    Word.Document oTgtDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

    //make Word visible
    oWord.Visible = true;

    // get number of pages
    Console.WriteLine("doc {0} has {1} page(s)", oTgtDoc.Name, MyWordHelpers.GetBuiltInDocumentProperty(oTgtDoc,"Number of Pages"));
    Console.ReadLine();

}