Vba 将表格从Excel粘贴到PowerPoint的最佳方法(保留源格式)

Vba 将表格从Excel粘贴到PowerPoint的最佳方法(保留源格式),vba,excel,powershell,automation,powerpoint,Vba,Excel,Powershell,Automation,Powerpoint,我目前正在通过PowerShell中的自动化在Excel中构建一个表。这个步骤非常有效,桌子完全按照我喜欢的方式结束。我现在想把它粘贴到PowerPoint演示文稿中 PowerPoint演示文稿是我创建的模板,然后用其他元素填充。我想除了这个,我的每个部分都裂开了 我想从已经在后台打开的Excel文件中粘贴。到目前为止,它已激活,并选择了所需的范围。然后将其粘贴到PowerPoint窗口中。但是,它是一个灰色的表格,没有任何格式 以前,当我把我的模板放在一起并手动测试不同的组件时,下面的一行从

我目前正在通过PowerShell中的自动化在Excel中构建一个表。这个步骤非常有效,桌子完全按照我喜欢的方式结束。我现在想把它粘贴到PowerPoint演示文稿中

PowerPoint演示文稿是我创建的模板,然后用其他元素填充。我想除了这个,我的每个部分都裂开了

我想从已经在后台打开的Excel文件中粘贴。到目前为止,它已激活,并选择了所需的范围。然后将其粘贴到PowerPoint窗口中。但是,它是一个灰色的表格,没有任何格式

以前,当我把我的模板放在一起并手动测试不同的组件时,下面的一行从Excel中进行了粘贴,非常完美

ActivePresentation.Application.CommandBars.ExecuteMso "PasteExcelTableSourceFormatting"
然而,由于移动到自动化(以及与不同窗口交互等),它不再工作。而是出现“无法创建activex组件”错误

完整代码如下:

Function CreateFLUTemplate(templateFile As String, PresPath As Variant, TalkingPointsDoc As Variant, LineOfBusiness As String, PolicyLink As String)

' Declare variables to be used
Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim WordApp As Word.Application
Dim PPFile As Object, WordDoc As Object
Dim TitleBox As PowerPoint.Shape, MetricsHeader As PowerPoint.Shape, MetricsTable As PowerPoint.Shape, PhishingHeader As PowerPoint.Shape, PhishingTable As PowerPoint.Shape
Dim PolicyHeader As PowerPoint.Shape, PolicyBox As PowerPoint.Shape, TalkingPointsHeader As PowerPoint.Shape, TalkingPointsBox As PowerPoint.Shape, shp As PowerPoint.Shape
Dim PPSlide As Slide
Dim WAIT As Double
Dim ShapeArray As Variant, LabelsArray As Variant, DateLabel As Variant
Dim i As Integer

' Open blank presentation file to be updated
Set PPApp = CreateObject("PowerPoint.Application")
PPApp.Visible = msoTrue
Set PPFile = PPApp.Presentations.Open(PresPath)
Set PPPres = PPApp.ActivePresentation

' Construct date that will be used in the header sections
DateLabel = Format(DateSerial(Year(Date), Month(Date), 0), "d mmmm yyyy")

' Set slide object so we can set our shape variables etc
Set PPSlide = PPPres.Slides(1)

' Copy finished Excel table

' Activate Spreadsheet with table to be copied
Windows(templateFile).Activate
Range("A1:E10").Copy

PPApp.Windows(1).Activate
' Paste Excel table in to PowerPoint
'ActivePresentation.Application.CommandBars.ExecuteMso "PasteExcelTableSourceFormatting"
'PPPres.Slides(1).Shapes.PasteSpecial(DataType:=ppPasteShape).Select
PPApp.ActivePresentation.Slides(1).Shapes.Paste

' Introduce delay to let paste action happen before moving on
WAIT = Timer
While Timer < WAIT + 0.5
   DoEvents
Wend

