使用 VBA 将单元格内容从 Excel 导入 PowerPoint 演示文稿

问题描述 投票:0回答:2

我在 Excel 中使用 VBA 代码在 PowerPoint 中为电子表格的每一行创建一张幻灯片。以下代码可以正确打开一个新演示文稿并为电子表格中突出显示的每一行创建一个新幻灯片。它将该行的第一个单元格放置在新幻灯片的标题占位符内:

Sub LoopRowsSelected()

Dim DataRange As Range
Dim DataRow As Range
Dim DataColumn As Range


Dim AppPPT As PowerPoint.Application
Dim Prs As PowerPoint.Presentation
Dim Sld As PowerPoint.Slide

Set AppPPT = New PowerPoint.Application
Set Pres = AppPPT.Presentations.Open("C:\Test\Sample.potx")

AppPPT.Visible = True

Set DataRange = Selection

For Each DataRow In DataRange.Rows
    
    Set Sld = Pres.Slides.AddSlide(Pres.Slides.Count + 1, Pres.SlideMaster.CustomLayouts(1))
 
        Sld.Shapes.Title.TextFrame.TextRange.Text = DataRow.Cells(1, 1)

    Next DataRow

End Sub

我曾想过以不同的方式命名模板中的每个占位符,然后复制此行:

Sld.Shapes.Title.TextFrame.TextRange.Text = DataRow.Cells(1, 1)

并将其添加为如下所示:

Sld.Shapes.Description.TextFrame.TextRange.Text = DataRow.Cells(1, 2)

它将将该行的第二个单元格插入名为“Description”的占位符中。它返回一个错误,指出“编译错误:找不到方法或数据成员”。显然,我认为“标题”是模板中实际的“标题”选择对象名称,这是错误的。

我的问题是,我需要做什么不同的事情才能将单元格内容定向到 PPT 模板中的正确占位符?请注意(正如您已经弄清楚的那样),我不是编程、VBA 或Excel 专家。我对元素的方法和层次结构没有很好的理解,我确信这给我带来了问题。

excel vba powerpoint excel-2019
2个回答
0
投票

使用以下内容:

 Sld.Shapes("Description").TextFrame.TextRange.Text = DataRow.Cells(1, 2)

当您在母版幻灯片中命名占位符时,名称不会自动转移到创建的幻灯片中。您必须知道这些占位符名称是什么,才能在 VBA 中正确编码。如果是常规文本框架,则第一个很可能是“TextBox #”,其中“#”符号将是随着更多对象放入主幻灯片中而递增的数字。您可以通过转到功能区并选择 Format -> Selection Pane 来找出名称是什么,然后选择您想知道其名称的对象以在选择窗格中突出显示它。


0
投票

子CreateDecorativeConcretePresentation() Dim pptApp 作为对象 将 pptPres 调暗为对象 昏暗的幻灯片索引作为整数 变暗幻灯片标题作为变体 变暗幻灯片内容作为变体

' Initialize PowerPoint application
Set pptApp = CreateObject("PowerPoint.Application")
pptApp.Visible = True
Set pptPres = pptApp.Presentations.Add

' Define slide titles and contents
slideTitles = Array( _
    "Decorative Concrete: Enhancing Surfaces with Style", _
    "Introduction - Overview of decorative concrete", _
    "Benefits and Advantages - Durability, versatility, aesthetic appeal", _
    "Chapter 1 - Types of Decorative Concrete", _
    "Stamped Concrete - Images and examples", _
    "Stenciled Concrete - Images and examples", _
    "Exposed Aggregate Concrete - Images and examples", _
    "Colored Concrete - Images and examples", _
    "Textured Concrete - Images and examples", _
    "Polished Concrete - Images and examples", _
    "Chapter 2 - Design and Pattern Options", _
    "Stamp Patterns - Brick, stone, wood examples", _
    "Stencil Designs - Borders, logos, images examples", _
    "Color Options and Combinations - Examples and palettes", _
    "Texture and Finish Options - Examples and comparisons", _
    "Chapter 3 - Materials and Tools", _
    "Concrete Mix and Materials - Overview and options", _
    "Coloring Agents and Techniques - Explanation and examples", _
    "Stamps, Stencils, and Texturing Tools - Images and descriptions", _
    "Sealers and Protective Coatings - Options and applications", _
    "Chapter 4 - Installation and Application", _
    "Preparing the Surface - Tips and best practices", _
    "Mixing and Pouring the Concrete - Steps and considerations", _
    "Applying Stamps, Stencils, and Textures - Techniques and tips", _
    "Conclusion - Summary, inspiration, and call to action" _
)

slideContents = Array( _
    "Welcome to our presentation on decorative concrete. Let's explore how it enhances surfaces with style.", _
    "Decorative concrete involves using special techniques to enhance the appearance of concrete surfaces.", _
    "Benefits include durability, versatility, and aesthetic appeal.", _
    "There are various types of decorative concrete techniques.", _
    "Stamped concrete mimics the look of materials like brick, stone, or tile.", _
    "Stenciled concrete uses patterns applied to the surface to create unique designs.", _
    "Exposed aggregate concrete reveals the natural stones within the concrete mix.", _
    "Colored concrete adds pigments to create a wide range of colors.", _
    "Textured concrete can be manipulated to create various surface textures.", _
    "Polished concrete is a smooth, high-gloss finish achieved through grinding.", _
    "Design and pattern options are crucial in creating a desired look.", _
    "Stamp patterns can resemble brick, stone, or wood, adding versatility to concrete surfaces.", _
    "Stencil designs allow for intricate patterns, including borders and custom images.", _
    "Color options range from solid shades to multi-colored combinations.", _
    "Texture and finish options vary from smooth to rough, each with its own aesthetic and functional benefits.", _
    "Materials and tools are essential for creating decorative concrete.", _
    "Concrete mix options and coloring agents determine the final appearance of the concrete.", _
    "Stamps and stencils are used to imprint patterns and textures onto the concrete.", _
    "Sealers protect the surface and enhance the longevity of the decorative finish.", _
    "Proper installation techniques ensure a quality finish and long-lasting results.", _
    "Preparing the surface involves cleaning and leveling to ensure adhesion.", _
    "Mixing and pouring require precise measurements and techniques to achieve the best results.", _
    "Applying stamps, stencils, and textures requires careful handling to achieve the desired effect.", _
    "In conclusion, decorative concrete offers a stylish and durable way to enhance surfaces. Get inspired and start your project today." _
)

' Create slides
For slideIndex = LBound(slideTitles) To UBound(slideTitles)
    With pptPres.Slides.Add(slideIndex + 1, 1) ' 1 represents ppLayoutText
        .Shapes.Title.TextFrame.TextRange.Text = slideTitles(slideIndex)
        .Shapes.Placeholders(2).TextFrame.TextRange.Text = slideContents(slideIndex)
    End With
Next slideIndex

' Inform the user
MsgBox "Presentation created with " & UBound(slideTitles) + 1 & " slides."

结束子

© www.soinside.com 2019 - 2024. All rights reserved.