【问题标题】:VBA how to copy images / inline shapes from Word to powerpointVBA如何将图像/内联形状从Word复制到PowerPoint
【发布时间】:2016-03-07 11:41:19
【问题描述】:

我正在尝试编写一个宏来查找和复制 Word 文档中内联的所有图形/图像,并将它们粘贴到新 powerpoint 中的单个幻灯片中。但是,当我遇到多个运行时错误时。这是整个代码。

Sub wordtoppt()
'This macro copies all pictures out of a word document of your choice and into a new powerpoint presentation.

'Two reference libraries need to be open - Word and Powerpoint. Go Tools > References, and tick the relevant box.


Dim wdApp As Word.Application   'Set up word and powerpoint objects
Dim wdDoc As Word.Document

Dim pptApp As PowerPoint.Application
Dim pptShw As PowerPoint.Presentation
Dim pptChart As PowerPoint.Shape
Dim pptSld As PowerPoint.Slide

On Error GoTo 0

Dim wcount As Integer       'Number of open word documents
Dim doclist() As String     'Collects the names of open word documents
Dim desc As String          'inputbox text
Dim chosendoc As Integer    'stores the index number of your selected word document
Dim ccount As Integer       'number of shapes in the word document

Dim wellpasted As Integer   'Counts the number of shapes that have successfully been pasted into powerpoint.

Application.ScreenUpdating = False

'Establishes link with word.
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
On Error GoTo 0
If wdApp Is Nothing Then 'Error message if Word is not open
    MsgBox "Error: Word is not open." & Chr(10) & Chr(10) & "Is word actually open? This is a bug."
    Exit Sub
End If

'Counts the number of word documents open
wcount = CInt(wdApp.Documents.Count)
ReDim doclist(wcount) 'resizes string array of word documents
If wcount = 0 Then 'Error message if Word is open, but there are no documents open
    MsgBox "There are no word documents open!" & Chr(10) & "Open a word document and try again"
    Exit Sub
End If

'text for input box
desc = "Which document would you like to extract the graphs from?" & Chr(10) & Chr(10) & "Type the number in the box (one number only)." & Chr(10) & Chr(10)

'input boxes for selection of word document
If wcount = 1 Then 'if only one document open
   myinput = MsgBox("Do you want to paste graphs from " & wdApp.Documents(1).Name & "?", vbYesNo, "From Release Note to Powerpoint")
    If myinput = vbYes Then
        chosendoc = 1
    Else
        Exit Sub
    End If
Else
    For i = 1 To wcount 'multiple documents open
        doclist(i) = wdApp.Documents(i).Name
        desc = desc & i & ": " & doclist(i) & Chr(10)
    Next
    myinput = InputBox(desc, "From Release Note to Powerpoint")

    If IsNumeric(myinput) And myinput <= wcount Then 'Error handling - if cancel is clicked, or anything other than a number is typed into the input box.
        chosendoc = CInt(myinput)
    Else
        If myinput = "" Then 'clicking cancel, or leaving input box blank
            MsgBox "You didn't enter anything!"
            Exit Sub
        Else 'if you type a short novel
            MsgBox "You didn't enter a valid number!" & Chr(10) & "(Your response was " & myinput & ")"
            Exit Sub
        End If
    End If
End If

'Error handling, for chart-free word documents.
If wdApp.Documents(chosendoc).InlineShapes.Count = 0 Then
    MsgBox "There are no charts in this Word Document!"
    Exit Sub
End If


'Opens a new powerpoint presentation
Set pptApp = CreateObject("PowerPoint.Application")
Set pptShw = pptApp.Presentations.Add

'PowerPoint.Application
'Sets up slide dimensions
Dim sldwidth As Integer
Dim sldheight As Integer
sldwidth = pptShw.PageSetup.SlideWidth
sldheight = pptShw.PageSetup.SlideHeight



wellpasted = 0


Dim shapecount As Integer 'Number of shapes in the word document
shapecount = wdApp.Documents(chosendoc).InlineShapes.Count

For j = 1 To shapecount 'Adds in the correct number of slides into the powerpoint presentation
Set pptSld = pptShw.Slides.Add(pptShw.Slides.Count + 1, ppLayoutBlank)
Next

For j = 1 To shapecount 'loops through all shapes in the document

