无法在一台计算机上从Excel/VBA保存PDF

无法在一台计算机上从Excel/VBA保存PDF,vba,excel,pdf,outlook,Vba,Excel,Pdf,Outlook,最近,我在这里收到了一份Excel电子表格的帮助,该电子表格允许用户为客户创建报价。电子表格使用VBA允许用户按下按钮,从某些工作表生成PDF,并将其附加到新的Outlook电子邮件 不幸的是,这在用户的计算机上不起作用。问题似乎在于PDF的生成。最初按下按钮时,什么也没发生。我怀疑这与Microsoft加载项以PDF格式保存有关,所以我确保它已安装,事实确实如此。在“注释掉”来自代码的错误消息以从Visual Basic获取真正的错误消息后,我发现它是这样的: run-time error '

最近,我在这里收到了一份Excel电子表格的帮助,该电子表格允许用户为客户创建报价。电子表格使用VBA允许用户按下按钮,从某些工作表生成PDF,并将其附加到新的Outlook电子邮件

不幸的是,这在用户的计算机上不起作用。问题似乎在于PDF的生成。最初按下按钮时,什么也没发生。我怀疑这与Microsoft加载项以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
Function Create\u PDF\u Sheet\u Level\u name(NamedRange为字符串,FixedFilePathName为字符串_
将EffileExist覆盖为布尔值,将OpenPDFAfterPublish(发布为布尔值)覆盖为字符串
'此函数将创建一个包含以下内容的每张工作表的PDF:
'其中包含图纸级别名称变量
Dim FileFormatstr作为字符串
Dim Fname作为变体
将灰烬作为工作表
将sh设置为工作表
Dim ShArr()作为字符串
他和我一样长
将SheetLevelName作为名称
'测试是否安装了Microsoft加载项
If目录(环境(“公共程序文件”)和“\Microsoft共享\OFFICE”_
&格式(Val(Application.Version),“00”和“\EXP\u PDF.DLL”)”,然后
'我们用带有sheet level name变量的图纸填充数组
对于ActiveWorkbook.工作表中的每个sh
如果sh.Visible=-1,则
Set SheetLevelName=Nothing
出错时继续下一步
Set SheetLevelName=sh.Names(NamedRange)
错误转到0
如果不是,那么名称什么都不是
s=s+1
重播保留共享(1到s)
ShArr(s)=上海名称
如果结束
如果结束
下一个sh
'如果没有带有
'一个名为
如果s=0,则退出函数
如果FixedFilePathName=“”,则
'打开GetSaveAsFilename对话框以输入pdf的文件名
FileFormatstr=“PDF文件(*.PDF),*.PDF”
Fname=Application.GetSaveAsFilename(“),filefilter:=FileFormatstr_
标题:=“创建PDF”)
'如果取消此对话框,请退出该函数
如果Fname=False,则退出函数
其他的
Fname=FixedFilePathName
如果结束
'如果OverwriteIfFileExist=False,我们将测试PDF
'已存在于文件夹中,如果为真,请退出该函数
如果OverwriteIfFileExist=False,则
如果Dir(Fname)“,则退出函数
如果结束
Application.ScreenUpdating=False
Application.EnableEvents=False
“还记得活动表吗
设置Ash=ActiveSheet
'选择包含图纸级别名称的图纸
工作表(ShArr)。选择
'现在文件名正确,我们将发布为PDF
出错时继续下一步
ActiveSheet.ExportAsFixedFormat_
类型:=xlTypePDF_
文件名:=Fname_
质量:=xlQualityStandard_
IncludeDocProperties:=True_
IgnorePrintAreas:=假_
OpenAfterPublish:=OpenPDFAfterPublish
错误转到0
'如果发布正常,函数将返回文件名
如果Dir(Fname)“,则
创建\u PDF\u工作表\u级别\u名称=Fname
如果结束
灰烬。选择
Application.ScreenUpdating=True
Application.EnableEvents=True
如果结束
端函数
我真是抓狂!在Excel和Outlook上与我的计算机并排检查所有设置,包括信任中心设置。还检查了加载项

  • 请检查是否有足够的磁盘空间供用户保存PDF文件
  • 我建议在调用“ActiveSheet.ExportAsFixedFormat(…)”之前检查PDF全名的长度(路径、文件名和文件扩展名;在您的示例中,它是变量“Fname”),因为Microsoft Windows下的文件名(或全名)通常不能超过255个字符(请参阅:)

  • 您是否尝试过在“下一步继续”错误中注释
    ?奇怪的是,调试器突出显示了函数调用,而不是实际的问题。当工作簿未保存到本地驱动器或根本未保存时,调试器也有类似的问题。检查
    此工作簿.Path的值,并验证其是否为空。