简体   繁体   English

如何使用Excel VBA和userform编辑嵌入在Excel中的PowerPoint演示文稿

[英]How can I edit a PowerPoint presentation embedded in Excel using Excel VBA and userform

I am building a database with information on a large collection of product designs. 我正在建立一个包含大量产品设计信息的数据库。 I chose to use an Excel file with userform input for the design details so as to easily filter by detail, then select a link to the corresponding page that contains an embedded PowerPoint with photos and design notes. 我选择使用带有用户窗体输入的Excel文件作为设计详细信息,以便轻松按详细信息进行过滤,然后选择一个指向相应页面的链接,该页面包含带有照片和设计说明的嵌入式PowerPoint。 At the moment I have a template that copies into a new tab, renames and creates a link to the tab in a directory based on text box input of part number. 目前,我有一个模板,可根据零件号的文本框输入复制到新选项卡,重命名并在目录中创建指向该选项卡的链接。 I might be asking too much but I'd also like the userform to add text to preexisting text boxes within the PowerPoint presentation. 我可能要问的太多了,但是我也希望用户窗体将文本添加到PowerPoint演示文稿中的预先存在的文本框中。 Everything up to this point works rather well. 到目前为止,一切工作都很好。

I found a similar question and tried my hand at coding several times. 我发现了一个类似的问题,尝试了几次编码。

Editing Embedded PowerPoint from Excel VBA 从Excel VBA编辑嵌入式PowerPoint

It was helpful in understanding but it didn't work for me in this context: 这有助于理解,但在这种情况下对我不起作用:

Private Sub cmdAddSlide_Click()

    template = "Slide Template"

    'Hide the sheet
    ufrmAddSlide.Hide

    'Copy the template to create a new sheet.
    Sheets(template).Select
    Sheets(template).Copy After:=Sheets(Sheets.Count)

    'Make the sheet visible in case the template is hidden.
    ActiveSheet.Visible = xlSheetVisible

    'Rename the sheet.
    ActiveSheet.Name = txtPartNumber

    'Add data to powerpoint object.
    Worksheets(Me.txtPartNumber.Value).Shapes("Object 1").Select 
    Selection.Verb Verb:=xlOpen
    Dim p As PowerPoint.Presentation
    Set p = Selection.Object
    ActivePresentation.Slides(1).Shapes("operationaltext1").TextFrame.TextRange.Text = Me.txtPartNumber.Value
    [a1].Select

    'Bring main sheet back to front if necessary.
    If chkBringToFront = False Then
        Sheets("Directory").Select
    End If

    'Copy input values to sheet.
    Dim lRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("Directory")
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

    With ws    
        'Create a link to the part design page with the part number text.
        ActiveSheet.Hyperlinks.Add Anchor:=.Cells(lRow, 1), Address:="", 
        SubAddress:=Me.txtPartNumber.Value & "!A1", 
        TextToDisplay:=Me.txtPartNumber.Value
        .Cells(lRow, 1).Value = Me.txtPartNumber.Value
        .Cells(lRow, 2).Value = Me.txtCustomer.Value
        .Cells(lRow, 3).Value = Me.cboSkydrol.Value
        .Cells(lRow, 4).Value = Me.cboPneumatic.Value
        .Cells(lRow, 5).Value = Me.cboFuel.Value
        .Cells(lRow, 6).Value = Me.cboRedOil.Value
        .Cells(lRow, 7).Value = Me.cboSpace.Value
        .Cells(lRow, 8).Value = Me.cboStyle.Value
        .Cells(lRow, 9).Value = Me.txtWeight.Value
        .Cells(lRow, 10).Value = Me.txtMaxPressure.Value
        .Cells(lRow, 11).Value = Me.txtOperatingPressure.Value
        .Cells(lRow, 12).Value = Me.txtProofPressure.Value
        .Cells(lRow, 13).Value = Me.txtBurstPressure.Value
        .Cells(lRow, 14).Value = Me.txtAmbientTemperature.Value
        .Cells(lRow, 15).Value = Me.txtFluidTemperature.Value
        .Cells(lRow, 16).Value = Me.txtPullIn.Value
        .Cells(lRow, 17).Value = Me.txtDropOut.Value
        .Cells(lRow, 18).Value = Me.txtCoilResistance.Value
        .Cells(lRow, 19).Value = Me.txtLeakage.Value
        .Cells(lRow, 20).Value = Me.txtFlow.Value
        .Cells(lRow, 21).Value = Me.txtNotes.Value
    End With

    'Clear all inputs.
    Me.cboSkydrol.Value = ""
    Me.cboPneumatic.Value = ""
    Me.cboFuel.Value = ""
    Me.cboRedOil.Value = ""
    Me.cboSpace.Value = ""
    Me.cboStyle.Value = ""
    Me.txtAmbientTemperature.Value = ""
    Me.txtBurstPressure.Value = ""
    Me.txtCoilResistance.Value = ""
    Me.txtDropOut.Value = ""
    Me.txtFlow.Value = ""
    Me.txtFluidTemperature.Value = ""
    Me.txtLeakage.Value = ""
    Me.txtMaxPressure.Value = ""
    Me.txtNotes.Value = ""
    Me.txtOperatingPressure.Value = ""
    Me.txtPartNumber.Value = ""
    Me.txtProofPressure.Value = ""
    Me.txtPullIn.Value = ""
    Me.txtWeight.Value = ""
    Me.txtCustomer.Value = ""
