總體目標是在Excel中採用RTF格式的文本並將其轉換爲HTML。到目前爲止,我一直無法讓Excel以合理的方式做出迴應,所以使用Word進行嘗試。我能夠在沒有問題的Word中進行轉換,因此希望通過將單元格複製出Excel,粘貼到Word中,然後運行代碼將格式轉換爲我想要的方式來自動執行該過程。從Excel到Word的VBA粘貼結束分
我遇到了一個問題,當我從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,但它總是有相同的結果。
有沒有人有任何想法,這是怎麼回事,或如何阻止它總是在粘貼後結束。
編輯:我已經測試了這一點,它的工作在Office 2010中它不會在辦公室工作的2013年
您在Excel中運行'webtext',正確嗎?當代碼*跳轉到'End Sub'時,你的任務是否完成?我不得不承認我很難在這裏理解你的問題 – 2014-02-27 11:21:05
我已經添加了幾個「測試」點來說明問題發生的位置以及它的影響(即:粘貼後我無法再運行任何代碼) 。粘貼本身是成功的。 – Andy