excel如何自動轉存成大量pdf且不重複命名

因工作需要需把excel表大量轉存成pdf
手工逐個檔案轉存並命名實在耗時
請問有沒有方法能將excel自動轉存成pdf並不重複命名呢?
例如:將一個excel表另存成100個pdf檔名不重複
感謝幫忙
文章關鍵字
用vba可以做到
mr.shadow87
目前嘗試用巨集錄製,執行時會停在輸入檔名,想要做到可以不用輸入檔名(自動命名)
mr.shadow87 wrote:
將一個excel表另存成10個pdf檔名不重複

真的可用,至於PowerShell腳本怎麼執行請上網找


用PowerShell轉換一個Excel檔至100個不同檔名的pdf檔


ChatGPT答
$ExcelFile = "C:\path\to\yourfile.xlsx"
$PDFDirectory = "C:\path\to\pdf\directory"

# Load the Excel Interop Assembly
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Excel") | Out-Null

# Open the Excel File
$Excel = New-Object -ComObject Excel.Application
$Workbook = $Excel.Workbooks.Open($ExcelFile)

# Loop through each worksheet in the workbook
for ($i=1; $i -le $Workbook.Sheets.Count; $i++) {
$Sheet = $Workbook.Sheets.Item($i)

for ($j = 1; $j -le 100; $j++) {
$PDFName = "$($PDFDirectory)\$($Sheet.Name)_$j.pdf"

# Export the current sheet to PDF
$Sheet.ExportAsFixedFormat([Microsoft.Office.Interop.Excel.XlFixedFormatType]::xlTypePDF, $PDFName)
}
}

# Close Excel and release the Interop objects
$Workbook.Close()
$Excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

mr.shadow87
感謝幫忙,趕緊來測試看看
mr.shadow87 wrote:
因工作需要需把exc...(恕刪)


嘗試使用powershell執行後出現以下錯誤
不曉得是哪邊出錯了呢?

Exception from HRESULT: 0x800A03EC
位於 C:\Users\AKAA-80008\Desktop\test.ps1:19 字元:5
+ $Sheet.ExportAsFixedFormat([Microsoft.Office.Interop.Excel.XlFixe ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
mr.shadow87 wrote:
Exception from HRESULT: 0x800A03EC

我只試過很基本的Excel,建議你先拿簡單內容的excel去測試,排除是程式的問題,若是仍有bug,網路上的說法有Office註冊不完全、電腦裡有多個Office等等
文章分享
評分
評分
複製連結

今日熱門文章 網友點擊推薦!