将Excel模块VB脚本转为独立可执行VBScript时遇语法错误求助
Excel图表导出至Word的VBScript编译错误修复
问题背景
Excel模块中编写的图表导出至Word的VB脚本在VBA环境运行正常,但修改为独立可执行VBScript后,第57行出现**“预期语句”**编译错误。
原Excel模块中的VB脚本
Sub ExportingToWord_MultipleCharts_Workbook() 'Declare Word Variables Dim WrdApp As Object Dim WrdDoc As Object Dim SecCnt As Integer 'Declare Excel Variables Dim ChrtObj As ChartObject Dim WrkSht As Worksheet Dim Rng As Range Dim ChrCnt As Integer ' Define paths to Excel and Word files excelFilePath = "D:\GIT\modules\core\bin\logs\AssessmentReport.xlsx" wordFilePath = "D:\WordDocument.docx" ' Create Excel and Word objects Set objExcel = CreateObject("Excel.Application") ' Open Excel workbook Set objWorkbook = objExcel.Workbooks.Open(excelFilePath) 'Create a new instance of Word Set WrdApp = CreateObject("Word.Application") WrdApp.Visible = True WrdApp.Activate 'Create a new word document Set WrdDoc = WrdApp.Documents.Add ChrCnt = 0 'Loop through all the worksheets in the Workbook For Each WrkSht In objWorkbook.Worksheets 'Fix the instability error WrkSht.Activate 'Loop through the charts on the active sheet For Each ChrtObj In WrkSht.ChartObjects 'Copy the chart ChrtObj.Chart.ChartArea.Copy 'Increment Chart Count ChrCnt = ChrCnt + 1 'Fix the instability error Application.Wait Now + #12:00:01 AM# 'Paste the Chart in the Word Document With WrdApp.Selection .PasteSpecial Link:=True, DataType:=wdPasteOLEObject, Placement:=wdInLine End With 'Count the pages in the Word Document SecCnt = WrdApp.ActiveDocument.Sections.Count 'Add a new page to the document. WrdApp.ActiveDocument.Sections.Add 'Go to the newly created page. WrdApp.Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext 'Fix instability Errors Application.CutCopyMode = False Next ChrtObj Next WrkSht WrdApp.Selection.InlineShapes.AddOLEObject ClassType:="Excel.Sheet.12", Filename _ :="D:\GIT\modules\core\bin\logs\AssessmentReport.xlsx", LinkToFile:=False _ , DisplayAsIcon:=True, IconFileName:= _ "C:\WINDOWS\Installer\{90160000-000F-0000-1000-0000000FF1CE}\xlicons.exe" _ , IconIndex:=1, IconLabel:="AssessmentReport.xlsx" WrdDoc.SaveAs wordFilePath End Sub
修改后的独立VBScript版本(原错误版本)
Sub ExportingToWord_MultipleCharts_Workbook() 'Declare Word Variables Dim WrdApp, WrdDoc Dim strdocname On Error Resume Next Dim SecCnt 'Declare Excel Variables Dim ChrtObj Dim WrkSht Dim Rng Dim ChrCnt ' Define paths to Excel and Word files excelFilePath = "D:\GIT\modules\core\bin\logs\AssessmentReport.xlsx" wordFilePath = "D:\WordDocument.docx" ' Create Excel and Word objects Set objExcel = CreateObject("Excel.Application") ' Open Excel workbook Set objWorkbook = objExcel.Workbooks.Open(excelFilePath) 'Create a new instance of Word Set WrdApp = CreateObject("Word.Application") WrdApp.Visible = True WrdApp.Activate 'Create a new word document Set WrdDoc = WrdApp.Documents.Add ChrCnt = 0 'Loop through all the worksheets in the Workbook For Each WrkSht In objWorkbook.Worksheets 'Fix the instability error WrkSht.Activate 'Loop through the charts on the active sheet For Each ChrtObj In WrkSht.ChartObjects 'Copy the chart ChrtObj.Chart.ChartArea.Copy 'Increment Chart Count ChrCnt = ChrCnt + 1 'Fix the instability error Application.Wait Now + #12:00:01 AM# 'Paste the Chart in the Word Document With WrdApp.Selection .PasteSpecial Link:=True, DataType:=wdPasteOLEObject, Placement:=wdInLine End With 'Count the pages in the Word Document SecCnt = WrdApp.ActiveDocument.Sections.Count 'Add a new page to the document. WrdApp.ActiveDocument.Sections.Add 'Go to the newly created page. WrdApp.Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext 'Fix instability Errors Application.CutCopyMode = False Next ChrtObj Next WrkSht WrdDoc.SaveAs wordFilePath End Sub
错误信息

