none
File Watcher RRS feed

  • Question

  • I have built a Filewatcher class that watches a folder and when an excel file is dumped in, Opens it and saves it to another directory. The trouble I am having is that is can't open the workbook. I have this on a created event. My code is show below. I appreciate any help with this. This is my first time at building a filewatcher.

    Private

    SubobjFsw_Created(ByValsender AsObject, ByVale AsSystem.IO.FileSystemEventArgs) HandlesobjFsw.Created

           

    RaiseEventFileCreated(e.FullPath)

           

    DimobjExcel AsObject= CreateObject("Excel.Application")

           

    DimFile AsObject= e.FullPath

           

    DimobjWorkbook AsObject= objExcel.Workbooks.Open(e.FullPath)

           

    DimstrFile AsString= "\\Fs2\Public\Service Connect E9\SigmaNest"& e.Name

            objExcel.Application.DisplayAlerts =

    False


           

    IfobjExcel.ActiveWorkbook("Sheet1").Range("A1") = ""Then


                MsgBox(

    "You are attempting to send a nest where the nested quanity does not match the required quanity")

           

    EndIf


            objExcel.ActiveWorkbook.Saveas(strFile)

            objExcel.ActiveWorkbook.Close()

            objExcel.application.Quit()

           

    OnErrorResumeNext


            Kill(

    "P:\000TASK SUMMARY SIGMANEST\COMPOSE NESTING\"& e.Name)

           

    OnErrorGoTo 0


    0:

       

    EndSub

    Wednesday, March 20, 2013 1:40 PM

Answers

  • Hi mbrown,

    You should add "Microsoft.Office.Interop.Excel.dll" reference to your project and then you can add the following code snippets to open a workbook:

    using Excel = Microsoft.Office.Interop.Excel;
    
    // Define Variables
    Excel.Application excelApp;
    Excel.Workbook excelWorkbook;
     Excel.Worksheet excelWorkSheet1;
    public void openWorkbook(string excelFile)
    {
    // Create an instance of Microsoft Excel and make it invisible
    excelApp = new Excel.Application();
    excelApp.Visible = false;
     // open a Workbook and get the active Worksheet
     excelWorkbook = excelApp.Workbooks.Open(excelFile);
     excelWorkSheet1 = excelWorkbook.ActiveSheet;
    }
    Thanks.

    Thursday, March 21, 2013 5:59 AM

All replies

  • How is the file created? It possible that it is still being used by whatever is creating it?

    You need to synchronize the two process so that you do not access the file before it is released.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Wednesday, March 20, 2013 3:58 PM
  • Hi mbrown,

    You should add "Microsoft.Office.Interop.Excel.dll" reference to your project and then you can add the following code snippets to open a workbook:

    using Excel = Microsoft.Office.Interop.Excel;
    
    // Define Variables
    Excel.Application excelApp;
    Excel.Workbook excelWorkbook;
     Excel.Worksheet excelWorkSheet1;
    public void openWorkbook(string excelFile)
    {
    // Create an instance of Microsoft Excel and make it invisible
    excelApp = new Excel.Application();
    excelApp.Visible = false;
     // open a Workbook and get the active Worksheet
     excelWorkbook = excelApp.Workbooks.Open(excelFile);
     excelWorkSheet1 = excelWorkbook.ActiveSheet;
    }
    Thanks.

    Thursday, March 21, 2013 5:59 AM