2

我正在使用 MSOffice2010 和 cscript(Windows 脚本版本 5.8),尝试做一些简单的事情:

  1. Open a native xlsx file 
  2. Save it as xls file (xlExcel8)

我所做的是:

xcx.vbs

' http://msdn.microsoft.com/en-us/library/ff198017(v=office.14).aspx

  Const xlExcel8 = 56

  Set wdo = CreateObject("Excel.Application")

  Set wdoc = wdo.Workbooks.Open("c:\path\to\foo.xlsx")

  wdoc.SaveAs "c:\path\to\bar.xls", xlExcel8

  wdoc.Close

  wdo.Quit

要运行它:

cscript xcx.vbs

然后,脚本完成,没有任何错误。但我根本找不到c:\path\to\bar.xls

如果有人能告诉我正确的方法,我将不胜感激,谢谢:)

4

3 回答 3

0

It's strange that you have not error on the first line. Maybe you have On Error Resume Next above this code? Try this:

Const xlExcel8 = 56

But if that constant is already defined, you'll get error too like "Name redefined", and if so, just remove that line.

于 2013-02-28T10:50:57.123 回答
0

This really drove me crazy! The counterpart simlar code for docx<->doc and pptx<->ppt just worked smoothly.

And I found a phenomenon (rather than workround) that the file will be saved if an extra wasted line is added ahead the target 'SaveAs' line. (see the commented code below).

Besides:

  1. The wasted SaveAs doesn't really have function to save things(as the original problem)

  2. The target SaveAs doesn't work if the wasted SaveAS is trying to save as a file of same format as the target.

I am really dizzy now and tend to consider a MS Excel bug, how's that sound?

Const xlExcel8          = 56
Const xlOpenXMLWorkbook = 51

Set wdo = CreateObject("Excel.Application")

Set wdoc = wdo.Workbooks.Open("c:\path\to\foo.xlsx")

' This works, but only to save as bar.xls, which "resolved" my original problem
wdoc.SaveAs "c:\path\to\bar.xlsx", xlOpenXMLWorkbook 'Sacrificed line to *activate* the following line of saving ...
wdoc.SaveAs "c:\path\to\bar.xls",  xlExcel8

' This works, but only to save as bar.xlsx, 
' wdoc.SaveAs "c:\path\to\bar.xls",  xlExcel8  'Sacrificed line to *activate* the following line of saving ...
' wdoc.SaveAs "c:\path\to\bar.xlsx", xlOpenXMLWorkbook

' While this doesn't work at all
' wdoc.SaveAs "c:\path\to\bar.xls",  xlExcel8 
' wdoc.SaveAs "c:\path\to\bar.xls",  xlExcel8

wdoc.Close

wdo.Quit
于 2013-03-01T07:05:48.970 回答
0
Const xlExcel8 = 56

wdoc.ActiveWorkbook.SaveAs "c:\path\to\bar.xls", xlExcel8
于 2013-08-14T22:49:50.927 回答