且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-14 09:24:00

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

I notice a few errors in your code:

  • 您尚未创建PowerPoint.Application的实例
  • 您已将pptPres声明为String,但可能应为As Object来表示Powerpoint.Presentation对象
  • 您未对pptPres
  • 进行任何分配
  • You haven't created an instance of PowerPoint.Application
  • You have declared pptPres as String but probably should be As Object to represent a Powerpoint.Presentation object
  • You do not make any assignment to pptPres

通过Shape的.Name会更容易做到,但是我认为这会起作用.除了上面的那些变量之外,我还进行了其他一些更改,以声明更多的变量.

This would be easier to do by the Shape's .Name but I think this will work. I have made some other changes to declare some more variables in addition to those above.

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中指定形状名称来完成.

NOTE This pastes directly to the slide, if you need to put it in a specific textbox/shape in the PowerPoint slide, let me know. I am fairly certain that could be done by specifying the shape name in PowerPoint/etc.

我以前见过CommandBars.ExecuteMso方法,但是与许多其他方法相比,它的文档记录不是很好. Application.CommandBars 属性参考几乎没有提及ExecuteMso方法,我在这里找到了一些信息:

I've seen the CommandBars.ExecuteMso method before but it is not very well-documented compared to many other methods. The Application.CommandBars property reference has nary a mention of the ExecuteMso method, which I found some information about here:

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

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

This method is useful in cases where there is no object model for a particular command. Works on controls that are built-in buttons, toggleButtons and splitButtons.

您需要浏览 idMso 参数列表,这些参数是一个相当大的可下载文件的一部分,我认为这是Office 2013的最新版本.

You'll need a list of idMso parameters to explore, which come as part of a rather large downloadable file, current for Office 2013 I believe:

http://www.microsoft.com/zh-我们/download/details.aspx?id=727