使用VBScript将Excel视图更改为页面布局

我需要使用VBScript将Excel工作簿中的所有工作表更改为页面布局视图,而不是默认视图。 但是,我不知道如何在VBS中做到这一点。 使用VBA,我一直使用的代码(用一个while循环遍历每个表)

With ActiveWindow .View = xlPageLayoutView End With 

这对我的目的很好。 但是我需要在VBS中这样做。 我认为这与Application对象有关,尽pipe我不确定。 任何帮助,将不胜感激。

编辑:这里是我用声明和东西写的代码示例。 它基本上遍历工作簿中的多个工作表,并将其全部(或尝试)设置为“页面布局”视图。 从这个段丢失是我填充工作簿与新名单匹配来自Names()的条目的子。

 Dim destFile, objWorkbook Set destFile = CreateObject("Excel.Application") Set objWorkbook = destFile.Workbooks.Add() objWorkBook.SaveAs(strPath) Sub OverNames() For i = 1 to 9 SetPagelayout(i) Next End Sub Sub SetPageLayout(hNum) Dim houseSheet, sheetName 'retrieves sheet name from array Names() sheetName = Names(hNum, 0) Set houseSheet = destFile.Worksheets(sheetName) houseSheet.Window.View = xlPageLayoutView End Sub 

VBA已经有excel和工作簿加载。 使用VBS,您需要创build一个excel对象并使用它打开工作簿。 此外,VBA有为Excel设置定义的静态variables,您将不得不在VBS中定义自己。

 Dim objExcel Dim excelPath Dim xlPageLayoutView=3 ' https://msdn.microsoft.com/en-us/library/office/ff838200.aspx excelPath = "C:\scripts\servers.xlsx" objExcel.DisplayAlerts = 0 Set objExcel = CreateObject("Excel.Application") 

为了改变窗口的状态,你必须访问窗口对象。 在Excel中有工作簿,其中包含工作表和Windows的集合。 该应用程序还包含所有工作表中所有窗口的集合。 在工作簿窗口集合中,总是通过索引1访问活动窗口。

 Set currentWorkBook = objExcel.ActiveWorkbook Set currentWorkSheet = currentWorkBook.Worksheets("Sheet Name Here") currentWorkSheet.Activate Set currentWindow = currentWorkBook.Windows(1) currentWindow.View = xlPageLayoutView