首先讓我解釋一下我正在構建什么。我有一個檔案夾,每天添加 50-100 個 .pdf。每個 .pdf 都必須被掃描并重新保存,并更改檔案名以顯示作業地點和完成它的員工。然后需要通過超鏈接將檔案附加到 Excel 電子表格中的物件以進行跟蹤。由于編程/安全作業的限制,無法從 excel VBA 中打開 .pdf。這導致流程的零自動化,每個檔案都需要使用 adobe 打開、查看、重新保存、在電子表格中創建的物件,然后單獨進行超鏈接。我正在嘗試創建一個用戶表單,該用戶表單將首先遍歷 .pdf 檔案夾并保存每個 .pdf 的 .gif 影像,然后允許用戶在 excel 用戶表單中查看每個 .gif 作為圖片,然后在保存 VBA 時將重命名檔案,在電子表格中創建物件并附加超鏈接。下面是我打開新 PPT、插入幻燈片、然后插入 .pdf 并最后將其重新保存為 .gif 的代碼。我在“pagesetup.slidewidth”上收到“運行時錯誤 438,物件不支持此屬性或方法”。我已經多年沒有使用 PPT 了,我不知道為什么 excel 除了這個語法之外不會。
Option Explicit
Sub ConvertPDFtoGIF()
Dim OriginalPath As String
Dim NewPath As String
Dim NewPPT As Object
Dim PDFWidth As Single
Dim PDFHeight As Single
Dim sh As Shape
OriginalPath = "C:\Users\hareb\Desktop\Work Tracker\Test\3763A1010100003112022 - Copy (2).pdf"
NewPath = "C:\Users\hareb\Desktop\Work Tracker\Test\Test\TestGIF.GIF"
PDFWidth = 8.5
PDFHeight = 11
Set NewPPT = CreateObject("Powerpoint.application")
NewPPT.Visible = True
NewPPT.Presentations.Add
With NewPPT.PageSetup
.SlideWidth = PDFWidth
.SlideHeight = PDFHeight
End With
NewPPT.Slides.addslide 1, NewPPT.slidemaster.customlayouts(1)
Set sh = NewPPT.Slides(1).Shapes.AddOLEObject(0, 0, PDFWidth, PDFHeight, , OriginalPath)
Call NewPPT.Slides(1).Export(NewPath, "GIF")
End Sub
uj5u.com熱心網友回復:
無論是 OM 中的錯誤還是什么,如果您獲得對 Presentation 作為物件變數的參考似乎會更快樂。Aircode 到我實際上沒有添加 PDF ole 物件并將幻燈片匯出為 GIF 的程度,但其余的作業:
Option Explicit
Sub ConvertPDFtoGIF()
Dim OriginalPath As String
Dim NewPath As String
Dim NewPPT As Object
Dim PDFWidth As Single
Dim PDFHeight As Single
Dim sh As Shape
' I added this
Dim PPTPres As Object
OriginalPath = "C:\Users\hareb\Desktop\Work Tracker\Test\3763A1010100003112022 - Copy (2).pdf"
NewPath = "C:\Users\hareb\Desktop\Work Tracker\Test\Test\TestGIF.GIF"
PDFWidth = 8.5
PDFHeight = 11
Set NewPPT = CreateObject("Powerpoint.application")
NewPPT.Visible = True
' get a reference to the presentation in PPTPres:
Set PPTPres = NewPPT.presentations.Add
' and use PPTPres to refer to the presentation and its
' properties/methods from here on:
PPTPres.Slides.AddSlide 1, PPTPres.SlideMaster.CustomLayouts(1)
With PPTPres.PageSetup
.SlideWidth = PDFWidth
.SlideHeight = PDFHeight
End With
Set sh = PPTPres.Slides(1).Shapes.AddOLEObject(0, 0, PDFWidth, PDFHeight, , OriginalPath)
Call PPTPres.Slides(1).Export(NewPath, "GIF")
End Sub
uj5u.com熱心網友回復:
謝謝史蒂夫,這正是我所需要的。我能夠調整代碼并且知道它的作業原理。這是最終代碼:
Sub ConvertPDFtoGIF()
Dim OriginalPath As String
Dim NewPath As String
Dim NewPPT As Object
Dim PDFWidth As Single
Dim PDFHeight As Single
Dim sh As Object
Dim PPTPres As Object
OriginalPath = "C:\Users\hareb\Desktop\Work Tracker\Test\3763A1010100003112022 - Copy (2).pdf"
NewPath = "C:\Users\hareb\Desktop\Work Tracker\Test\Test\TestGIF.GIF"
PDFWidth = 8.5 * 72
PDFHeight = 11 * 72
Set NewPPT = CreateObject("Powerpoint.application")
NewPPT.Visible = True
Set PPTPres = NewPPT.presentations.Add
PPTPres.Slides.AddSlide 1, PPTPres.SlideMaster.CustomLayouts(1)
With PPTPres.PageSetup
.SlideWidth = PDFWidth
.SlideHeight = PDFHeight
End With
Set sh = PPTPres.Slides(1)
sh.Shapes.AddOLEObject 0, 0, PDFWidth, PDFHeight, , OriginalPath
Call PPTPres.Slides(1).Export(NewPath, "GIF")
NewPPT.Quit
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/442146.html
標籤:excel vba powerpoint