首页 > 解决方案 > 从外部 vbs 运行时宏的行为不同

问题描述

我需要为打开的 xlsx 文件取消隐藏工作表 TAB,为此,我使用 VBS 文件打开 xlsm 文件并激活宏(位于模块中)。当我手动运行宏时,它可以工作。当它通过vbs激活时,它只能看到包含宏的xlsm文件,目前没有其他打开的excel。

有任何想法吗?谢谢

vbs代码:

Set objExcel = CreateObject("Excel.Application")
Set book = objExcel.Workbooks.Open("C:\Users\23892\Desktop\restore_ribbon.xlsm")
objExcel.Application.Run "'C:\Users\23892\Desktop\restore_ribbon.xlsm'!Module1.Restore_Ribbons"
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing

宏代码:

Sub Restore_Ribbons()

Dim wb As Workbook
Dim str As String
For Each wb In Application.Workbooks
    MsgBox wb.Name 'checking if name extracted correctly
    wb.Activate
    Application.ScreenUpdating = False
    Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
    ActiveWindow.DisplayWorkbookTabs = True
    wb.Application.ScreenUpdating = True

Next Workbook

MsgBox ("End")

End Sub

标签: excelvbavbscript

解决方案


再次输入代码......再次感谢

 Set objExcel = GetObject("C:\restore_ribbon.xlsm")
    objExcel.Application.Run "'C:\restore_ribbon.xlsm'!Module1.Restore_Ribbons"
    objExcel.Close
    Set objExcel = Nothing

推荐阅读