[英]Copy an image from a Word document into an Excel cell
I want to copy a picture from a word document into a cell in Excel, but every time I try to paste the picture I get an "\".我想将word文档中的图片复制到Excel的单元格中,但是每次我尝试粘贴图片时都会得到一个“\”。
Can someone help me please?有人能帮助我吗?
Is there a simple way to do this operation in VBA?在 VBA 中是否有简单的方法来执行此操作?
I use selection to search between two chapters (The selection works perfectly but the copy does not.)我使用选择在两章之间搜索(选择完美,但副本没有。)
My code is as follows:我的代码如下:
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("C:\test.docx")
Dim r1 As Long
wrdApp.Selection.WholeStory
wrdApp.Selection.Find.ClearFormatting
With wrdApp.Selection.Find
.Text = "ABCD"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
r1 = wrdApp.Selection.Range.End
wrdApp.Selection.Find.Text = "BCDE"
If wrdApp.Selection.Find.Execute Then
wrdApp.Selection.Collapse wdCollapseStart
Else
wrdApp.Selection.WholeStory
wrdApp.Selection.Collapse wdCollapseEnd
End If
wrdDoc.Range(r1, wrdApp.Selection.Start).Select
With wrdApp.Selection
MySheet.Range("B3").Value = .InlineShapes(1)
End With
There are several problems with your code.您的代码有几个问题。
The variable MySheet
isn't declared and doesn't point to anything.变量
MySheet
没有声明,也没有指向任何东西。 As a result your code doesn't compile.因此,您的代码无法编译。
Although your question mentions using copy and paste your code doesn't copy or paste anything.尽管您的问题提到使用复制和粘贴,但您的代码不会复制或粘贴任何内容。
The .Value
of a cell cannot be a picture.单元格的
.Value
不能是图片。
When using VBA, whether in Excel, PowerPoint or Word, it is best to avoid using the Selection
object and use Range
instead.在使用 VBA 时,无论是在 Excel、PowerPoint 还是 Word 中,最好避免使用
Selection
object,而是使用Range
。
Dim wrdApp As Word.Application Dim wrdDoc As Word.Document Set wrdApp = CreateObject("Word.Application") wrdApp.Visible = True Set wrdDoc = wrdApp.Documents.Open("C:\test.docx") Dim r1 As Word.Range Set r1 = ActiveDocument.Range With wrdDoc.Range With.Find.ClearFormatting.Replacement.ClearFormatting.Text = "ABCD".Format = False.Forward = True.Wrap = wdFindStop.MatchWildcards = False End With If.Find.Execute Then r1.Start =.End.Find.Text = "BCDE" If.Find.Execute Then r1.End =.Start End With r1.InlineShapes(1).Range.Copy ThisWorkbook.Sheets(1).Range("B3").PasteSpecial wrdDoc.Close False wrdApp.Quit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.