Excel VBA 从 word 中复制并粘贴选定的文本 [英] Excel VBA copy and paste selected text from word
问题描述
我想要在我的 Excel 工作表上有一个宏按钮:
我将突出显示(选择)word 文档中的一些文本
我按下按钮
然后将文本复制并粘贴到我的 Excel 表格中的指定单元格中
我试过了:
子粘贴()objword.selection.copy 范围(B2")结束子
首先我不知道 objword
是否是正确的语法,其次每次我在 word 中选择文本时,如果没有取消文本选择,我无法点击 excel,那我该怎么办这?我必须在word中创建宏吗?
谢谢
在 Excel 中运行的以下代码(需要引用 Microsoft Word xx 对象库)将抓取 Word 中的当前选择并将文本复制到 A1 中:
>
子粘贴()Dim oWd as Word.ApplicationSet oWd = GetObject(, "Word.Application")ActiveSheet.Cells(1, 1) = oWd.Selection设置 oWd = 无结束子
此外,以下是等价的相反,即可以在 Word 中运行的代码,可以在 Word 中进行选择并将其发送到 Excel:
子粘贴()Dim oXL 作为 Excel.ApplicationSet oXL = GetObject(, "Excel.Application")oXL.ActiveSheet.Cells(1, 1) = Selection.Text设置 oXL = 无结束子
这个需要对 Microsoft Excel xx 对象库的引用.
I want a macro button on my Excel sheet that:
I will highlight (select) some text in a word document
I press the button
the text is then copied and pasted into a specified cell in my excel sheet
I have tried:
sub paste()
objword.selection.copy range("B2")
End sub
Firstly I don't know whether objword
is the right syntax, and secondly everytime I select text in word, I cannot click on excel without the text deselecting, so how will I be able to do this? Do I have to create the macro in word?
Thanks
The following code (requiring a reference to Microsoft Word xx Object Library) running in Excel will grab the current selection in Word and copy the text into A1:
Sub paste()
Dim oWd As Word.Application
Set oWd = GetObject(, "Word.Application")
ActiveSheet.Cells(1, 1) = oWd.Selection
Set oWd = Nothing
End Sub
Also, the following is the equivalent opposite, i.e. code that can run in Word that takes the selection in Word and sends it to Excel:
Sub paste()
Dim oXL As Excel.Application
Set oXL = GetObject(, "Excel.Application")
oXL.ActiveSheet.Cells(1, 1) = Selection.Text
Set oXL = Nothing
End Sub
This one requires a reference to Microsoft Excel xx Object Library.
这篇关于Excel VBA 从 word 中复制并粘贴选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!