【问题标题】:Excel VBA Print w Automated File Name and Location using Range.ExportAsFixedFormatExcel VBA 使用 Range.ExportAsFixedFormat 自动打印文件名和位置
【发布时间】:2020-08-17 03:54:38
【问题描述】:

https://www.contextures.com/excelvbapdf.html 以下是完美的,但它会打印整张纸。

全线程

  Sub PDFActiveSheet()
'www.contextures.com
'for Excel 2010 and later
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

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

但我遇到了这个问题

'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` 

我想要完成的唯一一件事是打印一个范围(最好命名)来代替整个工作表。我创建了 dims 并设置了一个范围来代替“wsA”工作表,但它存在问题。

Dim rnG As Range
Set rnG = Range("Y1:AG46")
rnG.ExportAsFixedFormat _

是我添加的唯一行。它会按我的意愿工作,但间歇性地工作,我不知道为什么。它以黄色显示整个 ExportFileAsFixedFormat 子文本,并指出无法识别指定的 Range。

【问题讨论】:

  • 请尽量不要在整个帖子中使用粗体字:这样会更难阅读。

标签: excel vba printing


【解决方案1】:

我怀疑这是因为您没有限定范围。试试

Set rnG = wsA.Range("Y1:AG46")

只有当wsA 是活动工作表时,无限定才能正常工作。

【讨论】:

  • 这种格式真的很混乱 Ctrl+K 已经失效了。我按照你说的做了,同样的黄色正在发生。将 wsA 调暗为工作表 将 wbA 调暗为工作簿 将 rnG 调暗为范围集 wbA = ActiveWorkbook 集 wsA = ActiveSheet 集 rnG = wsA.Range("Y1:AG46")
  • rnG.ExportAsFixedFormat _ Type:=xlTypePDF, _ filename:=myFile, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False
  • 黄色箭头指向最后一行:OpenAfterPublish:=False 是否有更多信息正在寻找或命令未正确关闭?
猜你喜欢
  • 2015-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 1970-01-01
  • 2015-03-14
  • 1970-01-01
相关资源
最近更新 更多