Excel interop prevent showing password dialog

后端 未结 3 1178
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 20:07

    Do not use Nothing for method arguments you do not want to supply.

    Instead of:

    m_xlWrkb = m_xlWrkbs.Open(strFile, Nothing, Nothing, Nothing, "", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
    

    use any of the following:

    m_xlWrkb = m_xlWrkbs.Open(strFile, Password:="")
    

    or

    m_xlWrkb = m_xlWrkbs.Open(strFile, , , , "", , , , , , , , , , )
    

    or

    Dim missing As System.Reflection.Missing = System.Reflection.Missing.Value
    m_xlWrkb = m_xlWrkbs.Open(strFile,missing, missing, missing, "", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
    

    If the Workbook is password protected, doing this will cause a COMException to be thrown with a message of:

    "The password you supplied is not correct. Verify that the CAPS LOCK key is off and be sure to use the correct capitalization."

    If the Workbook is not password protected, no exception will be thrown assuming the file is accessible.

    You can also define the "missing" object shown above like this:

    Dim missing As Object = Type.Missing
    

    Type.Missing and System.Reflection.Missing refer to the same object.

提交回复
热议问题