我花了幾個小時試圖除錯這個。這是一個從我選擇的單元格生成 PDF 的宏。此代碼適用于我的個人作業簿,但是當我將其作為加載項匯出時,將其添加到開發人員選項卡中并在加載項作業簿中再次查看代碼我不斷收到運行時錯誤 91:物件變數或塊變數沒有設定。任何幫助將不勝感激!
Sub Save_Selection_As_PDF_sheet()
Dim my_file As String
With ActiveSheet.PageSetup
.LeftMargin = Application.InchesToPoints(0)
.RightMargin = Application.InchesToPoints(0)
.TopMargin = Application.InchesToPoints(0)
.BottomMargin = Application.InchesToPoints(0)
.HeaderMargin = Application.InchesToPoints(0)
.FooterMargin = Application.InchesToPoints(0)
'.Orientation = xlLandscape
.Orientation = xlPortrait
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
.PrintArea = Selection.Address
Debug.Print I
End With
FileName = ActiveWorkbook.Name
If InStr(FileName, ".") > 0 Then
FileName = Left(FileName, InStr(FileName, ".") - 1)
End If
my_file = "H:\data\Desktop\" & FileName & ".pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
FileName:=my_file, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
uj5u.com熱心網友回復:
一種可能的解決方案是以正確的方式加載加載項。如果直接打開加載項,則會出現錯誤。您應該按照以下說明加載加載項:Microsoft Excel:添加或洗掉加載項
uj5u.com熱心網友回復:
將選擇匯出為 PDF(從加載項運行)
- 這個特殊的錯誤,運行時錯誤 '91': Object variable or With block variable not set,發生在沒有未隱藏的作業簿打開并且ActiveSheet是圖表作業表時。
- 檔案夾不存在時會出現不同的錯誤(運行時錯誤“1004”:檔案未保存。檔案可能已打開,或者保存時可能遇到錯誤。)或選擇不是一個范圍(運行-時間錯誤“438”:物件不支持此屬性或方法)。
Option Explicit
Sub ExportSelectionToPDF()
Const ProcName As String = "ExportSelectionToPDF"
On Error GoTo ClearError
Const dFolderPath As String = "H:\data\Desktop\"
Const dFileExtension As String = ".pdf"
If Len(Dir(dFolderPath, vbDirectory)) = 0 Then
MsgBox "The path '" & dFolderPath & "' doesn't exist.", _
vbCritical, ProcName
Exit Sub
End If
Dim sh As Object: Set sh = ActiveSheet
If sh Is Nothing Then ' to test, close all workbooks
MsgBox "No active sheet ('Nothing').", vbCritical, ProcName
Exit Sub
End If
If sh.Type <> xlWorksheet Then ' to test, activate a chart sheet
MsgBox "No worksheet ('" & sh.Name & "') active.", vbCritical, ProcName
Exit Sub
End If
If TypeName(Selection) <> "Range" Then ' to test, select a shape
MsgBox "No range ('" & TypeName(Selection) & "') selected.", _
vbCritical, ProcName
Exit Sub
End If
Dim paAddress As String: paAddress = Selection.Address
Dim BaseName As String: BaseName = sh.Parent.Name
If InStr(BaseName, ".") > 0 Then
BaseName = Left(BaseName, InStrRev(BaseName, ".") - 1)
End If
Dim dFilePath As String: dFilePath = dFolderPath & BaseName & dFileExtension
With sh.PageSetup
.LeftMargin = Application.InchesToPoints(0)
.RightMargin = Application.InchesToPoints(0)
.TopMargin = Application.InchesToPoints(0)
.BottomMargin = Application.InchesToPoints(0)
.HeaderMargin = Application.InchesToPoints(0)
.FooterMargin = Application.InchesToPoints(0)
'.Orientation = xlLandscape
.Orientation = xlPortrait
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
.PrintArea = paAddress
End With
sh.ExportAsFixedFormat Type:=xlTypePDF, Filename:=dFilePath, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
ProcExit:
Exit Sub
ClearError:
Debug.Print "'" & ProcName & "' Run-time error '" _
& Err.Number & "':" & vbLf & " " & Err.Description
Resume ProcExit
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/398226.html
上一篇:查找Word檔案中的確切數字
