如何将文本框从MS Word复制/粘贴到Powerpoint幻灯片?

时间:2014-07-08 18:36:41

标签: vba ms-word word-vba powerpoint-vba powerpoint-2010

我目前正在创建一个文档,该文档将从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

1 个答案:

答案 0 :(得分:2)

我发现代码中有一些错误:

  • 您尚未创建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