Skip to content

Instantly share code, notes, and snippets.

@Benshi
Last active March 15, 2023 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Benshi/991161becbbaa1ffaa4576334c3de40a to your computer and use it in GitHub Desktop.
Save Benshi/991161becbbaa1ffaa4576334c3de40a to your computer and use it in GitHub Desktop.
[C#] .NET 6 で、GetActiveObject を使って VS/Access/Word/Excel/PowerPoint で開いているドキュメントのパスを得る
// this code for .NET 6
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
if (OperatingSystem.IsWindows())
{
// VS2022=17.0, VS2019=16.0, VS2017=15.0, VS2015=14.0, VS2013=12.0, VS2012=11.0, VS2010=10.0, VS2008=9.0, VS2005=8.0, VS2003=7.1, VS2002=7
foreach (var v in new []{ "", ".17.0", ".16.0", ".15.0", ".14.0", ".12.0", "11.0", ".10.0", ".9.0"})
{
dynamic? dte = Example.GetObject($"VisualStudio.DTE{v}");
if (dte is not null)
{
try
{
var solution = dte.Solution;
Console.WriteLine($"Visual Studio{v}: {solution?.FullName}");
Marshal.FinalReleaseComObject(solution);
}
catch (Exception) { }
Marshal.FinalReleaseComObject(dte);
}
}
dynamic? accessApp = Example.GetObject("Access.Application");
if (accessApp is not null)
{
var database = accessApp.CurrentDb(); // プロパティではなくメソッド
var accessHwnd = accessApp.hWndAccessApp(); // プロパティではなくメソッド
Console.WriteLine($"Access: {database.Name}, hWnd=0x{accessHwnd:X}");
var screen = accessApp.Screen;
var form = default(dynamic);
try
{
if ((form = screen?.ActiveForm) is not null)
{
Console.WriteLine($"\thWnd=0x{form.Hwnd:X}, Caption={form.Caption}");
Marshal.ReleaseComObject(form);
}
}
catch (Exception) { }
finally
{
if (form is not null)
{
Marshal.FinalReleaseComObject(form);
}
}
Marshal.FinalReleaseComObject(screen);
Marshal.FinalReleaseComObject(database);
Marshal.FinalReleaseComObject(accessApp);
}
dynamic? wordApp = Example.GetObject("Word.Application");
if (wordApp is not null)
{
var document = wordApp.ActiveDocument;
Console.WriteLine($"Word: Saved = {document.Saved}, {document.FullName}");
if (wordApp.ActiveWindow is { } window)
{
Console.WriteLine($"\tHwnd=0x{window.Hwnd:X}, Caption={window.Caption}");
Marshal.ReleaseComObject(window);
}
Marshal.FinalReleaseComObject(document);
Marshal.FinalReleaseComObject(wordApp);
}
dynamic? excelApp = Example.GetObject("Excel.Application");
if (excelApp is not null)
{
var book = excelApp.ActiveWorkbook;
Console.WriteLine($"Excel: Instance={excelApp.HinstancePtr}, Saved = {book.Saved}, {book.FullName}");
if (excelApp.ActiveWindow is { } window)
{
Console.WriteLine($"\tHwnd=0x{window.Hwnd:X}, Caption={window.Caption}");
Marshal.ReleaseComObject(window);
}
Marshal.FinalReleaseComObject(book);
Marshal.FinalReleaseComObject(excelApp);
}
dynamic? pptApp = Example.GetObject("PowerPoint.Application");
if (pptApp is not null)
{
var presentation = pptApp.ActivePresentation;
Console.WriteLine($"PowerPoint: Saved = {presentation.Saved}, {presentation.FullName}");
if (pptApp.ActiveWindow is { } window)
{
// Console.WriteLine($"\tHWND=0x{window.HWND:X}, Caption={window.Caption}");
Console.WriteLine($"\tCaption={window.Caption}");
Marshal.ReleaseComObject(window);
}
Marshal.FinalReleaseComObject(presentation);
Marshal.FinalReleaseComObject(pptApp);
}
}
static class Example
{
public static object? GetObject(string progID)
{
try
{
CLSIDFromProgID(progID, out Guid clsid);
return GetActiveObject(ref clsid, IntPtr.Zero);
}
catch
{
return null;
}
}
[DllImport("ole32.dll", PreserveSig = false)]
static extern void CLSIDFromProgID([MarshalAs(UnmanagedType.LPWStr)] string progId, out Guid clsid);
[DllImport("oleaut32.dll", PreserveSig = false)]
[return: MarshalAs(UnmanagedType.Interface)]
static extern object GetActiveObject(ref Guid rclsid, IntPtr reserved);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment