如何以编程方式打开一个新的隐藏的' PowerPoint / Word / Excel窗口进行处理

时间:2017-08-28 11:55:41

标签: vba

我正在使用Excel,PowerPoint和Word OLE对象,我的目标是“静默地”'将文档转换为PDF。

目前我有以下非静默方法:

Class PowerPointExporter
    Sub Export(path As String, newPath As String)
        Dim application As New PowerPoint.Application
        Dim presentation As PowerPoint.Presentation = application.Presentations.Open(path)
        presentation.SaveAs(newPath, PowerPoint.PpSaveAsFileType.ppSaveAsPDF)
        presentation.Close()
        application.Quit()
    End Sub
End Class
Class ExcelExporter
    Sub Export(path As String, newPath As String)
        Dim application As New Excel.Application
        Dim wb As Excel.Workbook = application.Workbooks.Open(path)
        wb.SaveAs(newPath, Excel.XlFixedFormatType.xlTypePDF)
        wb.Close()
        application.Quit()
    End Sub
End Class
Class WordExporter
    Sub Export(path As String, newPath As String)
        Dim application As New Word.Application
        Dim doc As Word.Document = application.Documents.Open(path)
        doc.SaveAs2(newPath, Word.WdSaveFormat.wdFormatPDF)
        doc.Close()
        application.Quit()
    End Sub
End Class

所以我的问题是,我怎样才能让这些沉默? I.E.隐藏窗口/将窗口移到屏幕外或类似的位置?

1 个答案:

答案 0 :(得分:2)

对于PowerPoint:

  Set PPTObj = New PowerPoint.Application
  If Debugging Then
    Set PPTClinic = PPTObj.Presentations.Open(fileName:=PPTFileName, ReadOnly:=msoCTrue, WithWindow:=msoTrue) 'this will prevent the window from being visible
  Else
    Set PPTClinic = PPTObj.Presentations.Open(fileName:=PPTFileName, ReadOnly:=msoCTrue, WithWindow:=msoFalse) 'this will prevent the window from being visible
  End If

请注意最后的WithWindow参数。

对于Excel:

  Set XLobj = New Excel.Application
  If Debugging Then
    XLobj.Visible = True
  Else
    XLobj.Visible = False
  End If

我在Word中做了很多年没做过任何事情,但它可能是类似的东西。我会看到谷歌说的......

啊,here是Word:

Application.Visible = False

对不起,没有漂亮的例子 - 我从我现有的代码中取了前两个,最后一个刚从链接页面借来。