none
Problem with Application.Workbooks.Open in C# RRS feed

  • Question

  • Hi all,

    I wrote a COM-Addin using C# (VS2010). This addin shows up a ribbon-menu which contains, amongst other things, a button. The addin resp. Excel 2010 is run on Windows 7 Pro. As the user clicks the button, following code is executed:

            private void btnZMAktuell_Click(object sender, RibbonControlEventArgs e)
            {
                string Datei = "S://Daten//ZM//ZVIZ_Fehlerlisten-ZM_ALLE//ZM-alleFehler-aktuelleArbeitsdatei.xls";
                Globals.ThisAddIn.Application.Workbooks.Open(Datei);
            }

    If the user

    1) click this button for the first time, everything works. If the user

    2) opens another workbook and

    3) clicks this button again, the workbook opened only 5 minutes ago can't be found, because the path which is hard coded in above source code, is wrong.

    It seems as if the path of the workbook opened in step 2) is mixed with the path which is hard coded in above source code. Because the workbook opened in step 2) is chosen by chance, the resulting mixed-up path is wrong.

    Any suggestions how to solve this?

    Wednesday, November 23, 2011 5:32 PM

Answers

  • Hi Tom, Andrei,

    thanks for answering.

    I was at the customers site yesterday. As we were experimenting we found out, that writing the path using // is differrent to writing it using /.

    So I changed my initial code (string Datei = "S://Daten//ZM//ZVIZ...") to string Datei = @"S:/Daten/ZM/ZVIZ.." and at the customers site it worked. Of course next I have to check, if this individual solution can be generalized.

    I still have to change the code at my office and test it thoroughly. I'm new to writing Addins in .NET and also to C#, could you tell a difference in using one or two slashes, resp. setting an @ in the beginnig of a string?

    If it is interesting for you, I can try your proposals, drop me a line. First I'll check above described way, as it seems to be a chance to fix it.

    Cheers

    Helmut62

    Edit: I tried Tom's suggestion. Same behaviour if I load the workbook from a Windows share, so it's no problem of the Novell driver

    Wednesday, November 30, 2011 5:20 PM

All replies

  • Hello,

    Does the scenario include closing the first workbook?

    Is the workbook opened in the ProtectedViewWindow?


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader
    Wednesday, November 23, 2011 7:57 PM
  • Hi Andrei,

    I have to say, as I heared yesterday this user complaint it was for me as if the Lady would try to tell me marsians had landend :D

    To answer your question:

    I found no difference in behavior if the first workbook remains open or not

    Protected View isn't used

     

    Today I found out some more facts:

    1) opened that workbook as coded (I clicked the button)

    2) opened several other workbooks on other network shares and local files

    3) repeated step 1), worked fine

    4) opened a workbook on the Novell-share S:, path was S:\Daten\Excel

    5) repeated 1), but this time the error occured, file not found. The path in the error message was S:\Daten\Excel\Daten... so the path from step 4) showed up and the coded path was concatenated (btw. I'm not sure if the error showed slashes or backslashes)

    Restarted Excel, opened a workbook in a sub folder on another Novell-share (Drive R:), clicked the button (step 1), everything was fine.

    I think the error ocurres, as I open a workbook in a sub-folder on the same drive as coded (i.e. S:). The path used is persisted somewhere and reused, as the button is clicked. If that persisted path is deleted, step 1) works again:

    1) opened a workbook on S:\<some_sub_dir>

    2) clicked button -> error

    3) opened workbook on S:\ (rootdir)

    4) repeated 2) -> works

    It would be great if you could help me, Andrei, I'll send a pint Bobrov ;)



    • Edited by Helmut62 Thursday, November 24, 2011 10:37 AM
    Thursday, November 24, 2011 10:29 AM
  • Hi Helmut62,

     

    Thanks for posting in the MSDN Forum.

     

    Would you try you workbook on Window’s Network driver. I’m wondering whether it works fine on Window’s Network Driver. If it is, I think you need to consult Novell for you issue.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, November 29, 2011 7:19 AM
    Moderator
  • Hello,

    Sorry for the delay. I 'd also recommend checking what workbooks are open:

    Sub checkBooks()
    Dim i As Integer
    For i = 1 To Application.Workbooks.Count
        Debug.Print i, Application.Workbooks(i).FullName
    Next i
    End Sub
    
    

    The macro above in the VBA IDE (start Excel, press Alt+F11) prints workbook names in the Immediate window.


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader
    Tuesday, November 29, 2011 7:31 AM
  • Hi Tom, Andrei,

    thanks for answering.

    I was at the customers site yesterday. As we were experimenting we found out, that writing the path using // is differrent to writing it using /.

    So I changed my initial code (string Datei = "S://Daten//ZM//ZVIZ...") to string Datei = @"S:/Daten/ZM/ZVIZ.." and at the customers site it worked. Of course next I have to check, if this individual solution can be generalized.

    I still have to change the code at my office and test it thoroughly. I'm new to writing Addins in .NET and also to C#, could you tell a difference in using one or two slashes, resp. setting an @ in the beginnig of a string?

    If it is interesting for you, I can try your proposals, drop me a line. First I'll check above described way, as it seems to be a chance to fix it.

    Cheers

    Helmut62

    Edit: I tried Tom's suggestion. Same behaviour if I load the workbook from a Windows share, so it's no problem of the Novell driver

    Wednesday, November 30, 2011 5:20 PM
  • Hi Helmut62,

     

    I’m glad to hear you solved you issue. Thanks for share your experience here, it’s very beneficial for other community members who have similar issue to see you how you solved your problem.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Friday, December 2, 2011 6:06 AM
    Moderator