How do I get the current excel filename in c# - c#

I am writing a VTSO application that should work across multiple excel document templates. I want the add in to check the document and attempt to select the one the user is using, however I cannot figure out how to grab the current filename in c# using VTSO

Try this example:
using System;
using Excel = Microsoft.Office.Interop.Excel;
namespace Foo
{
class Program
{
static void Main(string[] args)
{
var excelApplication = (Excel.Application)CustomMarshal.GetActiveObject("Excel.Application");
var currentWorkbook = excelApplication.ActiveWorkbook;
Console.WriteLine("Current active Excel Workbook: " + currentWorkbook.Name); // or FullName to get full path to opened file
Console.ReadKey();
}
}
}
CustomMarshal and GetActiveObject() explanations can be found here:
No definition found for GetActiveObject from System.Runtime.InteropServices.Marshal C#

Related

Why does Microsoft.Office.Interop.Excel require a full path name to open a file?

I have been trying for too long to open an Excel file in a C# application created in Visual Studio 2012. I finally found that I need to provide the full path name to the Excel file, even though it exists in the same folder as the executable. Why is that?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Excel.Application ExcelApp = new Excel.Application();
try
{
MessageBox.Show("Current directory: " + Directory.GetCurrentDirectory());
if (File.Exists("PLC01.xls"))
{
MessageBox.Show("Target file exists.");
}
else
{
MessageBox.Show("Target file does not exist.");
}
Excel.Workbook workbook = ExcelApp.Workbooks.Open(Directory.GetCurrentDirectory() + "\\PLC01.xls");
// Excel.Workbook workbook = ExcelApp.Workbooks.Open("PLC01.xls");
workbook.Close();
MessageBox.Show("Book opened and closed.");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
This version works. But if I comment out the first call to Open() and uncomment the second, which only gives the file name and not the full path, I am told that the file can't be found, even though the "File exists" message box appears, verifying its existence.
https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.getcurrentdirectory?view=netframework-4.8
Directory.GetCurrentDirectory -
"Gets the current working directory of the application"
"The application" here is your C# executable: the launched Excel application instance is not running in the same context as your C# executable and so its "current directory" isn't necessarily the same.
If you know the full path, just pass that to Excel - no need to try to pass only the file name.
FYI - GetCurrentDirectory may not be the most robust way to locate your Excel file
How can I get the application's path in a .NET console application?

How can I open AutoCAD 2015 through the .NET API

I've been browsing for a good hour and have yet to find something that would help with this. I'm working on opening AutoCAD from the .NET API in VS2013 using C#, but for some reason, I can never get AutoCAD to actually launch. I'm using the following code:
using System;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
namespace IOAutoCADHandler
{
public static class ACADDocumentManagement
{
[CommandMethod("ConnectToAcad")]
public static void ConnectToAcad()
{
AcadApplication acAppComObj = null;
// no version number so it will run with any version
const string strProgId = "AutoCAD.Application";
// Get a running instance of AutoCAD
try
{
acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
}
catch // An error occurs if no instance is running
{
try
{
// Create a new instance of AutoCAD
acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);
}
catch //// STOPS HERE
{
// If an instance of AutoCAD is not created then message and exit
// NOTE: always shows this box and never opens AutoCAD
System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" +
" could not be created.");
return;
}
}
// Display the application and return the name and version
acAppComObj.Visible = true;
System.Windows.Forms.MessageBox.Show("Now running " + acAppComObj.Name +
" version " + acAppComObj.Version);
// Get the active document
AcadDocument acDocComObj;
acDocComObj = acAppComObj.ActiveDocument;
// Optionally, load your assembly and start your command or if your assembly
// is demandloaded, simply start the command of your in-process assembly.
acDocComObj.SendCommand("(command " + (char)34 + "NETLOAD" + (char)34 + " " +
(char)34 + #"C:\Users\Administrator\Documents\All Code\main-libraries\IOAutoCADHandler\bin\Debug\IOAutoCADHandler.dll" + (char)34 + ") ");
acDocComObj.SendCommand("DRAWCOMPONENT");
}
}
Unfortunately, it always stops at the nested catch statement and always displays the popup box without opening AutoCAD. Any suggestions on how to at least make AutoCAD open for me?
EDIT: Error message
The issue is you're coding (correctly) to the AutoCAD interop interface. I recommend against that (due to potential version changes).
The other issue is that the documentation for AutoCAD plugins using the newer .net api is for plugins when AutoCAD is already running.
Final issue could be that the program Id of AutCAD is a mystery. I have resorted to making that a configurable setting, but default to "AutoCAD.Application", which will take the currently registered AutoCAD.Application on the production machine. If there are multiple versions installed on the machine and you want to be specific, then you could append the version number (which you'll need to research) to the ProgID like: "AutoCAD.Application.19", or "AutoCAD.Application.20" for 2015.
For the first issue, one technique is to use dynamics for the autoCad objects, particularly for creating instances. I have used the ObjectARX api for creating my application in a dummy project, and then switching to dynamics when I'm happy with the properties and method names.
In a standalone .Net application that starts AutoCAD you could use something like:
// I comment these out in production
//using Autodesk.AutoCAD.Interop;
//using Autodesk.AutoCAD.Interop.Common;
//...
//private static AcadApplication _application;
private static dynamic _application;
static string _autocadClassId = "AutoCAD.Application";
private static void GetAutoCAD()
{
_application = Marshal.GetActiveObject(_autocadClassId);
}
private static void StartAutoCad()
{
var t = Type.GetTypeFromProgID(_autocadClassId, true);
// Create a new instance Autocad.
var obj = Activator.CreateInstance(t, true);
// No need for casting with dynamics
_application = obj;
}
public static void EnsureAutoCadIsRunning(string classId)
{
if (!string.IsNullOrEmpty(classId) && classId != _autocadClassId)
_autocadClassId = classId;
Log.Activity("Loading Autocad: {0}", _autocadClassId);
if (_application == null)
{
try
{
GetAutoCAD();
}
catch (COMException ex)
{
try
{
StartAutoCad();
}
catch (Exception e2x)
{
Log.Error(e2x);
ThrowComException(ex);
}
}
catch (Exception ex)
{
ThrowComException(ex);
}
}
}
When there are several versions of AutoCAD installed on a computer, creating an instance with the ProgID "AutoCAD.Application" will run the latest version started on this computer by the current user. If the version of the Interop assemblies used does not match the version that is starting, you'll get a System.InvalidCastException with an HRESULT 0x80004002 (E_NOINTERFACE).
In your specific case, the {070AA05D-DFC1-4E64-8379-432269B48B07} IID in your error message is the GUID for the AcadApplicationinterface in R19 64-bit (AutoCAD 2013 & 2014). So there is an AutoCAD 2013 or 2014 that is starting, and you cannot cast this COM object to a 2015 type because 2015 is R20 (not binary compatible).
To avoid that, you can add a specific version to your ProgID (like "AutoCAD.Application.20" for AutoCAD 2015 (R20.0) to 2016 (R20.1)) to start the version matching your Interop assemblies or you can use late binding (eg. remove your references to Autodesk.AutoCAD.Interop* and use the dynamic keyword instead of the AutoCAD types).
In the last case, you will lost autocompletion, but your program will work with all the versions of AutoCAD.
Check also 32-bit vs 64-bit because TypeLib/Interop assemblies are not the same.
I open the application in a much straight-forward way. First, be sure to reference the correct type library. The one I am using is AutoCAD 2014 Type Library, located at:
c:\program files\common files\autodesk shared\acax19enu.tlb
To initialize the application:
using AutoCAD;
namespace test
{
class Program
{
static void Main(string[] args)
{
AutoCAD.AcadApplication app;
app = new AcadApplication();
app.Visible = true;
Console.Read();
}
}
}
Try this:
"sourcefile" is the original file
"newfile" is the new file
[CommandMethod("ModifyAndSaveas", CommandFlags.Redraw | CommandFlags.Session)]
public void ModifyAndSaveAs()
{
Document acDoc = Application.DocumentManager.Open(sourcefile);
Database acDB = acDoc.Database;
Transaction AcTran = acDoc.Database.TransactionManager.StartTransaction();
using (DocumentLock acLckDoc = acDoc.LockDocument())
{
using (AcTran)
{
BlockTable acBLT = (BlockTable)AcTran.GetObject(acDB.BlockTableId, OpenMode.ForRead);
BlockTableRecord acBLTR = (BlockTableRecord)AcTran.GetObject(acBLT[BlockTableRecord.ModelSpace], OpenMode.ForRead);
var editor = acDoc.Editor;
var SelectionSet = editor.SelectAll().Value;
foreach (ObjectId id in SelectionSet.GetObjectIds())
{
Entity ent = AcTran.GetObject(id, OpenMode.ForRead) as Entity;
//modify entities
}
AcTran.Commit();
}
}
acDB.SaveAs(newfile, DwgVersion.AC1021);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Tekkit
{
class Program
{
static void Main(string[] args)
{
//make sure to add last 2 using statements
ProcessStartInfo start = new ProcessStartInfo("calc.exe");
Process.Start(start);//starts the process
}
}
}

Retrieve query, form, and report properties from Access (.accdb) via a WPF application

Is there a way to get the objects (queries, forms, reports, macros, etc.) from an .accdb file? I am not looking for storing data, I want to examine the structure and design of those objects.
Edit: To make the question clear. I want to access those objects from C#, so that I can do automatic checking.
The following C# console app lists all of the controls in a specified Form:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace comAutoTest
{
class Program
{
static void Main(string[] args)
{
// this code requires the following COM reference in the project:
// Microsoft Access 14.0 Object Library
//
var objAccess = new Microsoft.Office.Interop.Access.Application();
objAccess.Visible = false;
objAccess.OpenCurrentDatabase(#"C:\Users\Public\Database1.accdb");
string formName = "MembersForm";
Console.WriteLine(String.Format("The form [{0}] contains the following controls:", formName));
objAccess.DoCmd.OpenForm(formName, Microsoft.Office.Interop.Access.AcFormView.acDesign);
Microsoft.Office.Interop.Access.Form frm = objAccess.Forms[formName];
foreach (Microsoft.Office.Interop.Access.Control ctl in frm.Controls)
{
Console.WriteLine();
Console.WriteLine(String.Format(" [{0}]", ctl.Name));
Console.WriteLine(String.Format(" {0}", ctl.GetType()));
}
objAccess.DoCmd.Close(Microsoft.Office.Interop.Access.AcObjectType.acForm, formName);
objAccess.CloseCurrentDatabase();
objAccess.Quit();
Console.WriteLine();
Console.WriteLine("Done.");
}
}
}
The output is:
The form [MembersForm] contains the following controls:
[LastName]
Microsoft.Office.Interop.Access.TextBoxClass
[Label0]
Microsoft.Office.Interop.Access.LabelClass
[MemberDonationsSubform]
Microsoft.Office.Interop.Access.SubFormClass
[MemberDonationsSubform Label]
Microsoft.Office.Interop.Access.LabelClass
[Command3]
Microsoft.Office.Interop.Access.CommandButtonClass
Done.
Edit: For Relationships, do something like this
Microsoft.Office.Interop.Access.Dao.Database cdb = objAccess.CurrentDb();
foreach (Microsoft.Office.Interop.Access.Dao.Relation rel in cdb.Relations)
{
Console.WriteLine(rel.Name);
}
There is a feature in Access that is called "Database Document". You can access it through the Database Tools ribbon panel. When you run the tool fol all objects it will generate a report called Objects Definition. You can print it, but I recommend you export it to a file of some sort (e.g. Excel, or Text). That way it will be easier to analyse.

How do I create the .docx document with Microsoft.Office.Interop.Word?

How do I create the .docx document with Microsoft.Office.Interop.Word from List? or the best way is to add docx.dll?
http://www.c-sharpcorner.com/UploadFile/scottlysle/using-the-docx-dll-to-programmatically-create-word-documents/
Update. May be my first question is a litle incorrect. What is the difference between Microsoft.Office.Interop.Word and DocX.dll? Do I need Microsft Word for creating and opening .docx document in both cases?
upd: It seems like openxml sdk is now being installed via nuget, take a look at their github repo https://github.com/OfficeDev/Open-XML-SDK
After installing OpenXML SDK you will able to reference DocumentFormat.OpenXml assembly: Add Reference -> Assemblies ->
Extensions -> DocumentFormat.OpenXml. Also you will need to reference WindowsBase.
Than you will be able to generate document, for example, like this:
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace MyNamespace
{
class Program
{
static void Main(string[] args)
{
using (var document = WordprocessingDocument.Create(
"test.docx", WordprocessingDocumentType.Document))
{
document.AddMainDocumentPart();
document.MainDocumentPart.Document = new Document(
new Body(new Paragraph(new Run(new Text("some text")))));
}
}
}
}
Also you can use Productivity Tool (the same link) to generate code from document. It can help to understand how work with SDK API.
You can do the same with Interop:
using System.Reflection;
using Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
namespace Interop1
{
class Program
{
static void Main(string[] args)
{
Application application = null;
try
{
application = new Application();
var document = application.Documents.Add();
var paragraph = document.Paragraphs.Add();
paragraph.Range.Text = "some text";
string filename = GetFullName();
application.ActiveDocument.SaveAs(filename, WdSaveFormat.wdFormatDocument);
document.Close();
}
finally
{
if (application != null)
{
application.Quit();
Marshal.FinalReleaseComObject(application);
}
}
}
}
}
But in this case you should reference COM type library Microsoft. Word Object Library.
Here are very useful things about COM interop: How do I properly clean up Excel interop objects?
If you don't want to use Microsoft interop office then
I really liked this
//Add reference DocX.dll
using Novacode;
// reference to the working document.
static DocX gDocument;
public void CreateWithOpenDoc(string _fileName, string _saveAs, int _LeadNo)
{
if (File.Exists(_fileName))
{
gDocument = DocX.Load(_fileName);
//--------------------- Make changes -------------------------------
// Strong-Type
Dictionary<string, string> changesList = GetChangesList(_LeadNo, dt.Rows[0]);
foreach (KeyValuePair<string, string> keyValue in changesList)
{
gDocument.ReplaceText(keyValue.Key.ToString().Trim(), keyValue.Value.ToString().Trim(), false);
}
//------------------------- End of make changes ---------------------
gDocument.SaveAs(_saveAs);
}
}
take reference
C-sharp corner
If you don't know how to access office 2016 interop objects, the link (https://social.msdn.microsoft.com/Forums/vstudio/en-US/55fe7d16-998b-4c43-9746-45ff35310158/office-2016-interop-assemblies?forum=exceldev) can help to you.
After this, You can try #Evgeny Timoshenko's example.
class Program
{
static void Main(string[] args)
{
Application application = null;
try
{
application = new Application();
var document = application.Documents.Add();
var paragraph = document.Paragraphs.Add();
paragraph.Range.Text = "some text";
string filename = GetFullName();
application.ActiveDocument.SaveAs(filename, WdSaveFormat.wdFormatDocument);
document.Close();
}
finally
{
if (application != null)
{
application.Quit();
Marshal.FinalReleaseComObject(application);
}
}
}
}

Can I add date/time to outputted filename in C# file.writeallbytes

I am writing an errorlog to to file in the same directory the script exists. Id like to potentially create a new folder as it writes as well as add date/time to the filenames so they 2nd doesnt save over the first.
Here is what I have so far:
File.WriteAllBytes("ErrorLog.txt")
Thanks!
You can create a valid Windows file name with DateTime in it like this:
string filename = "ErrorLogFolder" + DateTime.Now.ToString("dd-MM-yyyy_hh-mm-ss") + ".txt";
Take a look at this sample code for naming a file
using System;
using System.IO;
class Program
{
static void Main()
{
//
// Write file containing the date with BIN extension
//
string n = string.Format("text-{0:yyyy-MM-dd_hh-mm-ss-tt}.bin",
DateTime.Now);
File.WriteAllText(n, "aaa");
}
}

Categories