错误内容:Microsoft VBScript 编译错误: 预期语句,错误行号为57。
错误原因及修复方案
1. 核心编译错误(第57行)
独立VBScript脚本仅定义Sub过程但未执行,VBScript为过程式脚本,顶层代码直接执行,封装在Sub中的代码需显式调用。
修复:在脚本末尾添加调用语句:
Call ExportingToWord_MultipleCharts_Workbook()
2. VBScript与VBA的其他兼容性问题
除编译错误外,脚本存在以下VBS不兼容问题,需一并修复:
(1)Excel对象引用错误
VBA中Application为全局对象,但VBScript需通过创建的Excel实例objExcel访问:
- 将
Application.Wait改为objExcel.Wait - 将
Application.CutCopyMode = False改为objExcel.CutCopyMode = False
(2)Word常量未定义
VBScript不识别Word内置常量,需替换为对应数值:
| 常量名称 | 对应数值 |
|---|---|
| wdPasteOLEObject | 0 |
| wdInLine | 0 |
| wdGoToPage | 1 |
| wdGoToNext | 2 |
修复后的完整VBScript代码
Sub ExportingToWord_MultipleCharts_Workbook() 'Declare Word Variables Dim WrdApp, WrdDoc Dim SecCnt 'Declare Excel Variables Dim ChrtObj Dim WrkSht Dim ChrCnt ' Define paths to Excel and Word files excelFilePath = "D:\GIT\modules\core\bin\logs\AssessmentReport.xlsx" wordFilePath = "D:\WordDocument.docx" ' Create Excel and Word objects Set objExcel = CreateObject("Excel.Application") ' Open Excel workbook Set objWorkbook = objExcel.Workbooks.Open(excelFilePath) 'Create a new instance of Word Set WrdApp = CreateObject("Word.Application") WrdApp.Visible = True WrdApp.Activate 'Create a new word document Set WrdDoc = WrdApp.Documents.Add ChrCnt = 0 'Loop through all the worksheets in the Workbook For Each WrkSht In objWorkbook.Worksheets 'Fix the instability error WrkSht.Activate 'Loop through the charts on the active sheet For Each ChrtObj In WrkSht.ChartObjects 'Copy the chart ChrtObj.Chart.ChartArea.Copy 'Increment Chart Count ChrCnt = ChrCnt + 1 'Fix the instability error objExcel.Wait Now + #12:00:01 AM# 'Paste the Chart in the Word Document With WrdApp.Selection .PasteSpecial Link:=True, DataType:=0, Placement:=0 End With 'Count the pages in the Word Document SecCnt = WrdApp.ActiveDocument.Sections.Count 'Add a new page to the document. WrdApp.ActiveDocument.Sections.Add 'Go to the newly created page. WrdApp.Selection.GoTo What:=1, Which:=2 'Fix instability Errors objExcel.CutCopyMode = False Next ChrtObj Next WrkSht WrdDoc.SaveAs wordFilePath ' 清理对象 Set WrdDoc = Nothing Set WrdApp = Nothing Set objWorkbook = Nothing Set objExcel = Nothing End Sub ' 调用执行过程 Call ExportingToWord_MultipleCharts_Workbook()
内容的提问来源于stack exchange,提问作者pranavs
相关产品推荐
相关产品推荐

