【发布时间】:2022-11-03 07:45:55
【问题描述】:
我花了很多时间尝试编写 VBA 代码来自动化我的工作,但不知道如何。我希望这里有人可以帮助我。
目标是根据 Excel 中的值从文件夹中的 PowerPoint 表格中插入图片。
我有5位于我设备上的文件夹中的不同图片 (.png)。 Excel中的单元格值来自1至5.
根据单元格的值,我希望将 5 张图片中的一张插入 Powerpoint 的表格中。
例如:如果 excel-value = 2,则在 powerpoint-table 中插入图片 2。
我希望以上是有道理的,我希望有人可以帮助我。
我尝试了以下方法:
Sub ESG_Globes()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
' Define PPT objects
Dim oPPT As PowerPoint.Presentation
Dim appPPT As PowerPoint.Application
Dim oWS As Excel.Worksheet
Dim fileNameString As String
Dim boolUploadToIntranet As Boolean
Dim cells As Range
Dim s14 As Integer, s15 As Integer, s13 As Integer
Dim ESG1, ESG2, ESG3, ESG4, ESG5 As String
Dim ImageBox, ImageBox2 As PowerPoint.Shape
With oPPT.Slides(8)
For k = 4 To 22
'Globes PNG Location
ESG1 = "S:\S8RENTE\Credit & Equity Research\ESG\Grafik\Glober (PNG)\SustainabilityRating_Low.png"
ESG2 = "S:\S8RENTE\Credit & Equity Research\ESG\Grafik\Glober (PNG)\SustainabilityRating_BelowAverage.png"
ESG3 = "S:\S8RENTE\Credit & Equity Research\ESG\Grafik\Glober (PNG)\SustainabilityRating_Average.png"
ESG4 = "S:\S8RENTE\Credit & Equity Research\ESG\Grafik\Glober (PNG)\SustainabilityRating_AboveAverage.png"
ESG5 = "S:\S8RENTE\Credit & Equity Research\ESG\Grafik\Glober (PNG)\SustainabilityRating_High.png"
' Check if file is open - if not, open it
fOpen = IsFileOpen("S:\S8RENTE\Aktieanalyse\Vaerktoejer\Aktieoverblik\Aktieoverblik - Sektoropdeling\Aktieoverblik_PPT - Sektor.pptx")
If Not fOpen Then
Set appPPT = CreateObject(class:="PowerPoint.Application")
Set oPPT = appPPT.Presentations.Open("S:\S8RENTE\Aktieanalyse\Vaerktoejer\Aktieoverblik\Aktieoverblik - Sektoropdeling\Aktieoverblik_PPT - Sektor.pptx")
Else
Set appPPT = GetObject(class:="PowerPoint.Application")
Set oPPT = appPPT.Presentations("Udkast til Aktieoverblik.pptx")
End If
Application.ScreenUpdating = False
Application.EnableEvents = False
Set oWS = ActiveWorkbook.Worksheets("PPT DATA")
Set owb = ActiveWorkbook
If oWS.cells(k, 37) = "1" Then
Set wdPic = .Cell(k, 37).Range.InlineShapes.AddPicture(filename:=ESG1, _
LinkToFile:=False, SaveWithDocument:=True)
If oWS.cells(k, 37) = "2" Then
Set wdPic = .Cell(k, 37).Range.InlineShapes.AddPicture(filename:=ESG2, _
LinkToFile:=False, SaveWithDocument:=True)
If oWS.cells(k, 37) = "3" Then
Set wdPic = .Cell(k, 37).Range.InlineShapes.AddPicture(filename:=ESG3, _
LinkToFile:=False, SaveWithDocument:=True)
If oWS.cells(k, 37) = "4" Then
Set wdPic = .Cell(k, 37).Range.InlineShapes.AddPicture(filename:=ESG4, _
LinkToFile:=False, SaveWithDocument:=True)
If oWS.cells(k, 37) = "5" Then
Set wdPic = .Cell(k, 37).Range.InlineShapes.AddPicture(filename:=ESG5, _
LinkToFile:=False, SaveWithDocument:=True)
End If
wdPic.Height = 0.3 * 28.34646
wdPic.Width = 0.3 * 28.34646
Set wdPic2 = wdPic.ConvertToShape
wdPic2.Left = CentimetersToPoints(4 - (y * 0.3))
y = y + 1
End With
End Sub
我知道上面可能是完全错误的,但我迷路了:/
【问题讨论】:
标签: excel vba image powerpoint