' Take pasted table and save to object
If PPApp.ActiveWindow.Selection.Type = ppSelectionNone Then
    MsgBox "Nothing is selected", vbExclamation
Else
    For Each shp In PPApp.ActiveWindow.Selection.ShapeRange
        Set MetricsTable = PPApp.ActivePresentation.Slides(1).Shapes(shp.Name)
    Next shp
End If

' Reposition and resize pasted table.
With MetricsTable
    .Left = 27
    .Top = 108
    .Width = 363
    .Table.Columns(1).Width = 148
    .Table.Columns(2).Width = 28
    .Table.Columns(3).Width = 28
    .Table.Columns(4).Width = 28
    .Table.Columns(5).Width = 131
    .Height = 227
End With
函数createFluteTemplate(templateFile为字符串,PresPath为变量,TalkingPointsDoc为变量,Lineof Business为字符串,PolicyLink为字符串)
'声明要使用的变量
将PPApp设置为PowerPoint.Application
将PPPres变暗为PowerPoint演示文稿
Dim WordApp作为Word.Application
Dim PPFile作为对象,WordDoc作为对象
将标题框设置为PowerPoint.Shape,将度量标题框设置为PowerPoint.Shape,将度量表格设置为PowerPoint.Shape,将PhishingHeader设置为PowerPoint.Shape,将PhishingTable设置为PowerPoint.Shape
将PolicyHeader设置为PowerPoint.Shape,将PolicyBox设置为PowerPoint.Shape,将TalkingPointsHeader设置为PowerPoint.Shape,将TalkingPointsBox设置为PowerPoint.Shape,将shp设置为PowerPoint.Shape
将PPSlide设置为幻灯片
暗淡的等待
Dim ShapeArray作为变体,LabelsArray作为变体,DateLabel作为变体
作为整数的Dim i
'打开要更新的空白演示文稿文件
设置PPApp=CreateObject(“PowerPoint.Application”)
PPApp.Visible=msoTrue
设置PPFile=PPApp.Presentations.Open(PresPath)
设置PPPres=PPApp.ActivePresentation
'将在标题部分中使用的构造日期
DateLabel=格式(DateSerial(年(日期)、月(日期),0),“d mmmm yyyy”)
'设置幻灯片对象,以便我们可以设置形状变量等
设置PPSlide=PPPres.幻灯片(1)
'复制完成的Excel表
'使用要复制的表激活电子表格
Windows(模板文件)。激活
范围(“A1:E10”)。副本
PPApp.Windows(1).激活
'将Excel表格粘贴到PowerPoint
'ActivePresentation.Application.CommandBars.ExecuteMso“粘贴ExcelTableSourceFormatting”
'PPPres.Slides(1).Shapes.PasteSpecial(数据类型:=ppPasteShape)。选择
PPApp.ActivePresentation.Slides(1.Shapes.Paste)
'引入延迟,让粘贴操作在继续之前发生
等待=计时器
当定时器<等待+0.5
多芬特
温德
'获取粘贴的表并保存到对象
如果PPApp.ActiveWindow.Selection.Type=ppSelectionNone,则
MsgBox“未选择任何内容”,请使用感叹号
其他的
对于PPApp.ActiveWindow.Selection.ShapeRange中的每个shp
Set MetricsTable=PPApp.ActivePresentation.Slides(1).形状(shp.Name)
下一个小水电
如果结束
'重新定位粘贴的表格并调整其大小。
带度量表
.左=27
.Top=108
.宽度=363
.表.列(1).宽度=148
.表.列(2).宽度=28
.表.列(3).宽度=28
.表.列(4).宽度=28
.表.列(5).宽度=131
.高度=227
以

成功地修复了它,真不敢相信我竟然没有想到检查代码中已经运行的类似操作!我应该使用:

PPPres.Application.CommandBars.ExecuteMso "PasteExcelTableSourceFormatting"

请访问www.pptxbuilder.com