目前我正在致力于改进 VB.net 中翻译的 Word VBA 代码。该程序可处理 200 多个 Word 文档。至此,程序运行正常。我在每个文档的开头插入包含图像和文本(来自模板,
wTemp
)的表格时遇到问题(wDoc
)。现在的方式(在 VBA 中)使用剪贴板 - 复制 wTemp.Table(1) 并将其粘贴到 wDoc.range(0,0)
。
但是,这至少存在两个问题:
wTemp
需要一直打开(不是主要问题)我想要实现的目标在以下(伪)代码中描述:
open wTemp(readonly)
dim wTable as word.table = wTemp.tables(1) ' the table I need is the first one
close wTemp(false) ' preferably!
for each wDoc in someCollection
open wDoc
wDoc.Application.Selection.HomeKey(Word.WdUnits.wdStory, Word.WdMovementType.wdMove)
' here comes the copy/paste code that works, but I want to replace it with something like this
insert wTable at current selection point
wDoc.save
wDoc.close(false)
next wDoc
wTemp.close(false) ' if I have to keep it open for the duration of the loop
我知道
tables.add()
方法允许您插入包含行/列的表格,但我不需要构造该表,因为它已经在 wTemp 中。
你可以尝试使用FormattedText property代替剪贴板方法,参考我的回答:
Rem implement without the clipboard
myDoc.Range(tbl.Cell(tbl.Rows.count, 1).Range.Start, tbl.Cell(tbl.Rows.count, 1).Range.Start).FormattedText = tbl.Rows(2).Range.FormattedText
所以,先试试这个!试一试!!
open wTemp(readonly)
dim wTable as word.table = wTemp.tables(1) ' the table I need is the first one
close wTemp(false) ' preferably!
for each wDoc in someCollection
open wDoc
wDoc.Application.Selection.HomeKey(Word.WdUnits.wdStory, Word.WdMovementType.wdMove)
' here comes the copy/paste code that works, but I want to replace it with something like this
'insert wTable at current selection point
Selection.Range.FormattedText= wTable.Range.FormattedText'TRY THIS FIRST ! give it a shot !!
rem And rememer : Try to avoid using Selection objects, but use Range objects. OK?
wDoc.save
wDoc.close(false)
next wDoc
wTemp.close(false) ' if I have to keep it open for the duration of the loop