Author Topic: ...not declared. It may be inaccessible due to its protection level.  (Read 20834 times)

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: ...not declared. It may be inaccessible due to its protection level.
« Reply #30 on: May 23, 2012, 01:05:35 PM »
yea, thats what is so frustrating.  Here is more of the code
Code: [Select]
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);
        }
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: ...not declared. It may be inaccessible due to its protection level.
« Reply #31 on: May 23, 2012, 01:06:31 PM »
Entire thing for reference
Code: [Select]
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";
            }
        }
    }
}
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: ...not declared. It may be inaccessible due to its protection level.
« Reply #32 on: May 24, 2012, 07:44:33 AM »
I can't resist so I'll trow this out there. Anytime I'm trying to write/read excel files from AutoCAD I use http://closedxml.codeplex.com/.  It's based off of MS OpenXML.  It's so much easier than trying to use COM, it's syntax makes more sense to me, and MS Excel doesn't have to be installed on the machine.
Revit 2019, AMEP 2019 64bit Win 10