2015-12-16 59 views
2

我最近在這裏獲得了有關Excel電子表格的幫助,該電子表格允許用戶爲客戶創建報價單。電子表格使用VBA來允許用戶按下一個按鈕,該按鈕可以從某些工作表生成PDF,然後將其附加到新的Outlook電子郵件中。無法在一臺計算機上保存來自Excel/VBA的PDF

不幸的是,這不適用於用戶的計算機之一。這個問題似乎與PDF的生成有關。最初按下按鈕時,什麼都沒有發生。我懷疑這是與Microsoft Add-in一起做爲另存爲PDF格式的,所以我確定它已經安裝了,它就是這樣。 「註釋掉」從代碼來的錯誤信息獲取從Visual Basic真正的錯誤消息後,我發現它是這樣的:

run-time error '-2147467261 (80004003)': Document not saved. 

當單擊「調試」它強調:

FileName = Create_PDF_Sheet_Level_Names(NamedRange:="addtopdf1", _ 
             FixedFilePathName:=ThisWorkbook.Path & "\" & "Quotation - " & Range("G18") & ".pdf", _ 
             OverwriteIfFileExist:=True, _ 
             OpenPDFAfterPublish:=False) 

內容涉及:

Function Create_PDF_Sheet_Level_Names(NamedRange As String, FixedFilePathName As String, _ 
             OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String 
'This function will create a PDF with every sheet with 
'a sheet level name variable <NamedRange> in it 
    Dim FileFormatstr As String 
    Dim Fname As Variant 
    Dim Ash As Worksheet 
    Dim sh As Worksheet 
    Dim ShArr() As String 
    Dim s As Long 
    Dim SheetLevelName As Name 

    'Test If the Microsoft Add-in is installed 
    If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _ 
     & Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") <> "" Then 

     'We fill the Array with sheets with the sheet level name variable 
     For Each sh In ActiveWorkbook.Worksheets 
      If sh.Visible = -1 Then 
       Set SheetLevelName = Nothing 
       On Error Resume Next 
       Set SheetLevelName = sh.Names(NamedRange) 
       On Error GoTo 0 
       If Not SheetLevelName Is Nothing Then 
        s = s + 1 
        ReDim Preserve ShArr(1 To s) 
        ShArr(s) = sh.Name 
       End If 
      End If 
     Next sh 

     'We exit the function If there are no sheets with 
     'a sheet level name variable named <NamedRange> 
     If s = 0 Then Exit Function 

     If FixedFilePathName = "" Then 

      'Open the GetSaveAsFilename dialog to enter a file name for the pdf 
      FileFormatstr = "PDF Files (*.pdf), *.pdf" 
      Fname = Application.GetSaveAsFilename("", filefilter:=FileFormatstr, _ 
                Title:="Create PDF") 

      'If you cancel this dialog Exit the function 
      If Fname = False Then Exit Function 
     Else 
      Fname = FixedFilePathName 
     End If 


     'If OverwriteIfFileExist = False we test if the PDF 
     'already exist in the folder and Exit the function if that is True 
     If OverwriteIfFileExist = False Then 
      If Dir(Fname) <> "" Then Exit Function 
     End If 

     Application.ScreenUpdating = False 
     Application.EnableEvents = False 

     'Remember the ActiveSheet 
     Set Ash = ActiveSheet 

     'Select the sheets with the sheet level name in it 
     Sheets(ShArr).Select 

     'Now the file name is correct we Publish to PDF 
     On Error Resume Next 
     ActiveSheet.ExportAsFixedFormat _ 
       Type:=xlTypePDF, _ 
       FileName:=Fname, _ 
       Quality:=xlQualityStandard, _ 
       IncludeDocProperties:=True, _ 
       IgnorePrintAreas:=False, _ 
       OpenAfterPublish:=OpenPDFAfterPublish 
     On Error GoTo 0 

     'If Publish is Ok the function will return the file name 
     If Dir(Fname) <> "" Then 
      Create_PDF_Sheet_Level_Names = Fname 
     End If 

     Ash.Select 

     Application.ScreenUpdating = True 
     Application.EnableEvents = True 
    End If 
End Function 

我真的抓我的頭在這裏!在Excel和Outlook上檢查所有設置與我的計算機並列,包括信任中心設置。還檢查了加載項。

+1

你有沒有試過評論'On Error Resume Next'?調試器突出顯示函數調用而不是實際問題很奇怪 –

+0

當工作簿未保存到本地驅動器或根本沒有保存時​​,出現類似問題。檢查'ThisWorkbook.Path'的值並確認它不是空的。 –

回答

0
  1. 請檢查用戶是否有足夠的磁盤空間來保存PDF文件!
  2. 在調用「ActiveSheet.ExportAsFixedFormat(...)」之前,我會推薦檢查PDF全名的長度(路徑,文件名和文件擴展名;在您的示例中它是變量「Fname」),因爲文件名(或更確切地說,全名)定期不能超過255個字符(請參閱:Naming Files, Paths, and Namespaces)。
相關問題