将 Excel 中的 VB 脚本转换为独立可执行脚本

问题描述 投票:0回答:1

我在 Excel 模块中编写了一个 VB 脚本,用于将图表导出到 Word 文档,如果我在 Microsoft Visual Basic 应用程序中运行该模块,它可以正常工作。但我需要编写一个独立的可执行 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"

    ' VBScript to read data from Excel and export tables to Word with formatting

    ' 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 that contains this code.
    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

以下是独立运行程序的更新版本。


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"

    ' VBScript to read data from Excel and export tables to Word with formatting

    ' 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 that contains this code.
    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

错误信息

excel vba vbscript
1个回答
0
投票

不能在 VBScript 中使用命名参数。

所以这些:

With WrdApp.Selection
    .PasteSpecial Link:=True, DataType:=wdPasteOLEObject, Placement:=wdInLine
End With

需要使用逗号替换,直到按顺序到达参数

© www.soinside.com 2019 - 2024. All rights reserved.