View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Steve Kirk (Insomniac Games) Steve Kirk (Insomniac Games) is offline
external usenet poster
 
Posts: 4
Default SheetChange event crashes using Range.set_value (C#)

When I get a SheetChange event that contains a cell using my custom function
I cannot modify the value of any cells in the target range.
range is the target range passed into the sheet change event
gid_col is the column I wish to validate


for(int a = 1; a <= range.Areas.Count; a++)
{
Excel.Range area = range.Areas[a];
for(int r = 0; r < area.Rows.Count; r++)
{
//Excel.Range cell = (Excel.Range)area.Cells[r,1];
int row = area.Row + r;
if((row_changes[row].Equals(3))
&&(area.Column <= gid_col)
&&(area.Column+area.Columns.Countgid_col))
{
Excel.Range cell =
(Excel.Range)area.Cells[r+1,gid_col+1-area.Column];
String f = (String)cell.Formula;
try {
cell.set_Value(Excel.XlRangeValueDataType.xlRangeV alueDefault, "wtf"); }
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("Sheet change
error\n" + ex.ToString(),"Localization Addin");
}
}
}
area = null;
}


the code works fine if no cell in the target range uses my custom function,
but blows up with this error otherwise...

System.Runtime.InteropServices.COMException(0x800A 03EC): Exception from
HRESULT 0x800A03EC.
at System.RuntimeTYpe.ForwardCallToInvokeMember(Strin g memberName, ...)
at Microsoft.Office.Interop.Excel.Range.set_Value(Obj ect
RangeValueDataType, Object)


also if I try to change other cell properties, like Font.Italic it has no
effect if my custom function is used by any cell in the target

This is the code for my custom function
using System;
using System.Windows;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace LocalizationFunctions
{
using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;

/// <summary
/// Summary description for Class1.
/// </summary
//[GuidAttribute("EE98CECC-2DEF-4d0b-B707-1985711163F5"),
ProgId("LocalizationFunctions.Functions")]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class Functions
{
public Functions()
{
//
// TODO: Add constructor logic here
//
}
public String TAG(Excel.Range range)
{
String result = "";
try
{
for(int a = 1; a <= range.Areas.Count; a++)
{
Excel.Range area = range.Areas[a];
for(int r = 1; r <= area.Rows.Count; r++)
{
for(int c = 1; c <= area.Columns.Count; c++)
{
Excel.Range _cell = (Excel.Range)area.Cells[r,c];

if(_cell.Value2 != null)
{
String val = _cell.Value2.ToString();
if(val.Length0)
{
if(result.Length0)
{
result += "_";
}
result += val;
}
}
//_cell = null;
}
}
//area = null;
}
result = result.Replace(' ','_');
result = result.Replace('\n','_');
result = result.Replace('\t','_');
//result = (String)range.Value2;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("Bugger2!\n"
+ex.ToString(),"Localization Function");

result = "ERROR!";
}

return result;
}

[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type type)
{
Registry.ClassesRoot.CreateSubKey(GetSubKeyName(ty pe));
Registry.ClassesRoot.CreateSubKey("CLSID\\{" +
type.GUID.ToString().ToUpper() + "}\\Programmable");
RegistryKey key = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(
"CLSID\\{" + type.GUID.ToString().ToUpper() +
"}\\InprocServer32");
key.SetValue("",
System.Environment.GetFolderPath(Environment.Speci alFolder.System)
");
}

[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type type)
{
Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(ty pe),false);
}

private static string GetSubKeyName(Type type)
{
string s = @"CLSID\{" + type.GUID.ToString().ToUpper() +
@"}\Programmable";
return s;
}

}
}

Any ideas?