Here is the finished product at this point
[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 = false;
try
{
xlApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
xLopen = true;
}
catch
{
xlApp = new Microsoft.Office.Interop.Excel.Application();
}
Workbook 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);
Worksheet ws = wb.Sheets["Sheet1"] as Worksheet;
xlApp.Visible = true;
xlApp.UserControl = true;
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)
{
wb.Save();
}
else
{
wb.Close(true, Type.Missing, Type.Missing);
xlApp.Quit();
}
Marshal.FinalReleaseComObject(xlApp);
}
and the helper methods
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";
default:
return @"No Path Defined";
}