所以我有一些代码打印出PDF格式的工作表,使用EXP_PDF.DLL。我用它作为我的代码的基础:https://msdn.microsoft.com/en-us/library/ee834871(v=office.11).aspx。
现在,我使用相同的代码,只进行了一些小的更改(即正在更改要打印的工作表的名称以及文件名来源的引用),并且在我的计算机上它可以正常工作在所有三个工作簿中。在另一台计算机上,我用来测试这些工作簿的计算机是否适用于它们,打印功能也适用于所有三个工作簿。但是,其他三个用户'我在那里尝试过的计算机是一本没有的工作簿(让我们称之为“问题儿童”和“金童工作手册”)。问题Child具有上述相同的代码,我前面提到了相同的修改。
以下是适用于这些有问题的计算机的工作簿中的代码:
Option Explicit
'The code below is used by the macros in the other two modules
'Do not change the code in the functions in this module
Function RDB_Create_PDF(Source As Object, FixedFilePathName As String, _
OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String
Dim FileFormatstr As String
Dim Fname As Variant
Dim msTitle As String
msTitle = Sheets("Maintenance Dashboard Summary").Range("A5").Value
'Test If the Microsoft Add-in is installed
If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _
& Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") <> "" Then
If FixedFilePathName = "" Then
'Open the GetSaveAsFilename dialog to enter a file name for the pdf
FileFormatstr = "PDF Files (*.pdf), *.pdf"
Fname = Application.GetSaveAsFilename(msTitle, 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
'Now the file name is correct we Publish to PDF
On Error Resume Next
Source.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 RDB_Create_PDF = Fname
End If
End Function
Sub RDB_Worksheet_Or_Worksheets_To_PDF()
Dim FileName As String
'Remove comment formating on line immediately below this one and the one immediately above End Sub to hide Maintenance Dashboard Summary sheet
Sheets("Maintenance Dashboard Summary").Visible = xlSheetVisible
If ActiveWindow.SelectedSheets.Count > 1 Then
MsgBox "There is more then one sheet selected," & vbNewLine & _
"be aware that every selected sheet will be published"
End If
'Call the function with the correct arguments
'Tip: You can also use Sheets("YourSheetName") instead of ActiveSheet in the code(sheet not have to be active then)
FileName = RDB_Create_PDF(Source:=Sheets("Maintenance Dashboard Summary"), _
FixedFilePathName:="", _
OverwriteIfFileExist:=True, _
OpenPDFAfterPublish:=True)
'For a fixed file name use this in the FixedFilePathName argument
'FixedFilePathName:="C:\Users\Ron\Test\YourPdfFile.pdf"
If FileName <> "" Then
'Ok, you find the PDF where you saved it
'You can call the mail macro here if you want
Else
MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _
"Microsoft Add-in is not installed" & vbNewLine & _
"You Canceled the GetSaveAsFilename dialog" & vbNewLine & _
"The path to Save the file in arg 2 is not correct" & vbNewLine & _
"You didn't want to overwrite the existing PDF if it exist"
End If
'Sheets("Maintenance Dashboard Summary").Visible = xlSheetVeryHidden
End Sub
这里的代码给了我麻烦:
Option Explicit
'The code below is used by the macros in the other two modules
'Do not change the code in the functions in this module
Function RDB_Create_PDF(Source As Object, FixedFilePathName As String, _
OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String
Dim FileFormatstr As String
Dim Fname As Variant
Dim msTitle As String
msTitle = Sheets("Vehicle Tire Summary").Range("A5").Value
'Test If the Microsoft Add-in is installed
If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _
& Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") <> "" Then
If FixedFilePathName = "" Then
'Open the GetSaveAsFilename dialog to enter a file name for the pdf
FileFormatstr = "PDF Files (*.pdf), *.pdf"
Fname = Application.GetSaveAsFilename(msTitle, 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
'Now the file name is correct we Publish to PDF
On Error Resume Next
Source.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 RDB_Create_PDF = Fname
End If
End Function
Sub RDB_Worksheet_Or_Worksheets_To_PDF()
Dim FileName As String
'Remove comment formating on line immediately below this one and the one immediately above End Sub to hide Vehicle Tire Summary sheet
Sheets("Vehicle Tire Summary").Visible = xlSheetVisible
If ActiveWindow.SelectedSheets.Count > 1 Then
MsgBox "There is more then one sheet selected," & vbNewLine & _
"be aware that every selected sheet will be published"
End If
'Call the function with the correct arguments
'Tip: You can also use Sheets("YourSheetName") instead of ActiveSheet in the code(sheet not have to be active then)
FileName = RDB_Create_PDF(Source:=Sheets("Vehicle Tire Summary"), _
FixedFilePathName:="", _
OverwriteIfFileExist:=True, _
OpenPDFAfterPublish:=True)
'For a fixed file name use this in the FixedFilePathName argument
'FixedFilePathName:="C:\Users\Ron\Test\YourPdfFile.pdf"
If FileName <> "" Then
'Ok, you find the PDF where you saved it
'You can call the mail macro here if you want
Else
MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _
"Microsoft Add-in is not installed" & vbNewLine & _
"You Canceled the GetSaveAsFilename dialog" & vbNewLine & _
"The path to Save the file in arg 2 is not correct" & vbNewLine & _
"You didn't want to overwrite the existing PDF if it exist"
End If
Sheets("Vehicle Tire Summary").Visible = xlSheetVeryHidden
End Sub
由此可以看出,我只更改了对工作簿中某些单元格和工作表的引用。但是,当我运行代码时,这个位给了我错误:
If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _
& Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") <> "" Then
最初我认为存在.DLL关联问题或其他问题,因为这是调试器在问题子代码中运行代码时代码中的代码行。但后来我意识到,由于代码在这些计算机上的其他两个工作簿中基本相同,因此必然存在其他一些问题。我想也许我做了一个拼写错误或其他什么,所以我从工作簿中删除了模块,从其中一个Golden Children导出了工作代码,将该代码导入到Problem Child中,并对表单和单元格引用进行了一些更改需要。毕竟,它仍然没有奏效,而且我很难过。
有没有人有这种问题的经验?或者,任何可能导致问题的理论?如果它们都不起作用,那么我就知道代码存在问题,但由于代码在三个工作簿中非常相似,我无法理解为什么它适用于两本书而不是第三本书