public void LookupItemByCKey(string TemplateCKey, string ItemCKey = "") { string curPath = ""; if (_oAccess == null) { try { _oAccess = new Microsoft.Office.Interop.Access.Application(); _oAccess.Visible = false; curPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath); curPath = $"{curPath}\\{AccessPath}"; Application.UseWaitCursor = true; Application.DoEvents(); _oAccess.OpenCurrentDatabase(curPath); Application.UseWaitCursor = false; } catch (Exception ex) { //Probably, user has closed Access, the TE is closed, but we are still holding on to a handle to the Access process Application.UseWaitCursor = false; //if (!(_oAccess is null)) _oAccess.Visible = true; MessageBox.Show("LookupItemByCKey: There was a problem (0) loading TE in LookupItemByCKey. Details:\r\n\r\n" + ex); CloseAccess(); return; } } if (_oAccess != null) { try { object oMissing = System.Reflection.Missing.Value; if (_oAccess.hWndAccessApp() > 0) { try { BringToFront(); RunAccessFunction("InvokeLookup", TemplateCKey, ItemCKey); if (_oAccess != null) { _oAccess.Visible = true; } //RunAccessFunction("cboTemplateRequery", TemplateCKey); } catch (Exception ex) { //Probably, user has closed Access, the TE is closed, but we are still holding on to a handle to the Access process MessageBox.Show("LookupItemByCKey: There was a problem (1) loading TE in LookupItemByCKey. Details:\r\n\r\n" + ex); CloseAccess(); } } } catch (Exception ex) { //Probably, user has closed Access, the TE is closed, but we are still holding on to a handle to the Access process MessageBox.Show("There was a problem (2) loading TE in LookupItemByCKey"); CloseAccess(); } } }
public void GetProcessId_Test() { var microsoftAccess = new Microsoft.Office.Interop.Access.Application(); microsoftAccess.OpenCurrentDatabase(@"D:\MSAccessDatabase\MSAccessDatabase.accdb"); var myName = microsoftAccess.Run("GetName"); int id; GetWindowThreadProcessId(microsoftAccess.hWndAccessApp(), out id); var result = Process.GetProcessById(id); Assert.IsNotNull(result); Console.WriteLine(result.Id); if (microsoftAccess != null) { microsoftAccess.Quit(); Marshal.ReleaseComObject(microsoftAccess); microsoftAccess = null; } }
public static void LookupItemByCKey(string TemplateCKey, string ItemCKey) { if (_oAccess == null) { _oAccess = new Microsoft.Office.Interop.Access.Application(); _oAccess.Visible = false; string path = System.Windows.Forms.Application.ExecutablePath; path = System.IO.Path.GetDirectoryName(path); _oAccess.OpenCurrentDatabase(path + "\\TE\\eCC_TE.adp"); } _oAccess.Visible = true; try { object oMissing = System.Reflection.Missing.Value; if (_oAccess.hWndAccessApp() > 0) { try { RunAccessFunction("InvokeLookup", TemplateCKey, ItemCKey); } catch { //if exception occurrs here most likely Access instance from this application was closed by the user //start a new instance and try again _oAccess = new Microsoft.Office.Interop.Access.Application(); _oAccess.Visible = false; string path = System.Windows.Forms.Application.ExecutablePath; path = System.IO.Path.GetDirectoryName(path); _oAccess.OpenCurrentDatabase(path + "\\TE\\eCC_TE.adp"); _oAccess.Visible = true; RunAccessFunction("InvokeLookup", TemplateCKey, ItemCKey); } } } catch (Exception ex) { throw new Exception("Error in TEInterop: " + ex.Message); } }