none
C# how can delete link of Excel file

    Question

  • Hi everyone!

    I have a file a.xls link to b.xls and another file excel. I want to check a.xls, if it has link, I will delete that link. How can I do that by C#(winform)?

    thanks.

    Thursday, October 13, 2011 9:13 AM

Answers

  • There is no record (in the file itself) that will tell you whether there is a link to it somewhere. So logically you need to do this the other way around. Loop through your links and check whether they lead to an existing Excel workbook using File.Exists(path-from-link)
    James Finch (MCDST) -- Please vote as helpful if you found this post helpful, or mark as answer if it answered your question.
    • Marked as answer by Ho Duy Loi Thursday, October 13, 2011 1:01 PM
    Thursday, October 13, 2011 10:01 AM
  • There is information here which should help you read your Windows shortcut files and get the path information:

    http://stackoverflow.com/questions/139010/how-to-resolve-a-lnk-in-c

     

    That's the complicated bit, but it's been done for you! Once you have the path from a shortcut file, you can simply use File.Exists(path) to check if it exists. So you can check if a file is a link to a link! I'm assuming you're making some kind of desktop cleanup tool?

    Don't forget exception handling - and import System.IO

     


    James Finch (MCDST) -- Please vote as helpful if you found this post helpful, or mark as answer if it answered your question.
    • Edited by Woohoooo Thursday, October 13, 2011 1:23 PM
    • Marked as answer by Ho Duy Loi Thursday, October 13, 2011 2:56 PM
    Thursday, October 13, 2011 1:14 PM

All replies

  • There is no record (in the file itself) that will tell you whether there is a link to it somewhere. So logically you need to do this the other way around. Loop through your links and check whether they lead to an existing Excel workbook using File.Exists(path-from-link)
    James Finch (MCDST) -- Please vote as helpful if you found this post helpful, or mark as answer if it answered your question.
    • Marked as answer by Ho Duy Loi Thursday, October 13, 2011 1:01 PM
    Thursday, October 13, 2011 10:01 AM
  • hello Woo

    I don't understand your mean.

    how can detect link in a.xls by using File.Exists(path-from-link)?

    thank you.

    Thursday, October 13, 2011 1:06 PM
  • There is information here which should help you read your Windows shortcut files and get the path information:

    http://stackoverflow.com/questions/139010/how-to-resolve-a-lnk-in-c

     

    That's the complicated bit, but it's been done for you! Once you have the path from a shortcut file, you can simply use File.Exists(path) to check if it exists. So you can check if a file is a link to a link! I'm assuming you're making some kind of desktop cleanup tool?

    Don't forget exception handling - and import System.IO

     


    James Finch (MCDST) -- Please vote as helpful if you found this post helpful, or mark as answer if it answered your question.
    • Edited by Woohoooo Thursday, October 13, 2011 1:23 PM
    • Marked as answer by Ho Duy Loi Thursday, October 13, 2011 2:56 PM
    Thursday, October 13, 2011 1:14 PM
  • i'm not making some kind of destop cleanup tool. I just check a.xls, if it has macros  then I will delete those macros, I got it. But, if a.xls has some link to some another *.xls then code delete macro not run. here i my code.

     

     public static bool DeleteMacro(string file,string pass)
            {
                bool flag = false;
                try
                {

                    Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                    Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(file, Type.Missing, Type.Missing, 6, Type.Missing, Type.Missing, Type.Missing, Type.Missing, ",", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    //xlApp.Visible = true;


                    Microsoft.Vbe.Interop.Window projectwindow = null;

                    //Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

                    for (int i = 1; i < xlApp.VBE.Windows.Count; i++)
                    {

                        if (xlApp.VBE.Windows.Item(i).Caption.Substring(0, 7) == "Project")
                        {

                            projectwindow = xlApp.VBE.Windows.Item(i);

                            projectwindow.SetFocus();

                            break;

                        }

                    }

                    //Excel.Workbook xlWorkBook  = xlApp.Workbooks.Open(@sourceFile, Type.Missing, Type.Missing, 6, Type.Missing, Type.Missing, Type.Missing, Type.Missing, ",", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    //xlApp.Visible = true;
                    xlApp.VBE.MainWindow.Visible = true;

                    xlApp.DisplayAlerts = false;

                    xlApp.VBE.ActiveVBProject = xlWorkBook.VBProject;



                    System.Windows.Forms.SendKeys.SendWait("{ENTER}");

                    System.Windows.Forms.SendKeys.SendWait(pass);

                    System.Windows.Forms.SendKeys.SendWait("{ENTER}");

                    xlApp.VBE.MainWindow.Visible = false;
                   
                    Microsoft.Vbe.Interop.VBComponents com = xlWorkBook.VBProject.VBComponents;

                    foreach (Microsoft.Vbe.Interop.VBComponent c in com)
                    {
                        if (c.Type == Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_StdModule)
                        {
                            com.Remove(c);
                        }
                    }
                    xlWorkBook.Save();
                    //xlWorkBook.SaveCopyAs(file);
                    xlWorkBook.Close(false, true, true);
                   
                    xlApp.Workbooks.Close();
                   
                    flag = true;
                }
                catch (Exception ex)
                {
                    flag = false;
                }
                return flag;
            }

     

    Thursday, October 13, 2011 3:00 PM
  • I seeee... Why don't you simply check whether a file is indeed an Excel file and not a link before you execute your code on it? That way your code won't try to do something with a link that can't be done, producing errors!

    ??

    By the way, although I don't mind the rep :D, I think it only fair to advise that you should only mark my posts as answers if they actually answer your question and make you able to do what you want. If is not an answer but merely helpful, just click the up arrow to mark as helpful. If you mark every reply as an answer, it is not very useful to other readers.


    James Finch (MCDST) -- Please vote as helpful if you found this post helpful, or mark as answer if it answered your question.


    • Edited by Woohoooo Thursday, October 13, 2011 3:16 PM typo
    Thursday, October 13, 2011 3:12 PM
  • how can I check whether a file is indeed an Excel file and not a link?
    Friday, October 14, 2011 4:35 AM
  • It is far from foolproof, but the simplest way would be to check whether the file has an extension that matches an Excel file type (i.e. xls, .xlsx, xlsm). This is very easy using a FileInfo object (in System.IO):

    string path = @"C:\Users\Woohooo\Pictures\untitled.png";
    FileInfo fi = new FileInfo(path);
    if (fi.Extension == ".xls" |
        fi.Extension == ".xlsx" |
        fi.Extension == ".xlsm")
    {
        // Perform operations on the file!! Or not, in this case. ^^
    }
    


    Of course if someone gave, say, a picture an Excel extension then your code would attempt to perform operations on it... Not good. But logically you wouldn't want to perform operations on these files anyway! So just make sure you catch exceptions.
    James Finch (MCDST) -- Please vote as helpful if you found this post helpful, or mark as answer if it answered your question.
    Friday, October 14, 2011 7:52 AM