Entire thing for reference
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System.IO;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using acApp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace ProjLog
{
public class Plog2012
{
[CommandMethod("PLog")]
public static void ProjectLog()
{
Document doc = acApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
string DwgPath = Path.GetDirectoryName(acApp.DocumentManager.MdiActiveDocument.Name);
string ProjNum = DwgPath.Substring(DwgPath.LastIndexOf("\\") + 1);
PromptSelectionOptions pso = new PromptSelectionOptions();
TypedValue[] filter = new TypedValue[2] { new TypedValue((int)DxfCode.Start, "INSERT"), new TypedValue((int)DxfCode.BlockName, "VTEP-INFO,TEP-INFO,TEP-CIP,VTEP-CIP,VUES-INFO,UES-INFO,UES-CIP,VUES-CIP") };
SelectionFilter selFilter = new SelectionFilter(filter);
PromptSelectionResult psr = ed.SelectAll(selFilter);
if (psr.Status != PromptStatus.OK)
{
acApp.ShowAlertDialog("Invalid Titleblock in drawing!");
return;
}
string strDwgLine1 = GetAttributeValueFromBlock(db, psr.Value.GetObjectIds()[0], "TITLE1");
string strDwgLine2 = GetAttributeValueFromBlock(db, psr.Value.GetObjectIds()[0], "TITLE2");
string strDwgNum = GetAttributeValueFromBlock(db, psr.Value.GetObjectIds()[0], "NUM");
string strDwgVendor = GetAttributeValueFromBlock(db, psr.Value.GetObjectIds()[0], "VENDORNAME");
string strDwgVendorNumber = GetAttributeValueFromBlock(db, psr.Value.GetObjectIds()[0], "VENDORNUMBER");
string strUserPath = GetUser(System.Environment.UserName);
string strMyFile = string.Concat(strUserPath, "\\", ProjNum, "\\", ProjNum, ".xlsx");
string strFilename = string.Concat(ProjNum + ".xlsx");
Excel.Application xlApp;
bool xLopen = IsExcelRunning();
xlApp = (Excel.Application)GetOrCreateInstance("Excel.Application");
Workbook wb;
if (xlApp.Workbooks.Count < 1)
{
wb = xlApp.Workbooks.Open(strMyFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
else
{
wb = xlApp.Workbooks[strFilename];
}
Worksheet ws = wb.Sheets["Sheet1"] as Worksheet;
xlApp.Visible = true;
xlApp.UserControl = false ;
Excel.Range currentFind = null;
Excel.Range firstFind = null;
currentFind = (Excel.Range)ws.Columns["B", Type.Missing];
firstFind = currentFind.Find(strDwgNum, Type.Missing, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlWhole, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, 0, 0, Type.Missing);
string foundAddress = firstFind.get_Address(Type.Missing, Type.Missing, Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing);
firstFind.Activate();
int rowNum = firstFind.Row;
if (!string.IsNullOrEmpty(strDwgVendor))
{
ws.Cells[rowNum, 5] = strDwgLine1 + "," + strDwgLine2 + "/" + strDwgVendor + "," + strDwgVendorNumber;
}
else
{
ws.Cells[rowNum, 5] = strDwgLine1 + "," + strDwgLine2;
}
ws.Cells[rowNum, 1] = GetAttributeValueFromBlock(db, psr.Value.GetObjectIds()[0], "SHT");
ws.Cells[rowNum, 3] = GetAttributeValueFromBlock(db, psr.Value.GetObjectIds()[0], "REV");
ws.Cells[rowNum, 4] = GetAttributeValueFromBlock(db, psr.Value.GetObjectIds()[0], "CODE");
if (xLopen)
{
acApp.ShowAlertDialog("Excel will now close!");
}
wb.Close(true, Type.Missing, Type.Missing);
xlApp.Quit();
Marshal.FinalReleaseComObject(xlApp);
//GC.Collect(2);
//GC.Collect(2);
//GC.Collect(2);
}
public static bool IsExcelRunning()
{
Process[] pros = Process.GetProcesses();
foreach (var process in pros)
{
if (process.ProcessName == "EXCEL")
{
return true;
}
}
return false;
}
static object GetOrCreateInstance(string appName)
{
try { return GetInstance(appName); }
catch { return CreateInstance(appName); }
}
static object GetInstance(string appName)
{
return Marshal.GetActiveObject(appName);
}
static object CreateInstance(string appName)
{
return Activator.CreateInstance(Type.GetTypeFromProgID(appName));
}
public static string GetAttributeValueFromBlock(Database db, ObjectId blockRefId, string attName)
{
string textString = null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockReference blockRef = tr.GetObject(blockRefId, OpenMode.ForRead) as BlockReference;
if (blockRef != null)
{
foreach (ObjectId id in blockRef.AttributeCollection)
{
AttributeReference attRef = tr.GetObject(id, OpenMode.ForRead) as AttributeReference;
if ((attRef != null) && (attRef.Tag.ToUpper() == attName))
{
textString = attRef.TextString;
break;
}
}
}
tr.Commit();
}
return textString;
}
private static string GetUser(string User)
{
switch (User.ToUpper())
{
case "UA02038":
return @"\\tuslpna01\autocad\Reproductions_Area\000-Hall";
case "UA50151":
return @"\\tuslpna01\autocad\Reproductions_Area\000-Richardson";
case "UA00648":
return @"\\tuslpna01\autocad\Reproductions_Area\000-Livingstone";
case "UA50270":
return @"\\tuslpna01\autocad\Reproductions_Area\000-Dominguez";
case "UA00071":
return @"\\tuslpna01\autocad\Reproductions_Area\000-Taylor";
case "UA50050":
return @"\\tuslpna01\autocad\Reproductions_Area\000-Lopez";
case "UA00664":
return @"\\tuslpna01\autocad\Reproductions_Area\000-Olivas";
case "UA50463":
return @"\\tuslpna01\autocad\Reproductions_Area\000-Padilla";
case "UA50271":
return @"\\tuslpna01\autocad\Reproductions_Area\000-ALVIRA";
case "UA51273":
return @"\\tuslpna01\autocad\Reproductions_Area\000-Heritage";
default:
return @"No Path Defined";
}
}
}
}