I have a Word VBA macro that I am converting to C# using the Office.Interop
.
The code works nicely and I was able to convert everything, but I got stuck with trying to read to number of pages from the BuiltInDocumentProperties
.
No matter what cast
I use, it still does not work and returns null
.
Here is the converted code:
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"];
}
I have looked at several other post and msdn and did not get to the right solution yet. For example this one: Accessing document properties - Excel Workbook/CSV in VB
Or this one with the Dictionary
, which would not work as I need to access it several times in the while
loop: Read BuiltInDocumentProperties/CustomDocumentProperties alway null with Word 2010?
Any suggestions how to access this BuiltInDocumentProperties["Number of Pages"]
from my C# code?