Excel interop prevent showing password dialog

后端 未结 3 1173
Happy的楠姐
Happy的楠姐 2020-12-17 19:37

I am writing a program to clean excel files from empty rows and columns, i started from my own question Fastest method to remove Empty rows and Columns From Excel Files usin

3条回答
  •  遥遥无期
    2020-12-17 19:44

    Concerning your solution, are you sure that it will not work with anything, that is not the password? Something like this:

    Public Function wb_get_workbook(ByVal sFullName As String) As Workbook
    
        Dim sFile As String
        Dim wbReturn As Workbook
    
        sFile = Dir(sFullName)
    
        On Error Resume Next
            Set wbReturn = Workbooks(sFile)
    
            If wbReturn Is Nothing Then
                Application.AskToUpdateLinks = False
                Set wbReturn = Workbooks.Open(sFullName, , , , "ThisIsDefinitelyAPasswordThatNooneHasUsed681")
            End If
        On Error GoTo 0
    
        Set wb_get_workbook = wbReturn
    
    End Function
    

    Will also throw an error, if it is password protected, and if it is not, then it would not care about the password you are providing. I am trying in VBA, but in C# you use the Excel Application object, so it should not be different.

提交回复
热议问题