2017-07-06 38 views
0

我想將文檔doc上的圖像導出到本地驅動器如何使用vba從excel中執行此操作。如何將圖像從Word文檔導出到本地驅動器

Sub gen_Files() 

Dim WdApp As Word.Application, Doc As Word.Document, fPath As String 
Dim i As Long 

fPath = ThisWorkbook.Path & Application.PathSeparator & "Test.docx" 
If fPath = "" Or Dir(fPath) = "" Then MsgBox "Invalid file path.": Exit Sub 

Set WdApp = New Word.Application 
WdApp.Visible = True 
Set Doc = WdApp.Documents.Open(fPath) 
Doc.SaveAs2 ThisWorkbook.Path & "\New.docx", FileFormat:=12 

For i = 1 To Doc.InlineShapes.Count 
    'Doc.InlineShapes(i).Range.ExportAsFixedFormat(ThisWorkbook.Path & Application.PathSeparator & i & ".jpg",wdExportFormatXPS,False,,,,,,,,,,) 
Next i 

'Save the file and done 
Doc.Save 
Doc.Close 
WdApp.Quit 

End Sub 

回答

1

代碼會是這樣的。

Sub gen_Files() 

Dim WdApp As Word.Application, Doc As Word.Document, fPath As String 
Dim i As Long 
Dim cht As Chart, obj As ChartObject 
Dim Ws As Worksheet 
Dim myFn As String 
Dim shp As InlineShape 

Set Ws = ActiveSheet 

fPath = ThisWorkbook.Path & Application.PathSeparator & "Test.docx" 
If fPath = "" Or Dir(fPath) = "" Then MsgBox "Invalid file path.": Exit Sub 

Set WdApp = New Word.Application 
WdApp.Visible = True 
Set Doc = WdApp.Documents.Open(fPath) 
Doc.SaveAs2 ThisWorkbook.Path & "\New.docx", FileFormat:=12 

For i = 1 To Doc.InlineShapes.Count 
    Set shp = Doc.InlineShapes(i) 
    shp.Range.CopyAsPicture 
    Set obj = Ws.ChartObjects.Add(Range("i1").Left, 0, shp.Width, shp.Height) 
    myFn = ThisWorkbook.Path & Application.PathSeparator & i & ".jpg" 
    With obj.Chart 
     .Paste 
     .Export myFn 
    End With 
    obj.Delete 
Next i 

'Save the file and done 
Doc.Save 
Doc.Close 
WdApp.Quit 

End Sub 
+0

感謝它確實導出圖像,但它們都是空白的白色圖像 – Rohan

+0

@Rohan:在我的測試中,運行良好。 –

相關問題