以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  vba 保存为PDF求助  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=70866)

--  作者:ljh29206
--  发布时间:2015/6/30 14:30:00
--  vba 保存为PDF求助

 

Sub aaa()
Dim sht As Worksheet

For Each sht In Worksheets

Dim str As String
str = ActiveWorkbook.Path & "\\pda°ü\\" & sht.Name & ".pdf"
    sht.ExportAsFixedFormat Type:=xlTypePDF, Filename:=str, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        False
Next
End Sub

在excel 运行的  

 

运行到这段时   sht.ExportAsFixedFormat Type:=xlTypePDF, Filename:=str, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        False

提示 运行错误5   无效过程 或者参数。

但该段代码是用  录制宏 录出来的!! 就修改了   历遍 所有 sheet 而已 不知道怎么搞 求帮助!


--  作者:大红袍
--  发布时间:2015/6/30 14:52:00
--  

 适当修改

 

Dim App As New MSExcel.Application
try  
    Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open("d:\\test.xls")
    \'创建Excel文件对应的PDF文件
    wb.Saved = True
    wb.ExportAsFixedFormat(MSExcel.XlFixedFormatType.xlTypePDF, "d:\\test.pdf", MsExcel.XlFixedFormatQuality.xlQualityStandard, True, True,  System.Reflection.Missing.Value,  System.Reflection.Missing.Value, False,  System.Reflection.Missing.Value)
    app.quit
catch ex As exception
    msgbox(ex.message)
    app.quit
End try


--  作者:ljh29206
--  发布时间:2015/6/30 15:11:00
--  

老师 我修改了一下  想 一个 sheet 保存为 1个 pdf

 

这样该了

 

Dim App As New MSExcel.Application
try  
    Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open("C:\\Users\\user\\Desktop\\a.xls")
    \'创建Excel文件对应的PDF文件
    wb.Saved = True
Dim Ws As MSExcel.WorkSheet
For Each   ws In wb.worksheets 
    Dim str As String  = ws.name
    ws.ExportAsFixedFormat(MSExcel.XlFixedFormatType.xlTypePDF, "d:\\test\\" & str & ".pdf", MsExcel.XlFixedFormatQuality.xlQualityStandard, True, True,  System.Reflection.Missing.Value,  System.Reflection.Missing.Value, False,  System.Reflection.Missing.Value)
Next
   app.quit
catch ex As exception
    msgbox(ex.message)
    app.quit
End try

 

但不对 , 该如何操作呢?


--  作者:大红袍
--  发布时间:2015/6/30 15:24:00
--  

路径的问题

 

Dim App As New MSExcel.Application
try
    Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open("d:\\test.xls")
    \'创建Excel文件对应的PDF文件
    wb.Saved = True
    If FileSys.FileExists("d:\\test\\") = False Then
        filesys.CreateDirectory("d:\\test\\")
    End If
    Dim Ws As MSExcel.WorkSheet
    For Each ws In wb.worksheets
        Dim str As String  = ws.name
        ws.ExportAsFixedFormat(MSExcel.XlFixedFormatType.xlTypePDF, "d:\\test\\" & str & ".pdf", MsExcel.XlFixedFormatQuality.xlQualityStandard, True, True,  System.Reflection.Missing.Value,  System.Reflection.Missing.Value, False,  System.Reflection.Missing.Value)
    Next
    app.quit
catch ex As exception
    msgbox(ex.message)
    app.quit
End try