【问题标题】:How to Copy/Paste text box from MS Word to Powerpoint slide?如何将文本框从 MS Word 复制/粘贴到 Powerpoint 幻灯片?
【发布时间】:2014-08-29 15:07:08
【问题描述】:

我目前正在创建一个文档,该文档将从 MS Word 文档中获取格式和文本,并将其粘贴到 PowerPoint 模板的文本框中。我已经四处寻找,看看如何做到这一点,但没有找到太多。任何帮助将不胜感激!

Public Sub CommandButton1_Click()
Dim pptApp As Object
Dim pptPres As String
Dim folderPath, file As String

    folderPath = ActiveDocument.Path & Application.PathSeparator
    file = "Huntington_Template.pptx"
    pptApp.Visible = True
    pptApp.presentations.Open (folderPath & file)

    ActiveDocument.Bookmarks("Update_Image").Range.Copy 'text box to copy in MS WS
    'not sure what to do next?

End Sub

【问题讨论】:

    标签: vba ms-word powerpoint powerpoint-2010


    【解决方案1】:

    我注意到您的代码中有一些错误:

    • 您尚未创建 PowerPoint.Application 的实例
    • 您已将 pptPres 声明为字符串,但可能应该是 As Object 来表示 Powerpoint.Presentation 对象
    • 您没有对pptPres 进行任何分配

    Shape 的.Name 会更容易做到这一点,但我认为这会奏效。除了上述变量之外,我还进行了一些其他更改以声明更多变量。

    Sub Test()
    Dim pptApp As Object    'PowerPoint.Application
    Dim pptPres As Object   'PowerPoint.Presentation
    Dim folderPath As String, file As String
    Dim bk As Bookmark
    Dim doc As Document
    Dim wdRange As Range
    Dim shpTextBox as Object 'PowerPoint.Shape
    
        '## As a matter of prefernce I use variable rather than "ActiveDocument"
        Set doc = ActiveDocument
    
        '## Use a variable for the bookmark
        Set bk = doc.Bookmarks("Update_Image")
    
        '## Assign to the pptApp Application Object
        Set pptApp = CreateObject("PowerPoint.Application")
    
        folderPath = doc.Path & Application.PathSeparator
        file = "Huntington_Template.pptx"
    
        pptApp.Visible = True
        '## assign to the pptPres Presentation Object
        Set pptPres = pptApp.presentations.Open(folderPath & file)
    
        '## Select the bookmark so we can copy it
        bk.Select
    
        '## Copy it
        Selection.Copy
    
    
        'Note: ensure you are at the correct slide location
    
        '## Assign to the shpTextBox & select it:
        Set shpTextBox = pptPres.Slides(1).Shapes("Text Box 2")
        shpTextBox.Select
    
        '## Paste in to PPT
        pptApp.CommandBars.ExecuteMso "PasteSourceFormatting"
    
    
    End Sub
    

    注意 这会直接粘贴到幻灯片中,如果您需要将其放在 PowerPoint 幻灯片中的特定文本框/形状中,请告诉我。我相当确定可以通过在 PowerPoint/etc 中指定形状名称来完成。

    我以前见过CommandBars.ExecuteMso 方法,但与许多其他方法相比,它的文档记录并不充分。 Application.CommandBars property reference 没有提到 ExecuteMso 方法,我在这里找到了一些信息:

    http://msdn.microsoft.com/en-us/library/office/ff862419(v=office.15).aspx

    此方法在特定命令没有对象模型的情况下很有用。适用于内置按钮、toggleButtons 和 splitButtons 的控件。

    您需要一个 idMso 参数列表来探索,这是一个相当大的可下载文件的一部分,我相信它适用于 Office 2013:

    http://www.microsoft.com/en-us/download/details.aspx?id=727

    【讨论】:

    • 这太棒了!谢谢!我试图将内容直接粘贴到幻灯片上的文本框中,而不是直接粘贴到幻灯片上。你也可以帮忙吗?我确实添加了 PowerPoint 14.0 参考库,所以我现在可以访问这些方法。
    • 在 PowerPoint 中命名形状/文本框时,是否通过“格式”选项卡中的“选择窗格”完成?
    • 是的,我正要提到这一点。如果您不知道名称,请使用“选择”窗格查看名称(并根据需要进行编辑)。
    • 顺便说一句,更新了我上面的答案以粘贴到 PowerPoint 中的特定形状:)
    • 如果这对你有用,请告诉我,如果是这样,请考虑接受答案,以便将来有类似 Q 的其他人可以从中受益。
    猜你喜欢
    • 2020-07-09
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 2015-05-10
    相关资源
    最近更新 更多