以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  把指定文件中的所有word文档转化为pdf  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=131504)

--  作者:qaz17909
--  发布时间:2019/2/27 23:01:00
--  把指定文件中的所有word文档转化为pdf

我有个文件夹,里面全部为word文档,想全部转化为pdf,代码如下,报错是什么原因?

Dim dlg As New FolderBrowserDialog
If dlg.ShowDialog = DialogResult.Ok Then
    For Each File As String In FileSys.GetFiles(dlg.SelectedPath)
        Dim rs As String = File.SubString(0,File.Length-4)
        Dim tg As String = rs & ".pdf"
        Dim app As New MSWord.Application
        try
            Dim doc = app.Documents.Open(File)
            doc.ExportAsFixedFormat(tg, MSWord.WdExportFormat.wdExportFormatPDF)
            doc.saved = True
            app.Quit
        catch ex As exception
            msgbox(ex.message)
            app.Quit
        End try
    Next
End If


--  作者:有点甜
--  发布时间:2019/2/27 23:19:00
--  

我测试代码没问题。你的报什么错?

 

要注意,你文件夹下,要全部是doc文件,不能有别的文件,不然肯定报错。

 

或者,你判断一下后缀是不是doc文件,如

 

Dim dlg As New FolderBrowserDialog
If dlg.ShowDialog = DialogResult.Ok Then
    For Each File As String In FileSys.GetFiles(dlg.SelectedPath)
        If file.EndsWith(".doc") OrElse file.EndsWith(".docx") Then
            Dim rs As String = File.SubString(0,File.Length-4)
            Dim tg As String = rs & ".pdf"
            Dim app As New MSWord.Application
            try
                Dim doc = app.Documents.Open(File)
                doc.ExportAsFixedFormat(tg, MSWord.WdExportFormat.wdExportFormatPDF)
                doc.saved = True
                app.Quit
            catch ex As exception
                msgbox(ex.message)
                app.Quit
            End try
        End If
    Next
End If