[英]Copy an image from a Word document into an Excel cell
我想將word文檔中的圖片復制到Excel的單元格中,但是每次我嘗試粘貼圖片時都會得到一個“\”。
有人能幫助我嗎?
在 VBA 中是否有簡單的方法來執行此操作?
我使用選擇在兩章之間搜索(選擇完美,但副本沒有。)
我的代碼如下:
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
您的代碼有幾個問題。
變量MySheet
沒有聲明,也沒有指向任何東西。 因此,您的代碼無法編譯。
盡管您的問題提到使用復制和粘貼,但您的代碼不會復制或粘貼任何內容。
單元格的.Value
不能是圖片。
在使用 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.