Word文档中的页数 [英] Page Count in Word doc
本文介绍了Word文档中的页数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我将项目移至服务器之前,每件事在我的本地计算机上都运行良好,现在在进行页面计数时,错误如下:
对象引用未设置为对象的实例.
Every thing was working fine on my local till i moved my project to the server and now while doing a page count it gives error as:
Object reference not set to an instance of an object.
Microsoft.Office.Interop.Word.WdStatistic stat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages;
count = aDoc.ComputeStatistics(stat, ref missing);//this is the place where im getting this error.
相同的代码在localhost上可以正常工作.
The same code works fine on the localhost.
Microsoft.Office.Interop.Word.ApplicationClass WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
// give any file name of your choice.
string tempath = numberofpage;
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(@"[ ]{2,}", options);
tempath = regex.Replace(tempath, @"");
tempath = tempath.Replace(" \\", "\\");
object fileName = tempath;
object readOnly = false;
object isVisible = true;
// the way to handle parameters you don't care about in .NET
object missing = System.Reflection.Missing.Value;
object objDNS = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
// Make word visible, so you can see what's happening
//WordApp.Visible = true;
// Open the document that was chosen by the dialog
Microsoft.Office.Interop.Word.Document aDoc = WordApp.Documents.Open(ref fileName,
ref missing, ref readOnly, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref isVisible);
Microsoft.Office.Interop.Word.WdStatistic stat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages;
count = aDoc.ComputeStatistics(stat, ref missing);
WordApp.Quit(ref objDNS, ref missing, ref missing);
aDoc = null;
WordApp = null;
GC.Collect();
可能是问题的解决方法
What could be issue and fix
推荐答案
此异常是最容易检测和修复的异常之一.它仅表示某些引用类型变量为null,最有可能是aDoc
.太糟糕了,您没有显示上面的代码.
不用担心:在调试器中以这些行的断点执行它.当执行在该断点处停止时,请检查这些值.如果引用之一必须为非null,但实际上为null,请检查它是如何发生的.通常,请确保使用非空值正确初始化了引用(调用了构造函数或工厂方法,返回并使用了值),或者检查变量或成员是否为空,如果为空,根据您的逻辑做其他事情.
—SA
This exception is one of the easiest to detect and fix. It simply means that some reference-type variable is null, and most likely this isaDoc
. Too bad you did not show the code above.
Not to worry: execute it under debugger with a break point of these line. When execution stops on that break point, examine the values. If one of the reference is required to be non-null but in fact is null, check up how it happens. Generally, either make sure the reference is properly initialized with non-null value (constructor or factory method is called, value returned and used), or, alternatively, check up a variable or a member for null, and, in case of null, do something else, according to your logic.
—SA
我认为,您需要在服务器上安装word并可能分配适当的访问权限.
查看以下链接是否有帮助.
http://support.microsoft.com/kb/257757 [
I think, You need to install word on the server and probably assign appropriate access.
see if following link helps.
http://support.microsoft.com/kb/257757[^]
这篇关于Word文档中的页数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文