none
When open an protected excel document by VBA, how to hide main window? RRS feed

  • Question

  • I use C# to call VBA(PIA) function.

    when open an protected excel document, I want to show the password dialog, let user input his password, but the main window is always show, cover my application . I want to hide the main window or minimize it.

    So does the word document.


    Thanks

    Thursday, April 16, 2015 10:07 AM

Answers

  • Hi Emily_li,

    >>when open an protected excel document, I want to show the password dialog, let user input his password, but the main window is always show, cover my application . I want to hide the main window or minimize it.

    In my option, this is the expected behavior if we open the workbook first, then requiring the password from the Window Form. For a protected excel document, you could not operate the workbook like hiding and minimize it before the workbook has been opened.

    With a recommended design, I will recommend you get the password from user first, and then open the workbook with password. You could add a form with one button and a textbox for user to enter the password. Some key code as below:

            private void OepnExcel_Click(object sender, EventArgs e)
            {
                Excel.Application xlApp;
                xlApp = new Excel.Application();
                string path =System.IO.Path.GetFullPath(@"..\..\Password.xlsx"); //excel file path 
                xlApp.Visible = true;
                xlApp.Workbooks.Open(Filename: path,Password:txtPwd.Text.Trim()); //open with password
                MessageBox.Show("ok");
            }
    

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 20, 2015 9:09 AM
  • Hi Emily_li,

    >> the main window with null content is shown and password dialog popup before the main window。.

    In my option, it is expected behavior when you open a protected Excel workbook with a password. I am afraid that you could not achieve that when password dialog popup, the main windows with null content did not show.

    >> I found the "wordApplication.Dialogs" may contain the password dialog, but I don't know how to use it.

    As far as I know, Dialogs Object (Excel) is a collection of all the Dialog objects in Microsoft Excel. Before you open the protected Excel, you could not achieve the Dialogs Object (Excel). If you have issues about using Dialogs Object (Excel), I will recommend you post a new thread for how to use Dialogs Object (Excel). You could refer the link below for information about Dialogs Object (Excel)

    # Dialogs Object (Excel)
    https://msdn.microsoft.com/en-us/library/office/ff839409.aspx?f=255&MSPPError=-2147217396

    >> I think that the password dialog may not return the user's password to me.
    When user enter the password in the textbox of the Form, you could get the user’s password by “Password:txtPwd.Text.Trim()”. If you have issues about winforms and do not know how to use winform get TextBox value, I will recommend go to the link below for help.

    Reference: http://social.msdn.microsoft.com/Forums/en-US/home?forum=winforms

    Hope it will help.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 22, 2015 8:31 AM

All replies

  • Is the entire Excel workbook protected with a password or just worksheets?

    Use Save As and at the bottom of the dialog next to Save button there is a Tools DropDown. Click the DropDown and select General Options to enter password and then Save the document.

    If password is set as above then the document should not open until the password is entered.


    Regards, OssieMac

    Friday, April 17, 2015 6:09 AM
  • Hi Emily_li,

    >>when open an protected excel document, I want to show the password dialog, let user input his password, but the main window is always show, cover my application . I want to hide the main window or minimize it.

    In my option, this is the expected behavior if we open the workbook first, then requiring the password from the Window Form. For a protected excel document, you could not operate the workbook like hiding and minimize it before the workbook has been opened.

    With a recommended design, I will recommend you get the password from user first, and then open the workbook with password. You could add a form with one button and a textbox for user to enter the password. Some key code as below:

            private void OepnExcel_Click(object sender, EventArgs e)
            {
                Excel.Application xlApp;
                xlApp = new Excel.Application();
                string path =System.IO.Path.GetFullPath(@"..\..\Password.xlsx"); //excel file path 
                xlApp.Visible = true;
                xlApp.Workbooks.Open(Filename: path,Password:txtPwd.Text.Trim()); //open with password
                MessageBox.Show("ok");
            }
    

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 20, 2015 9:09 AM
  • Yes, I save the document by select General Options ,entire Excel workbook protected with a password。

    When I call "wordApplication.Documents.OpenNoRepairDialog" to open it, the main window with null content is shown and password dialog popup before the main window。.

    Tuesday, April 21, 2015 9:22 AM
  • Hi,Edward:

    Thank you very much for provide me the key code!

    But I want to show office password dialog to avoid localization issue. 

    I found the "wordApplication.Dialogs" may contain the password dialog, but I don't know how to use it.

    I think that the password dialog may not return the user's password to me.

    Best Regards,

    Emily_li



    • Edited by emily_li Tuesday, April 21, 2015 9:41 AM
    Tuesday, April 21, 2015 9:40 AM
  • Hi Emily_li,

    >> the main window with null content is shown and password dialog popup before the main window。.

    In my option, it is expected behavior when you open a protected Excel workbook with a password. I am afraid that you could not achieve that when password dialog popup, the main windows with null content did not show.

    >> I found the "wordApplication.Dialogs" may contain the password dialog, but I don't know how to use it.

    As far as I know, Dialogs Object (Excel) is a collection of all the Dialog objects in Microsoft Excel. Before you open the protected Excel, you could not achieve the Dialogs Object (Excel). If you have issues about using Dialogs Object (Excel), I will recommend you post a new thread for how to use Dialogs Object (Excel). You could refer the link below for information about Dialogs Object (Excel)

    # Dialogs Object (Excel)
    https://msdn.microsoft.com/en-us/library/office/ff839409.aspx?f=255&MSPPError=-2147217396

    >> I think that the password dialog may not return the user's password to me.
    When user enter the password in the textbox of the Form, you could get the user’s password by “Password:txtPwd.Text.Trim()”. If you have issues about winforms and do not know how to use winform get TextBox value, I will recommend go to the link below for help.

    Reference: http://social.msdn.microsoft.com/Forums/en-US/home?forum=winforms

    Hope it will help.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 22, 2015 8:31 AM