【发布时间】:2018-08-01 13:51:14
【问题描述】:
我正在尝试编写一个宏,以便在打开 Word 文档时将子文档添加到 Word 文档的末尾。有问题的文档中已经有一些文本,所以在运行宏之前,我想将光标移动到文档的末尾。我可以使用以下代码实现此目的:Selection.EndKey Unit:=wdStory 当我在打开文档后运行宏 时效果很好,但如果我在使用 Sub 打开文档后立即运行宏:
Private Sub Document_Open()
Selection.EndKey Unit:=wdStory
'Add subdocuments based on user input to a form
'(See edit below)
End Sub
在 ThisDocument 对象中,子文档被添加到文档的开头。这可能是因为光标还没有出现所以Selection 还没有“存在”。
如何在文档打开时运行宏,但在文档末尾添加子文档?
我已经尝试先写一个空格以使光标产生但没有变化...
也欢迎任何关于替代方法的建议。
编辑: ThisDocument中的这段代码:
Private Sub Document_Open()
CreateWorkbook.Show
End Sub
调用表单CreateWorkbook,用一个按钮点击子:
Private Sub GenerateButton_Click()
Dim i As Integer
Dim rng As Word.Range
Set rng = ActiveDocument.Content
rng.Collapse wdCollapseEnd
'ModulesListBox is a user input box that is a list of paths to the subdocuments
For i = 0 To ModulesListBox.ListCount - 1
docpath = ModulesListBox.List(i)
rng.Subdocuments.AddFromFile docpath
Next i
End Sub
【问题讨论】:
-
尝试在该行之前添加
Selection.Wholestory。 -
@dwirony 不幸的是没有变化-谢谢你的建议。