使用WScript修改密码保护的Excel Excel项目

时间:2013-11-26 18:51:25

标签: excel vba passwords wsh

我正在尝试使用WScript来修改excel VB项目组件,其中一些组件受密码保护。有两个目标:替换一些项目引用并修改一些代码。当我运行脚本来替换引用时,会显示密码对话框,我输入密码,结果应该是。但是修改代码的脚本没有给我密码对话框;它简直失​​败了。任何人都可以提出这个原因和解决方案。

更改参考的代码:

'------------------------------------------------------------------------

function ReplaceReference(path)

    replaced = false

    set app = createobject("Excel.Application")
    app.visible = true

    set wb = app.workbooks.open(path)

    for each r in wb.vbproject.references
        if r.name = "OldReference" then
            wb.vbproject.references.remove r
            wb.vbproject.references.addfromfile "NewReference.xla"
            wb.save
            replaced = true
            exit for
        end if
    next

    wb.close false

    app.quit

    ReplaceReference = replaced

end function

'------------------------------------------------------------------------

修改模块代码的代码:

'------------------------------------------------------------------------

function ReplaceCode(filename)

    set app = CreateObject("Excel.Application")
    app.visible = true

    file_modified = false

    set wb = app.workbooks.open(filename)

    set comps = wb.vbproject.vbcomponents

    for each comp in comps

        set modu = comp.codemodule

        if not modu is nothing then

            for i = 1 to modu.countoflines

                oldline = modu.lines(i, 1)

                newline = replace(oldline, "OldCode", "NewCode")

                modified = not (newline = oldline)

                if modified then
                    modu.replaceline i, newline
                    file_modified = true
                end if

            next

        end if

    next

    if file_modified then wb.save
    wb.close false

    ReplaceCode = file_modified

    app.quit

end function

'------------------------------------------------------------------------

0 个答案:

没有答案