【发布时间】:2013-10-04 11:19:26
【问题描述】:
感谢这里的某个人,我终于有了一个工作循环......几乎:) 它大部分时间都有效,但在某些情况下,会出现此错误。
代码:
procedure TLetters.ReplaceDate(NewDate: String);
var I : Integer;
ARegion : OleVariant;
FieldType : Integer;
FieldCount : Integer;
begin
FieldCount := WordApp.ActiveDocument.Fields.Count;
For I := 1 to FieldCount do
Begin
FieldType := WordApp.ActiveDocument.Fields.Item( I ).type;
If FieldType IN [ 31,32 ] Then
Begin
ARegion := WordApp.ActiveDocument.Fields.Item( I ).Code;
WordApp.ActiveDocument.Fields.Item( I ).Cut;
ARegion.Text := NewDate;
End;
End;
end;
上述代码的问题在于,有时 Count 会返回 2,但是当我尝试检查第二个条目时,它会在主题中输出异常。
难道只是我必须做一个 Count -1 而不是 Count 吗?
【问题讨论】:
-
是的,AFAIK,索引从 0 到 Count-1。
-
我确信索引从 1 开始。这是肯定的。但它可能会停在 Count-1,我不知道。
-
你没有告诉我们实际的错误。
标签: delphi ms-word ole-automation