InteropServices Issue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: InteropServices Issue

  1. #1
    Join Date
    Sep 2013
    Location
    Wisconsin
    Posts
    5

    InteropServices Issue

    Hello,
    I have been attempting for weeks to find a good way to bind my C# app to Excel and/or Access. Since my users may or may not have the target document open, I need a way to either bind to the running instance, or launch the correct document. I have chosen to use System.Runtime.InteropServices.Marshal.BindToMoniker. I have had a fair amount of success after much trial and error, but I am not running into an issue with opening multiple and unique documents. For example, if my user has three different entries in my app, each will be linked to its own excel doc. My "Open" routine loops through all entries, and launches, or binds to, the appropriate workbook. All three appear to be open, but only one shows in the taskbar, and the rest appear hidden. If I open the VBA editor I can see all three are open, and access the macros.

    Code:
    bool _AppOpen = false;
    try
                {
                    _Book = System.Runtime.InteropServices.Marshal.BindToMoniker(base.Pe.DestApp) as Excel.Workbook;
                    _App = _Book.Parent;
                    _App.Visible = true;
                    _AppOpen = _App.Run("AppBind");
                    if (_AppOpen) { base.Pe.AppStatus = AppStatus.AppTested; }
                    else { base.Pe.AppStatus = AppStatus.AppOpen; }
                    return true;
                }
                catch
                {
                    Process[] proc = Process.GetProcessesByName("Excel");
                    if (proc.Length < 1)
                       _App = new Excel.Application();
                    else
                       _App = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application") as Excel.Application;
                       _Book = _App.Workbooks.Open(base.Pe.DestApp);
                       _App.Visible = true;
                       _AppOpen = _App.Run("AppBind");
                       return true;
                }
    "AppBind" is a VBA function that returns True, to test that the workbook is, in fact, open. base.Pe.DestApp is the location and filename of the workbook. Hopefully I have explained enough to have this make sense.

    Thanks in advance,
    Brandon

  2. #2
    Join Date
    Sep 2013
    Location
    Wisconsin
    Posts
    5

    Re: InteropServices Issue

    Of course it takes asking the question publicly before finding my answer. For anyone else that may be interested, I cast my book to a worksheet, then activated.

    Code:
                    _Book = System.Runtime.InteropServices.Marshal.BindToMoniker(base.Pe.DestApp) as Excel.Workbook;
                    _App = _Book.Parent;
                    _App.Visible = true;
                    ((Microsoft.Office.Interop.Excel._Worksheet)_Book).Activate();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center