我正在用C#编写一个Office外接程序.我正试图从带有的Word文档中获取没有空格的字符的数量。但是,到long的转换不起作用。
错误信息是:
"System._ComObject“类型的COM对象不能转换为"System.IConvertible”
到目前为止,下面是我的thisAddIn.cs代码片段:
using Word = Microsoft.Office.Interop.Word;
// ...
public partial class ThisAddIn
{
public void Calc()
{
Word.Document doc = this.Application.ActiveDocument ;
long c=doc.BuiltInDocumentProperties[Word.WdBuiltInProperty.wdPropertyCharacters];
// ^^^ Error ^^^
}
}问题:
和/或
发布于 2015-10-29 16:20:51
不要使用BuiltInDocumentProperties,而是使用具有Count属性的Characters。
long c = doc.Characters.Count;
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.characters.count.aspx
编辑(来自VBA example):
Sub CountChars()
Dim iCount(57) As Integer
Dim x As Integer
Dim iTotal As Integer
Dim iAsc As Integer
Application.ScreenUpdating = False
iTotal = ActiveDocument.Range.Characters.Count
For x = 1 To iTotal
iAsc = Asc(ActiveDocument.Range.Characters(x))
If iAsc >= 65 And iAsc <= 122 Then
iCount(iAsc - 65) = iCount(iAsc - 65) + 1
End If
Next x
For x = 0 To 57
Debug.Print x, iCount(x)
Next x
Application.ScreenUpdating = True
End Sub发布于 2018-10-26 15:57:32
int c = doc.BuiltInDocumentProperties[Word.WdBuiltInProperty.wdPropertyCharacters].Value;BuiltInDocumentProperties[]的结果是Office.DocumentProperty (可以通过检查Word VBA编辑器中的局部变量窗口找到),它的默认.Value属性包含数字。
它在VBA中不使用.Value,因为VBA具有不同的默认值赋值(Let),以及与Set分离的对象引用赋值。另外,VBA中的Long类型是.NET中的Int32类型。
https://stackoverflow.com/questions/33419199
复制相似问题