Click here to Skip to main content
15,899,754 members
Home / Discussions / C#
   

C#

 
AnswerRe: pin it to whatever version of VS you have - windows 8.1 Pin
Richard MacCutchan2-Sep-14 7:39
mveRichard MacCutchan2-Sep-14 7:39 
QuestionPopulate Checkboxlist from Database, and Populate second checkboxlist from selected values in first checkboxlist, all with databases Pin
Member 1004319229-Aug-14 18:21
Member 1004319229-Aug-14 18:21 
AnswerRe: Populate Checkboxlist from Database, and Populate second checkboxlist from selected values in first checkboxlist, all with databases Pin
Richard MacCutchan29-Aug-14 23:00
mveRichard MacCutchan29-Aug-14 23:00 
AnswerRe: Populate Checkboxlist from Database, and Populate second checkboxlist from selected values in first checkboxlist, all with databases Pin
Kornfeld Eliyahu Peter30-Aug-14 9:06
professionalKornfeld Eliyahu Peter30-Aug-14 9:06 
AnswerRe: Populate Checkboxlist from Database, and Populate second checkboxlist from selected values in first checkboxlist, all with databases Pin
V.31-Aug-14 8:10
professionalV.31-Aug-14 8:10 
GeneralRe: Populate Checkboxlist from Database, and Populate second checkboxlist from selected values in first checkboxlist, all with databases Pin
Paul Cong2-Sep-14 20:47
Paul Cong2-Sep-14 20:47 
GeneralRe: Populate Checkboxlist from Database, and Populate second checkboxlist from selected values in first checkboxlist, all with databases Pin
V.2-Sep-14 20:59
professionalV.2-Sep-14 20:59 
QuestionNeed Help!! Pin
Member 1104570529-Aug-14 5:57
Member 1104570529-Aug-14 5:57 
AnswerRe: Need Help!! Pin
OriginalGriff29-Aug-14 8:24
mveOriginalGriff29-Aug-14 8:24 
QuestionRe: Need Help!! Pin
Eddy Vluggen29-Aug-14 11:53
professionalEddy Vluggen29-Aug-14 11:53 
AnswerRe: Need Help!! Pin
Pete O'Hanlon29-Aug-14 12:09
mvePete O'Hanlon29-Aug-14 12:09 
QuestionAfter create setup Window form Application Following Error:- Pin
Nishant.Chauhan8029-Aug-14 2:17
Nishant.Chauhan8029-Aug-14 2:17 
AnswerRe: After create setup Window form Application Following Error:- Pin
Dave Kreskowiak29-Aug-14 2:32
mveDave Kreskowiak29-Aug-14 2:32 
AnswerRe: After create setup Window form Application Following Error:- Pin
Richard MacCutchan29-Aug-14 2:35
mveRichard MacCutchan29-Aug-14 2:35 
Questionhow to dropdownlist foreign keys? Pin
Member 1102146428-Aug-14 23:20
Member 1102146428-Aug-14 23:20 
AnswerRe: how to dropdownlist foreign keys? Pin
Pete O'Hanlon28-Aug-14 23:35
mvePete O'Hanlon28-Aug-14 23:35 
QuestionJSON Parsing Data Pin
Alex.bordei28-Aug-14 22:15
Alex.bordei28-Aug-14 22:15 
AnswerRe: JSON Parsing Data Pin
Freak3028-Aug-14 23:00
Freak3028-Aug-14 23:00 
GeneralRe: JSON Parsing Data Pin
Alex.bordei29-Aug-14 2:43
Alex.bordei29-Aug-14 2:43 
QuestionC# connection between two SQLExpress servers on one machine Pin
Member 1103943928-Aug-14 18:44
Member 1103943928-Aug-14 18:44 
AnswerRe: C# connection between two SQLExpress servers on one machine Pin
Kornfeld Eliyahu Peter28-Aug-14 20:16
professionalKornfeld Eliyahu Peter28-Aug-14 20:16 
GeneralRe: C# connection between two SQLExpress servers on one machine Pin
Member 1103943928-Aug-14 20:28
Member 1103943928-Aug-14 20:28 
GeneralRe: C# connection between two SQLExpress servers on one machine Pin
Paul Conrad29-Aug-14 18:47
professionalPaul Conrad29-Aug-14 18:47 
AnswerRe: C# connection between two SQLExpress servers on one machine Pin
PIEBALDconsult2-Sep-14 8:24
mvePIEBALDconsult2-Sep-14 8:24 
QuestionUsing Process.Start on Excel Pin
mjackson1128-Aug-14 8:35
mjackson1128-Aug-14 8:35 
I am trying to use Process.Start to launch Excel. If I immediately grab the process returned by the Start method, any addins or extensions to Excel (xlls or xlam) will not work. If I wait until Excel finishes opening and loading all the addins, then grab the process, it works. Need them to work for this project.

