Click here to Skip to main content
15,890,123 members
Home / Discussions / C#
   

C#

 
AnswerRe: Modfying Print Document Pin
Emmet_Brown22-Jun-10 1:12
Emmet_Brown22-Jun-10 1:12 
QuestionRun a script on a linux box from Windows OS using windows-form written in C# Pin
ksaw12321-Jun-10 20:57
ksaw12321-Jun-10 20:57 
AnswerRe: Run a script on a linux box from Windows OS using windows-form written in C# Pin
Łukasz Nowakowski21-Jun-10 21:29
Łukasz Nowakowski21-Jun-10 21:29 
AnswerRe: Run a script on a linux box from Windows OS using windows-form written in C# Pin
Bernhard Hiller22-Jun-10 2:27
Bernhard Hiller22-Jun-10 2:27 
GeneralRe: Run a script on a linux box from Windows OS using windows-form written in C# Pin
Dave Kreskowiak22-Jun-10 3:54
mveDave Kreskowiak22-Jun-10 3:54 
QuestionExcel Exception from HRESULT: 0x800AC472 Pin
KaurGurpreet21-Jun-10 20:45
KaurGurpreet21-Jun-10 20:45 
AnswerMessage Closed Pin
21-Jun-10 22:29
stancrm21-Jun-10 22:29 
GeneralRe: Excel Exception from HRESULT: 0x800AC472 Pin
KaurGurpreet24-Jun-10 20:02
KaurGurpreet24-Jun-10 20:02 
I have created a simple application to show the problem:

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {

    Microsoft.Office.Interop.Excel.Application _objAppln;
    Workbook _objWorkBook;
    Workbooks _objWorkBooks;
    Worksheet _objWorkSheet;


    public Form1()
    {
      InitializeComponent();
      InitializeExcelObjectModel();
    }

    ~Form1()
    {
      DisposeExcelObjects();
    }

    void SaveExcel()
    {

      _objAppln.DisplayAlerts = false;//Since, we are using SaveFileDialog's overwrite prompt(control is on view). 
      
      _objWorkBook.SaveAs("C:\\tmp.xls",
        XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, XlSaveAsAccessMode.xlNoChange,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
      _objWorkBook.Close(true, "C:\\tmp.xls", false);
      _objAppln.DisplayAlerts = true;//restore back for other display alerts 
    }

    private void InitializeExcelObjectModel()
    {

      _objAppln = new Microsoft.Office.Interop.Excel.Application(); // To initialize excel file   
      //_objAppln.Visible = true; 
      if (_objAppln != null)
      {
        _objWorkBooks = _objAppln.Workbooks;
        _objWorkBook = _objWorkBooks.Add(Type.Missing); // To add workbook with sheets in excel file         
        _objWorkSheet = (Worksheet)_objAppln.ActiveSheet; // To get the current active sheet in excel file              

      }
    }

    public void DisposeExcelObjects()
    {

      System.Runtime.InteropServices.Marshal.ReleaseComObject(_objWorkSheet);
      System.Runtime.InteropServices.Marshal.ReleaseComObject(_objWorkBook);
      System.Runtime.InteropServices.Marshal.ReleaseComObject(_objWorkBooks);
      System.Runtime.InteropServices.Marshal.ReleaseComObject(_objAppln);
      _objWorkSheet = null; _objWorkBooks = null; _objWorkBooks = null; _objAppln = null;
    }

    private void button1_Click(object sender, EventArgs e)
    {
      string cell1 = string.Empty, cell2 = string.Empty;
      string[] chars = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T" };


      for (int row = 1; row < 1000; row++)
      {
        for (int column = 0; column < 20; column++)
        {
          cell1 = chars[column] + row.ToString();
          Range objRange = _objWorkSheet.get_Range(cell1, cell1);
          objRange.Value2 = cell1;
          System.Runtime.InteropServices.Marshal.ReleaseComObject(objRange);
          objRange = null;
        }
      }
      SaveExcel();
    }
  }
}


Project will need a reference of Microsoft.Office.Interop.Excel.dll

While button1_Click is processing, at that time if you open any other excel and just click on cells... it throws error.
Gurpreet

Questionhow do i turn my usb into an U3 smart drive Pin
edward remo21-Jun-10 18:38
edward remo21-Jun-10 18:38 
AnswerRe: how do i turn my usb into an U3 smart drive Pin
DaveyM6921-Jun-10 21:17
professionalDaveyM6921-Jun-10 21:17 
Questionneed help...(urgent) Pin
Ice_Freez0521-Jun-10 16:17
Ice_Freez0521-Jun-10 16:17 
AnswerRe: need help...(urgent) Pin
Mycroft Holmes21-Jun-10 17:48
professionalMycroft Holmes21-Jun-10 17:48 
AnswerRe: need help...(urgent) Pin
Roger Wright21-Jun-10 17:52
professionalRoger Wright21-Jun-10 17:52 
AnswerRe: need help...(urgent) Pin
Abhinav S21-Jun-10 18:30
Abhinav S21-Jun-10 18:30 
AnswerRe: need help...(urgent) Pin
yu-jian27-Jun-10 9:58
yu-jian27-Jun-10 9:58 
QuestionMy C# Project Connect to ocsp.verising.com Pin
I Believe In GOD21-Jun-10 9:10
I Believe In GOD21-Jun-10 9:10 
AnswerRe: My C# Project Connect to ocsp.verising.com Pin
Luc Pattyn21-Jun-10 9:18
sitebuilderLuc Pattyn21-Jun-10 9:18 
GeneralRe: My C# Project Connect to ocsp.verising.com Pin
I Believe In GOD21-Jun-10 9:26
I Believe In GOD21-Jun-10 9:26 
GeneralRe: My C# Project Connect to ocsp.verising.com Pin
freakyit21-Jun-10 9:44
freakyit21-Jun-10 9:44 
GeneralRe: My C# Project Connect to ocsp.verising.com [modified] Pin
Dave Kreskowiak21-Jun-10 9:54
mveDave Kreskowiak21-Jun-10 9:54 
AnswerRe: My C# Project Connect to ocsp.verising.com Pin
hammerstein0521-Jun-10 9:49
hammerstein0521-Jun-10 9:49 
AnswerRe: My C# Project Connect to ocsp.verising.com Pin
I Believe In GOD21-Jun-10 12:25
I Believe In GOD21-Jun-10 12:25 
GeneralRe: My C# Project Connect to ocsp.verising.com Pin
Luc Pattyn21-Jun-10 12:44
sitebuilderLuc Pattyn21-Jun-10 12:44 
GeneralHi! I need help for you Pin
hyps4u21-Jun-10 5:20
hyps4u21-Jun-10 5:20 
GeneralRe: Hi! I need help for you Pin
Dave Kreskowiak21-Jun-10 5:38
mveDave Kreskowiak21-Jun-10 5:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.