简体   繁体   English

VBA从Excel粘贴到Word结尾子

[英]VBA paste from Excel to Word ends sub

The overall aim is to take RTF formatted text in Excel and convert it to HTML. 总体目标是在Excel中采用RTF格式的文本并将其转换为HTML。 I have been unable thus far to get Excel to respond in a sensible way so tried using Word. 到目前为止,我一直无法使Excel以明智的方式做出响应,因此尝试使用Word。 I am able to convert in Word without issue so want to automate the process by copying the cell out Excel, pasting into Word and then running code to convert the formatting into the way I want. 我能够在Word中进行转换而不会出现问题,因此希望通过将单元格复制到Excel中,粘贴到Word中,然后运行代码以将格式转换为所需的方式来自动化该过程。

I am running into an issue where when I paste into Word from Excel VBA, the paste is successful but then it jumps to End Sub . 我遇到一个问题,当我从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

I have tried using both wordDoc and wordApp, and both paste and pastespecial but it always has the same result. 我尝试同时使用wordDoc和wordApp以及粘贴和pastespecial,但始终具有相同的结果。

Does anyone have any ideas as to what is going on here or how to stop it from always ending after the paste. 是否有人对粘贴后发生的事情或如何阻止其始终结束有任何想法?

EDIT: I have tested this and it worked on Office 2010. It does not work on Office 2013. 编辑:我已经对此进行了测试,并且它可以在Office 2010上工作。它不能在Office 2013上工作。

It worked fine for me as is 它对我来说很好

But suggest you try these tweaks to see if this works ( AppActivate is redundant). 但是建议您尝试这些调整,看看是否AppActivateAppActivate是多余的)。

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

main 主要

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

Seems to be solved. 似乎有待解决。 VBA debugger cannot understand what is going on after it hits the WordApp section and so the code appears to jump to the end of the section. VBA调试器在点击WordApp部分后无法理解发生了什么,因此代码似乎跳到了该部分的末尾。

Additionally, it seems that any word alterations should be done within the With wordDoc section as it can be a little odd at hitting code properly in between sometimes (though never figured out why was missing message box first time round). 此外,似乎任何单词更改都应在With wordDoc部分中进行,因为有时在两者之间正确命中代码可能有些奇怪(尽管从未弄清楚为什么第一次缺少消息框)。

Thanks @brettdj for help with testing and apologies for my error in understanding what was going on. 感谢@brettdj在测试和道歉方面所提供的帮助,以帮助我理解所发生的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM