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

Vba 如何将文本框从MS Word复制/粘贴到Powerpoint幻灯片?,vba,ms-word,powerpoint,powerpoint-2010,Vba,Ms Word,Powerpoint,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 & A

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

我注意到您的代码中有几个错误:

  • 您尚未创建PowerPoint.Application的实例
  • 您已将
    pptPres
    声明为字符串,但可能应将
    声明为对象
    以表示Powerpoint.Presentation对象
  • 您不向
    pptPres
通过形状的
.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中指定形状名称来实现这一点

我以前见过
commandbar.ExecuteMso
方法,但与许多其他方法相比,它的文档记录得不是很好。
Application.commandbar
没有提到
ExecuteMso
方法,我在这里找到了一些相关信息:

在没有特定命令的对象模型的情况下,此方法非常有用。用于内置按钮、切换按钮和拆分按钮的控件

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


这太棒了!非常感谢。我试图将内容直接粘贴到幻灯片上的文本框中,而不是直接粘贴到幻灯片上。你也能帮忙吗?我确实添加了PowerPoint 14.0参考库,因此我现在可以访问这些方法。在PowerPoint中命名形状/文本框时,是通过“格式”选项卡中的选择窗格完成的吗?是的,我正要提到这一点。如果您不知道名称,请使用选择窗格来查看名称(如果需要的话,请编辑它)。BTW,更新我的答案以粘贴到PowerPoint中的特定形状:请让我知道这是否适用于您,如果是,请考虑接受答案,以便将来类似Q的其他人从中受益。