question

ansalc-1556 avatar image
0 Votes"
ansalc-1556 asked laurentcutey-3677 commented

Name does not exist in the current context

In the code below, the line

XlApp.ScreenUpdating = false;

throws the error: The name XlApp.ScreenUpdating does not exist in the current context

How can I fix it?

I can however refer within the same scope to Workbooks and Worksheets of the Excel App XlApp

If I include the line XlApp.ScreenUpdating = false; inside a method in the same class it works fine. But I need at the top to apply to all methods in the class



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;


namespace Sample
{

 public class EW: EWrapper
 {
  

     static Microsoft.Office.Interop.Excel.Application XlApp = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

     XlApp.ScreenUpdating = false; //throws error


     Worksheet Inventory_pos = XlApp.Workbooks["Inventory.xlsm"].Worksheets["pos"]; //no error
dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ansalc-1556 avatar image
0 Votes"
ansalc-1556 answered laurentcutey-3677 commented

Thank you Castorix31!

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Mark his answer as right answer, not your own message, or it won't help people to find the right answer...

0 Votes 0 ·
Castorix31 avatar image
0 Votes"
Castorix31 answered

You can put it in the Constructor, like :

 public class EW: EWrapper
 {
     public Microsoft.Office.Interop.Excel.Application XlApp = null;
     public EW()
     {
         if (Process.GetProcessesByName("excel").Count() > 0)
         {
             XlApp = Marshal.GetActiveObject("Excel.Application") as Microsoft.Office.Interop.Excel.Application;
             XlApp.ScreenUpdating = false;
         }
     }
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.