-
Jan 3rd, 2019, 10:06 AM
#1
Thread Starter
Addicted Member
Open Access document with C#?
I have this class in C# which i developed from examples:
Code:
class ManagedAccess:IDisposable
{
dynamic _access;
dynamic _doc;
bool _isDisposed = false;
public ManagedAccess(string filePath = "")
{
_access = new Microsoft.Office.Interop.Access.Application();
_access.Visible = true;
if (filePath.Equals(string.Empty))
{
_doc = _access.Workbooks.Add();
_doc.Activate();
}
else
{
_doc = _access.Workbooks.Open(filePath);
}
}
protected virtual void Dispose(bool isDisposing)
{
if (this._isDisposed)
return;
if (isDisposing)
{
this._access.Quit();
}
// Release unmanaged resources here
//if (this._excel != null)
//{
// System.Runtime.InteropServices.Marshal.ReleaseComObject(this._excel);
//}
// Indicate that the object has been disposed.
this._isDisposed = true;
}
~ManagedAccess()
{
this.Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
If you check the constructor there is the word "Workbooks" which suits when opening Excel application from C#.
Though Access usage will decrease, we still use old Access software and lot of apps are built with it, so I might need to open an Access program from C#.
What should i replace "Workbooks" with in the following code:
Code:
if (filePath.Equals(string.Empty))
{
_doc = _access.Workbooks.Add();
_doc.Activate();
}
else
{
_doc = _access.Workbooks.Open(filePath);
}
This is using Microsoft office Interop libraries and the IDisposable pattern in C#
-
Jan 3rd, 2019, 01:31 PM
#2
Re: Open Access document with C#?
This should help if you need to open an access database from c# code
http://www.vbforums.com/showthread.p...73#post5346573
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|