我正在用C#编写Office加载项。
我正在尝试使用WdBuiltInProperty
从Word文档中获取没有空格的字符数。但是,转换为long
不起作用。
错误消息是:
Type" 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 ^^^
}
}
问题:
和/或
答案 0 :(得分:0)
使用BuiltInDocumentProperties
具有Characters
属性的Count
,而不是long c = doc.Characters.Count;
。
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
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.characters.count.aspx
编辑(来自VBA example):
library(data.table)
setDT(mydata)
res <- mydata[,list(name=paste(name,collapse=", "),
definition=definition[.N]),
by=list(id, description,type)]
res
id description type name definition
1: gene1 protein1 coding nucleus, binding, ribosome blabla 3
2: gene2 protein2 coding transcription factor, regulation of transcription, DNA, sequence-specific binding blabla 7
3: gene3 protein3 coding hydrolase blabla 8
4: gene4 protein4 coding transporter, ion transport blabla 10
答案 1 :(得分:0)
int c = doc.BuiltInDocumentProperties[Word.WdBuiltInProperty.wdPropertyCharacters].Value;
BuiltInDocumentProperties[]
的结果为Office.DocumentProperty
(可以通过检查Word VBA编辑器中的“本地”窗口来找到),并且它的默认.Value
属性包含数字。
它在VBA中没有.Value
的情况下可以工作,因为VBA具有不同的默认值分配(Let
),并且使用Set
进行了单独的和对象引用分配。此外,在VBA中,Long
类型在.NET中是Int32