首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Excel使用Range.ExportAsFixedFormat打印自动文件名和位置

Excel使用Range.ExportAsFixedFormat打印自动文件名和位置
EN

Stack Overflow用户
提问于 2020-08-17 03:54:38
回答 1查看 797关注 0票数 0

https://www.contextures.com/excelvbapdf.html 如下所示,但它打印了整个工作表.

全螺纹

代码语言:javascript
运行
复制
  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

,但我在上遇到麻烦了

代码语言:javascript
运行
复制
'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‘工作表,它正在窃听.

代码语言:javascript
运行
复制
Dim rnG As Range
Set rnG = Range("Y1:AG46")
rnG.ExportAsFixedFormat _

是我添加的唯一行。它会像我想的那样工作,但是间歇性的,我不知道为什么。它错误地显示在黄色的整个ExportFileAsFixedFormat潜台词中,并指向不识别指定的范围.

EN

Stack Overflow用户

发布于 2020-08-17 07:56:57

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

代码语言:javascript
运行
复制
Set rnG = wsA.Range("Y1:AG46")

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

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63444361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档