精英干员瑕光 2021-10-25 09:26 采纳率: 76.5%
浏览 11
已结题

VBA#遍历双层文件夹xlsx转pdf

本人想编写一个VBA脚本,可以自动遍历文件夹下的所有xlsx并转成pdf,如文件夹中还有文件夹,则对子文件夹进行相同操作,现有如下代码,但只能实现第一个目的,请问第二个目的该如何实现

Sub TO_PDF()

On Error Resume Next
Dim ALL_FILE As String, SourcePath As String, NewSaveFile As String
Dim CurFile As Object
Dim shit As Worksheet

SourcePath = "C:\Users\rz\Desktop\新建文件夹\"
'待转换的源xlsx文件夹路径
OBJPath = "C:\Users\rz\Desktop\新建文件夹\PDF\"
'转换为pdf文件后的存放路径

ALL_FILE = Dir(SourcePath & "*.xlsx") '用*.xls*匹配其他xls、xlsm格式文件

Do While ALL_FILE <> ""

   Set CurFile = Workbooks.Open(SourcePath & ALL_FILE, , msoTrue)
   
   '遍历每个xlsx文件中的每个sheet进行转换
   For Each shit In CurFile.Worksheets

   With ActiveSheet.PageSetup
        .LeftHeader = ""
        .CenterHeader = ""
        .RightHeader = ""
        .LeftFooter = ""
        .CenterFooter = ""
        .RightFooter = ""
        .LeftMargin = Application.InchesToPoints(0.75)
        .RightMargin = Application.InchesToPoints(0.75)
        .TopMargin = Application.InchesToPoints(1)
        .BottomMargin = Application.InchesToPoints(1)
        .HeaderMargin = Application.InchesToPoints(0.5)
        .FooterMargin = Application.InchesToPoints(0.5)
        .PrintHeadings = False
        .PrintGridlines = False
        .PrintComments = xlPrintNoComments
        .PrintQuality = 600
        .CenterHorizontally = False
        .CenterVertically = False
        .Orientation = xlPortrait
        .Draft = False
        .PaperSize = xlPaperA4
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .BlackAndWhite = False
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 0
        .PrintErrors = xlPrintErrorsDisplayed
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = False
        .ScaleWithDocHeaderFooter = True
        .AlignMarginsHeaderFooter = True
        .EvenPage.LeftHeader.Text = ""
        .EvenPage.CenterHeader.Text = ""
        .EvenPage.RightHeader.Text = ""
        .EvenPage.LeftFooter.Text = ""
        .EvenPage.CenterFooter.Text = ""
        .EvenPage.RightFooter.Text = ""
        .FirstPage.LeftHeader.Text = ""
        .FirstPage.CenterHeader.Text = ""
        .FirstPage.RightHeader.Text = ""
        .FirstPage.LeftFooter.Text = ""
        .FirstPage.CenterFooter.Text = ""
        .FirstPage.RightFooter.Text = ""
    End With
    
   
   NewSaveFile = OBJPath & "\" & CurFile.Name & "--" & shit.Name & ".pdf"
   '设置pdf文件名称
   shit.ExportAsFixedFormat Type:=xlTypePDF, Filename:=NewSaveFile
   '依次转换
   Next
  
   CurFile.Close SaveChanges:=False
   ALL_FILE = Dir
Loop
Set CurFile = Nothing

End Sub





  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 11月2日
    • 创建了问题 10月25日

    悬赏问题

    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作