将Excel工作表另存为PDF

时间:2017-02-01 08:25:10

标签: excel-vba vba excel

作为一个绝对的VBA新手,我拼凑了一些代码,这些代码将通过Excel工作表运行,检查那里的所有内容,然后将工作表另存为PDF文件。但是,我对代码的保存部分有些麻烦。我不断收到错误“编译错误:预期:=”到这一行:

`Wsa.ExportAsFixedFormat(Type:=xlTypePDF,Filename:=myFile,Quality:=xlQualityStandard,IncludeDocProperties:=True,IgnorePrintAreas:=False, OpenAfterPublish:=False)´

我在这里只是一个完整的n00b吗?

整件事看起来像这样:

Sub mcrSave()

Dim wsA As Worksheet

Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler

'Check for mandatory fields
If Range("B3").Value = "" Then MsgBox ("Please fill in applicant")
Exit Sub
If Range("C1").Value = "" Then MsgBox ("Please fill in project title")
Exit Sub
If Range("H3").Value = "" Then MsgBox ("Please fill in date of application")
Exit Sub
If Range("C5").Value = "" Then MsgBox ("Please fill in expected cost")
Exit Sub
If Range("C7").Value = "" Then MsgBox ("Please fill in time schedule")
Exit Sub
If Range("B10").Value = "" Then MsgBox ("Please fill in project description")
Exit Sub
If Range("B18").Value = "" Then MsgBox ("Please fill in potential benefits")
Exit Sub
If Range("B26").Value = "" Then MsgBox ("Please fill in potential drawbacks")
Exit Sub
If Range("B34").Value = "" Then MsgBox ("Please fill in internal/external ressources")
Exit Sub



Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "yyyymmdd\_hhmm")

'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
  strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

'replace spaces and periods in sheet name
strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")

'create default name for savng file
strFile = strName & "_" & strTime & ".pdf"
strPathFile = strPath & strFile

'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename(InitialFileName:=strPathFile, FileFilter:="PDF Files (*.pdf), *.pdf", Title:="Select Folder and FileName to save")

'export to PDF if a folder was selected
If myFile <> "False" Then
    Wsa.ExportAsFixedFormat(Type:=xlTypePDF,Filename:=myFile,Quality:=xlQualityStandard,IncludeDocProperties:=True,IgnorePrintAreas:=False, OpenAfterPublish:=False)
    'confirmation message with file info
    MsgBox "PDF file has been created: " & vbCrLf & myFile
End If

exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler

End Sub

2 个答案:

答案 0 :(得分:1)

语法错误。正确的语法是

call Wsa.ExportAsFixedFormat(Type:=xlTypePDF,Filename:=myFile,...)

Wsa.ExportAsFixedFormat Type:=xlTypePDF,Filename:=myFile,...

答案 1 :(得分:0)

将您的导出命令更改为以下内容:

Wsa.ExportAsFixedFormat Type:=xlTypePDF, Filename:=myFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

此外,您需要更改强制字段检查,以便每个都看起来像:

If Range("B3").Value = "" Then
   MsgBox ("Please fill in applicant")
   Exit Sub
End If