从Excel到Word的VBA粘贴结束小组

总体目标是在Excel中采用RTF格式的文本并将其转换为HTML。 到目前为止,我还没有能够让Excel以合理的方式做出回应,所以使用Word进行尝试。 我能够在Word中转换没有问题,所以想通过复制单元格Excel,粘贴到Word,然后运行代码将格式转换为我想要的方式自动化的过程。

我遇到了一个问题,当我从Excel VBA粘贴到Word中时,粘贴成功,但它跳转到End Sub

 Sub webtext(r As String) Range(r).Copy Dim wordApp As Word.Application Dim wordDoc As Word.Document Set wordApp = CreateObject("word.Application") Set wordDoc = wordApp.Documents.Add wordApp.Visible = True wordApp.Activate wordApp.Selection.PasteSpecial 'Any code here is missed out z = MsgBox("tada") 'this will never trigger x=5 'this will never set With wordDoc 'Operations on the text pasted would happen here 'but vba never gets here. End With 'Code jumps to here End Sub 

我已经尝试使用wordDoc和wordApp,并粘贴和pastespecial,但它总是有相同的结果。

有没有人有任何想法,这是怎么回事,或如何阻止它总是在粘贴后结束。

编辑:我已经testing了这一点,它在Office 2010上工作。它不适用于Office 2013。

它对我来说工作得很好

但build议你尝试这些调整,看看是否有效( AppActivate是多余的)。

 Sub StartIt() Call webtext("a1:a10") End Sub 

主要

 Sub webtext(r As String) Range(r).Copy Dim wordApp As Word.Application Dim wordDoc As Word.Document Set wordApp = New Word.Application Set wordDoc = wordApp.Documents.Add wordApp.Visible = True With wordDoc .ActiveWindow.Selection.Paste End With End Sub 

似乎被解决了。 VBAdebugging程序不能理解到达WordApp部分后发生了什么,因此代码似乎跳转到该部分的末尾。

此外,似乎任何单词的变化都应该With wordDoc部分中完成,因为有时(或者为什么在第一次丢失消息框时为什么没有弄清楚)之间的代码有点奇怪。

感谢@ brettdj帮助testing和道歉我的错误理解发生了什么事情。