C# C“转换Word VBA宏内置文档属性(“页数”)为空

C# C“转换Word VBA宏内置文档属性(“页数”)为空,c#,vba,ms-word,office-interop,C#,Vba,Ms Word,Office Interop,我有一个Word VBA宏,我正在使用Office.Interop将其转换为C 代码运行得很好,我能够转换所有内容,但我一直在尝试从内置文档属性读取页面数 无论我使用什么cast,它仍然不起作用并返回null 以下是转换后的代码: 使用Word=Microsoft.Office.Interop.Word; 使用微软办公软件; 使用Microsoft.Office.Interop; 使用系统诊断; 运用系统反思; Word.Application oWord=新单词.Application();

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

代码运行得很好,我能够转换所有内容,但我一直在尝试从
内置文档属性
读取页面数

无论我使用什么
cast
,它仍然不起作用并返回
null

以下是转换后的代码:

使用Word=Microsoft.Office.Interop.Word;
使用微软办公软件;
使用Microsoft.Office.Interop;
使用系统诊断;
运用系统反思;
Word.Application oWord=新单词.Application();
Word.Document oTgtDoc=新的Word.Document();
var PgNum=oTgtDoc.BuiltInDocumentProperties[“页数”];
浮点intWidthCount=engColWidth;
while(true)
{
oTgtDoc.Tables[1]。Columns[1]。SetWidth(intWidthCount,Word.WdRulerStyle.wdAdjustProportional);
intWidthCount+=5;
oTgtDoc.Repaginate();
oWord.Application.screenfresh();
if(oTgtDoc.builtinocumentproperties[“页数”]>PgNum&&intWidthCount>engColWidth)
{
while(oTgtDoc.builtinocumentproperties[“页数”]>PgNum)
{
整数计数--;
oTgtDoc.Tables[1]。Columns[1]。SetWidth(intWidthCount,Word.WdRulerStyle.wdAdjustProportional);
oTgtDoc.Repaginate();
oWord.Application.screenfresh();
}
打破
} 
其他的
{
PgNum=oTgtDoc.BuiltInDocumentProperties[“页数”];
}
我看过其他几篇文章和msdn,但还没有找到正确的解决方案。例如:

或者这一个带有
字典
,它不起作用,因为我需要在
循环中多次访问它,而
循环:


如何从我的C#code访问此
内置文档属性[“页数”]
有什么建议吗?

它必须来自内置文档属性吗

你可以试试这个:

// get number of pages
Microsoft.Office.Interop.Word.WdStatistic stat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages;
int pages = doc.ComputeStatistics(stat, Type.Missing);
摘自此答案:


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

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

你必须使用

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

顺便说一句

Word.Document oTgtDoc = new Word.Document();
不会返回任何新的Word文档(即Word UI中的“真实”新文档),但只返回类中的新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();

        }

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

}

最后,通过深入研究您提供的链接,我认为构建一个helper类并通过利用
builtinocumentproperties
object了解反射是如何工作的是很有用的

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

}

谢谢.RE:“您必须使用
var PgNum=oTgtDoc.BuiltInDocumentProperties(Word.WdBuiltInProperty.wdPropertyPages).Value.ToString();
并且顺便说一句
Word.Document oTgtDoc=new Word.Document();
不会返回任何新的Word文档(即Word UI中的“真正的”新文档),而只是类中的一个新Word文档对象。”确定这没问题,在我的代码中,只保存了空间。这个应该返回页码计数。是吗?
var PgNum=oTgtDoc.BuiltInDocumentProperties(Word.WdBuiltInProperty.wdPropertyPages)。Value.ToString()
我不这么认为。在我的测试中确实如此。因为我没有看到您完整的代码,可能有一些东西丢失了,并阻止了它正常工作。请参阅编辑的答案,向您展示它对我的作用。事实上,在此期间,我发现了如何利用反射。我实现了该代码,我甚至发布了代码:但是您的解决方案n带有
var PgNum=oTgtDoc.BuiltInDocumentProperties(Word.WdBuiltInProperty.wdPropertyPages).Value。‌​ToString();
是最好的!更直接更快。除了我需要使用
Int.Parse
将其转换为
Int
以便我可以进行比较。很高兴能提供帮助。如果我的答案满足了您的问题,请将其标记为已接受。谢谢您这一个也非常好。事实上,更简单的格式副本:
PgNum=oTgtDoc.ComputeStatistics(Word.WdStatistic.wdStatisticPages,类型。缺少)因为我已经有了一个
使用Word=Microsoft.Office.Interop.Word;
,而这一个不需要强制转换。我想知道哪一个更快,上面的这个和另一个看起来都很快。计时会很有趣。我相信我会选择你的版本,因为代码很简单。
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();

}