End Sub

The PowerPoint Presentation opened in a separate window but nothing was changed. PowerPoint演示文稿在单独的窗口中打开,但未进行任何更改。 Also, the rest of my VBA code didn't execute. 另外,我的其余VBA代码也没有执行。 Though I'm only a week into my understanding of ExcelVBA coding. 尽管对ExcelVBA编码的理解只有一周的时间。 So far has been just trying Frankenstein code from several sites. 到目前为止,仅在几个站点上尝试了Frankenstein代码。

Can I do this without opening the slide in a separate window? 我可以在没有在单独的窗口中打开幻灯片的情况下执行此操作吗?

I'd appreciate some input. 我将不胜感激。 :) :)

The workbook contains worksheets "Slide Template" and "Directory". 该工作簿包含工作表“幻灯片模板”和“目录”。

The PowerPoint slide is named "Object 1". PowerPoint幻灯片名为“对象1”。

The destination texbox within the slide is named "operationaltext1". 幻灯片中的目标texbox名为“ operationaltext1”。

here is some code that inserts a powerpoint slide, adds text to it and also reads the text back 这是一些代码,可插入幻灯片,向其中添加文本并向后读取文本

if you only need one slide, then this is for you 如果您只需要一张幻灯片,那么这个适合您

Option Explicit

Sub testPPslide()

    ' NOTE: this adds a slide everytime the code is run
    ' it will be the standard "click here to add title" slide

    Worksheets("Sheet1").Range("c1:d1") = ""

    Dim pps As OLEObject

    Set pps = Worksheets("Sheet1").OLEObjects.Add( _
            ClassType:="PowerPoint.Slide.12", _
            Link:=False, _
            DisplayAsIcon:=False)

'   pps.Verb Verb:=xlOpen                     ' this edits slide in standalone PP app
'   pps.Verb Verb:=xlPrimary                  ' this one opens PP in excel
    pps.Top = 40
    pps.Left = 60

    Dim ps As powerpoint.Slide
    Set ps = pps.Object

    ps.Shapes(1).TextFrame.TextRange.Text = "cccccccccc"
    ps.Shapes(2).TextFrame.TextRange.Text = "this works"

    Worksheets("Sheet1").Range("c1") = ps.Shapes(1).TextFrame.TextRange.Text
    Worksheets("Sheet1").Range("d1") = ps.Shapes(2).TextFrame.TextRange.Text

End Sub

this one works on presentations 此作品适用于演示文稿

example code 示例代码

Option Explicit


Sub testPPpresentation()

    ' NOTE: adds a PP presentation to the worksheet each time it is run

    Worksheets("Sheet1").Range("c1:d1") = ""

    Dim aaa As OLEObject
    Set aaa = Worksheets("Sheet1").OLEObjects.Add( _
                ClassType:="PowerPoint.Show.12", _
                Link:=False, _
                DisplayAsIcon:=False)

'   aaa.Verb Verb:=xlOpen       ' this edits the presentation in standalone PP app
'   aaa.Verb Verb:=xlPrimary    ' this one edits the presentation in excel

    aaa.Top = 90
    aaa.Left = 60

    Dim ppp As PowerPoint.Presentation
    Set ppp = aaa.Object

    ppp.Slides(1).Shapes(1).TextFrame.TextRange.Text = "cccccccccc"
    ppp.Slides(1).Shapes(2).TextFrame.TextRange.Text = "this works"

    Worksheets("Sheet1").Range("c1") = ppp.Slides(1).Shapes(1).TextFrame.TextRange.Text
    Worksheets("Sheet1").Range("d1") = ppp.Slides(1).Shapes(2).TextFrame.TextRange.Text

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM