I have a problem when I throw my program. My program contsist to change a excel file, but when I try to do it, visual studio show me this missatge "Disconnected context was detected".
This is some parts of my code:
public static void read_file(string file, string dir, string pathOut, string pathLogOut)
{
string[] words = file.Split('.');
int LengthWord = words.Length;
string fileFormat = words[LengthWord - 1];
try
{
string fileDestiny = file.Replace(@"IN\\", "");
Console.WriteLine(fileDestiny);
System.IO.File.Copy(file, fileDestiny, true);
FileAttributes attributes = File.GetAttributes(fileDestiny);
File.SetAttributes(fileDestiny, FileAttributes.Normal);
Excel.Application xlApp;
Excel.Workbook xlWorkBook = null;
Excel.Range range = null;
var misValue = Type.Missing;//System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlApp.DisplayAlerts = false;
xlWorkBook = xlApp.Workbooks.Open(fileDestiny, misValue, misValue,
misValue, misValue, misValue, misValue, misValue, misValue,
misValue, misValue, misValue, misValue, misValue, misValue);
VBA.VBProject project = xlWorkBook.VBProject;
VBA.VBComponents VBComponents = project.VBComponents;
string filePath = null;
xlApp.VBE.ActiveVBProject.VBComponents.Add(VBA.vbext_ComponentType.vbext_ct_StdModule).CodeModule.AddFromFile(@"C:\IN\RemplazoString.bas");
xlApp.Run("MACRO");
}
// Guardar y cerrar
Excel.XlFileFormat FileFormat = xlWorkBook.FileFormat;
xlWorkBook.SaveAs(fileDestiny, FileFormat);
xlWorkBook.Close();
File.SetAttributes(fileDestiny, attributes);
// liberar
releaseObject(xlWorkBook);
releaseObject(xlApp);
IndexFile = IndexFile + 1;
using (StreamWriter sw = File.AppendText(pathLogOut))
{
sw.WriteLine(IndexFile + "." + file + " --> Hecho");
}
}
catch (Exception e)
{
Console.WriteLine("El fichero {0} no se guardo correctamente {1}", file, e.ToString());
}
}
public static void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
Console.WriteLine("Unable to release the object(object:{0})", obj.ToString());
}
finally
{
obj = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
I have this error:
"Transition into COM context 0x86b848 for this RuntimeCallableWrapper failed with the following error: System call failed. (Exception from HRESULT: 0x80010100 (RPC_E_SYS_CALL_FAILED)). This is typically because the COM context 0x86b848 where this RuntimeCallableWrapper was created has been disconnected or it is busy doing something else. Releasing the interfaces from the current COM context (COM context 0x86b6d8). This may cause corruption or data loss. To avoid this problem, please ensure that all COM contexts/apartments/threads stay alive and are available "
User contributions licensed under CC BY-SA 3.0