On Error GoTo Skiptheloop 'sometimes some objects don't paste. This is a way to skip over them.

'Application.Wait Now + (1 / 86400)

   wdApp.Documents(chosendoc).InlineShapes(j).Range.Copy 'copies chart

   Set pptSld = pptShw.Slides(j)

   pptSld.Shapes.Paste 'pastes chart

'Application.CutCopyMode = False

   With pptSld.Shapes(1)     'resizes and aligns shapes
        .LockAspectRatio = msoTrue 'Currently sets charts to the height of the slide. Alternatively can scale to 100%
        .Height = sldheight
        .Left = (sldwidth / 2) - (.Width / 2)
        .Top = (sldheight / 2) - (.Height / 2)
   End With
   wellpasted = wellpasted + 1 'if the chart was pasted successfully, increment by 1.

Skiptheloop:
Next


On Error GoTo 0
If (shapecount - wellpasted) <> 0 Then 'produces a message box if some shapes did not paste successfully.
    MsgBox CStr(shapecount - wellpasted) & " (of " & CStr(shapecount) & ") shapes were not pasted. Best that you check all the graphs are in."
End If

Application.ScreenUpdating = True
pptApp.Activate 'brings powerpoint to the front of the screen


Exit Sub

End Sub

pptSld.shapes.paste 行我得到错误剪贴板为空或无法粘贴。

有什么想法吗?

【问题讨论】:

  • 这段代码在哪里运行?如果您在 Range.Copy 之后的代码中放置一个断点,然后单击,比如说,另一个文档粘贴了一些东西?如果没有,请复制 Range.Copy 行并将其粘贴到上方,然后将 Copy 更改为 Select。运行该行,再次停止并检查是否实际选择了您期望的内容。尝试手动复制,然后在 pptSld.Shapes.Paste 再次启动代码,看看是否有效。
  • 请注意,最好将 Word.Document 对象变暗并为其分配 wdApp.Documents(chosendoc),然后在代码中使用它而不是依赖 Word 不更改文档顺序...
  • @CindyMeister 谢谢你的建议。我都试过了,但仍然遇到同样的问题。当我逐步完成时,它似乎可以很好地选择每个对象。
  • 不要使用剪贴板。很乱。

标签: vba ms-word powerpoint


【解决方案1】:

我正在为我的工作使用简单的解决方案,分为两个部分

1) 从word文件中提取所有图片 这可以通过两种方式完成。

a. 保存为 html,这将创建文件夹 filenam_files,该文件夹将保存 .png 格式中的所有图像。 diff 格式中可能存在重复的图像,但 .png 将是唯一的。

b. 将单词的文件名从 file.docx 更改为 file.docx.zip 您可以在file.docx\word\media 获取图像 此方法不会出现重复的图像。

2) 导入 powerpoint 中的所有图片。

1)

由于您已经手动打开了文档,您可以手动执行更多步骤或录制如下所示的宏。

Sub exportimages()
ChangeFileOpenDirectory "D:\temp\"
ActiveDocument.SaveAs2 FileName:="data.html", FileFormat:=wdFormatHTML, _
    LockComments:=False, passWord:="", AddToRecentFiles:=True, WritePassword _
    :="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
    SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
    False, CompatibilityMode:=0
End Sub

2)

关闭word文档。 打开电源点并粘贴此

Sub ImportABunch()

Dim strTemp As String
Dim strPath As String
Dim strFileSpec As String
Dim oSld As Slide
Dim oPic As Shape


strPath = "D:\temp\data_files\"
strFileSpec = "*.png" 'if you are using mehtod **a.** to extract the images.
'strFileSpec = "*.*" 'if you are using mehtod **b.** to extract the images.

strTemp = Dir(strPath & strFileSpec)

Do While strTemp <> ""
    Set oSld = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutBlank)
    Set oPic = oSld.Shapes.AddPicture(FileName:=strPath & strTemp, _
    LinkToFile:=msoFalse, _
    SaveWithDocument:=msoTrue, _
    Left:=0, _
    Top:=0, _
    Width:=-1, _
    Height:=-1)
    strTemp = Dir
Loop

End Sub

您可以编写 vbscript 将这两个步骤组合在一起。我不知道该怎么做。你可以google一下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 2017-07-28
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多