I mashed up a class from code I found around to either grab a reference to an existing instance of Excel or to start a new one if it isn't running. Unfortunately, it doesn't work. If the Process.Start method is called, Excel starts loading. The WaitForInputIdle method waits for the message loop to start. But when the message loop starts, Excel is still loading things and has not created any child windows. So the class tries to enumerate the child windows but there are none. And I discovered that once you call EnumChildWindows() it "freezes" things in that it will not refer to a child window created subsequently even if you call it again after the child window is created. So loops to wait are out.

One solution is to recursively create new instances of the class to get around the EnumChildWindows problem, but I end up with 1000's of these instances.

As a kludge, I have a long Thread.Sleep in the code to wait until everything is open. It works correctly but I would like to know if there is a better way to determine if a process has finished opening, especially given the bug/feature in EnumChildWindows.

C#
public class ExcelInteropService
{
    private const string EXCEL_CLASS_NAME = "EXCEL7";

    private const uint DW_OBJECTID = 0xFFFFFFF0;

    private static Guid rrid = new Guid("{00020400-0000-0000-C000-000000000046}");

    public delegate bool EnumChildCallback(int hwnd, ref int lParam);

    [DllImport("Oleacc.dll")]
    public static extern int AccessibleObjectFromWindow(int hwnd, uint dwObjectID, byte[] riid, ref Microsoft.Office.Interop.Excel.Window ptr);

    [DllImport("User32.dll")]
    public static extern bool EnumChildWindows(int hWndParent, EnumChildCallback lpEnumFunc, ref int lParam);

    [DllImport("User32.dll")]
    public static extern int GetClassName(int hWnd, StringBuilder lpClassName, int nMaxCount);

    public static Microsoft.Office.Interop.Excel.Application GetExcelInterop(int? processId = null)
    {
        var p = processId.HasValue ? Process.GetProcessById(processId.Value) : Process.Start("excel.exe");
        p.WaitForInputIdle();
        System.Threading.Thread.Sleep(60000);
        Debug.Assert(p != null, "p != null");
        try {
            return new ExcelInteropService().SearchExcelInterop(p);
        }
        catch (Exception) {
            Debug.Assert(p != null, "p != null");
            return GetExcelInterop(p.Id);
        }
    }

    private bool EnumChildFunc(int hwndChild, ref int lParam)
    {
        var buf = new StringBuilder(128);
        GetClassName(hwndChild, buf, 128);
        if (buf.ToString() == EXCEL_CLASS_NAME) { lParam = hwndChild; return false; }
        return true;
    }

    private Microsoft.Office.Interop.Excel.Application SearchExcelInterop(Process p)
    {
        bool timeout = false;
        DateTime start = new DateTime();
        TimeSpan span = new TimeSpan();
        TimeSpan d = new TimeSpan(0, 1, 0);
        Microsoft.Office.Interop.Excel.Window ptr = null;
        int hwnd = 0;
        int hWndParent = 0;
        int hr = -1;

        Debug.Assert(p != null, "p != null");
        try {
            start = DateTime.Now;
            do {
                do {
                    i++;
                    hWndParent = (int)p.MainWindowHandle;
                    if (hWndParent == 0) {
                        Debug.WriteLine("MainWindowNotFound");
                        break;
                    }

                    EnumChildWindows(hWndParent, EnumChildFunc, ref hwnd);
                    if (hwnd == 0) {
                        Debug.WriteLine("ChildWindowNotFound");
                        break;
                    }

                    hr = AccessibleObjectFromWindow(hwnd, DW_OBJECTID, rrid.ToByteArray(), ref ptr);
                    if (hr < 0) {
                        Debug.WriteLine("AccessibleObjectNotFound");
                        break;
                    }
                    if (ptr != null)
                        return ptr.Application;
                } while (ptr == null);
                span = DateTime.Now - start;
                if (span > d)
                    timeout = true;
            } while (timeout == false);
        }
        catch (Exception ex) {
            Debug.Write("Search Exception - ");
            Debug.WriteLine(ex.Message);
            return null;
        }
        try {
            p.CloseMainWindow();
        }
        catch (Exception ex) {
            Debug.Write("CloseWinMain Exception = ");
            Debug.WriteLine(ex.Message);
        }
        return null;
    }
}

Mark Jackson

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.