0

我有一个问题 - Excel 和 Word 的实例在同一过程中的行为不同。看看代码。想法是有一个程序来处理以各种格式组合在 excel 和 word 中重新保存文件。

问题是我注意到 word 和 excel 的行为不同 - appWord 和 appExcel 具有不同的类型名称。在某些时候,appWord 从应用程序更改为对象,这使得它无法关闭。我不理解行为的差异,因为应用于它们的代码是相同的。

Option Explicit
Dim fso
Dim appWord
Dim appExcel
Set fso = CreateObject("Scripting.FileSystemObject")

startWord
ResaveFiles appWord.Documents, "docx", 12, 0
appWord.quit

startExcel
ResaveFiles appExcel.Workbooks, "xlsx", 51, 56
appExcel.quit


MsgBox "All done."


Sub ResaveFiles(appType, srcExtName, srcExtNum, tmpExtNum)
Dim objFile
Dim objOpenFile
Dim strDirectory
    For Each objFile in fso.GetFolder(".").Files
        If lcase(fso.GetExtensionName(objFile)) = srcExtName Then
                If typeName(appType) = "Documents" Then StartWord
                If typeName(appType) = "Workbooks" Then StartExcel  
            Set objOpenFile = appType.Open(objFile.path)
            strDirectory = fso.BuildPath(objOpenFile.path, fso.GetBaseName(objOpenFile.name) & "._temp")
            objOpenFile.SaveAs strDirectory, tmpExtNum
            objOpenFile.Close
            msgBox typename(appType) & objFile
            msgBox typename(appWord) 'First typename test
            msgBox Typename(appExcel)
                If typeName(appType) = "Documents" Then appWord.Quit
                If typeName(appType) = "Workbooks" Then appExcel.Quit   
            set objOpenFile = appType.Open(strDirectory)
            objOpenFile.SaveAs objFile.path, srcExtNum
            objOpenFile.Close
            fso.DeleteFile(strDirectory)
            msgBox typename(appWord) 'Second typename test
    msgBox Typename(appExcel)
        End If  
    Next
    End Sub

'Start Word
 Sub StartWord
            Set appWord = CreateObject("Word.Application")
                appWord.visible = false
                appWord.DisplayAlerts = false
    End Sub

'Start Excel
Sub StartExcel
            Set appExcel = CreateObject("Excel.Application")
                appExcel.visible = false
                appExcel.DisplayAlerts = false
End Sub

我已经通过以下方式对其进行了测试(使用两个类型名测试) - 当有可用的 word 文件时,首先 appWord 是 Application 并且 appExcel 为空,然后它更改为 Object 并且 appExcel 保持为空(在这种情况下,当子过程在 AppWord.Quit 处结束)。当没有 word 文件,并且脚本正在处理 Excel 时,首先 appWord 是 Object,appExcel 是 Application,然后 appWord 仍然是 Object,appExcel 仍然是 Application - 在这种情况下,当子过程结束时,appExcel 上没有错误。辞职。

4

1 回答 1

1

也许我错了,只是我的看法:

If typeName(appType) = "Documents" Then appWord.Quit
If typeName(appType) = "Workbooks" Then appExcel.Quit   

set objOpenFile = appType.Open(strDirectory)

appType是在输入 ResaveFiles Sub 之前对什么appWord.Documents或正在引用的内容的引用,您在其中实例化“Excel.Application”或“Word.Application”的新副本,并且在每种情况下,您都指示应用程序退出。appExcel.Workbooks问题不是为什么在单词的情况下你会出错。从我的角度来看,你应该得到一个错误。问题是,为什么如果指示退出,excel 会保持打开状态并维护引用来处理您的代码。

编辑 - 并没有尝试过。刚刚改编自 OP 代码。根据需要进行调整

Option Explicit

    ResaveFiles "Word.Application", "docx", 12, 0
    ResaveFiles "Excel.Application", "xlsx", 51, 56

MsgBox "All done."


Sub ResaveFiles(progID, srcExtName, srcExtNum, tmpExtNum )
Dim app, doc
Dim fso, objFile, objOpenFile, strDirectory

    Set fso = CreateObject("Scripting.FileSystemObject")
    For Each objFile in fso.GetFolder( "." ).Files
        If LCase(fso.GetExtensionName( objFile.Name )) = srcExtName Then

            ' Get references
            Set app = GetNewAppInstance( progID )
            Set doc = GetDocumentHandler( app )

            ' Save temp
            Set objOpenFile = doc.Open( objFile.Path )
            strDirectory = fso.BuildPath( objOpenFile.path, fso.GetBaseName(objOpenFile.name) & "._temp" )
            objOpenFile.SaveAs strDirectory, tmpExtNum
            objOpenFile.Close

            ' Release objects
            Set objOpenFile = nothing 
            Set doc = nothing 
            app.Quit
            Set app = nothing

            ' Get references again
            Set app = GetNewAppInstance( progID )
            Set doc = GetDocumentHandler( app )

            ' Resave file
            Set objOpenFile = doc.Open( strDirectory )
            objOpenFile.SaveAs objFile.path, srcExtNum
            objOpenFile.Close

            ' Release objects
            Set objOpenFile = nothing 
            Set doc = nothing 
            app.Quit
            Set app = nothing

            ' Clean
            fso.DeleteFile(strDirectory)

        End If
    Next 

End Sub


Function GetNewAppInstance( ByVal progID )
    Set GetNewAppInstance = CreateObject( progID )
    With GetNewAppInstance
        .Visible = False
        .DisplayAlerts = False
    End With
End Function

Function GetDocumentHandler( app )
    Dim name
    name = app.Name
    If InStr(name,"Excel") > 0 Then
        Set GetDocumentHandler = app.Workbooks
    ElseIf InStr(name,"Word") > 0 Then
        Set GetDocumentHandler = app.Documents
    Else
        Set GetDocumentHandler = app
    End If
End Function
于 2013-11-12T16:46:51.243 回答