|
| 1 | +# ComAutoWrapper |
| 2 | + |
| 3 | +**ComAutoWrapper** is a lightweight, zero-Interop, fluent C# helper library for automating COM objects such as **Excel** and **Word** β without relying on bulky Primary Interop Assemblies (PIAs). |
| 4 | + |
| 5 | +βοΈ Fully dynamic |
| 6 | +βοΈ Typed property/method access |
| 7 | +βοΈ Introspectable |
| 8 | +βοΈ Ideal for WPF / Console / WinForms projects |
| 9 | +βοΈ Just **~30 KB** compiled DLL |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## π Features |
| 14 | + |
| 15 | +- **No Interop DLLs needed** |
| 16 | +- Lightweight COM helper for C# |
| 17 | +- Elegant dynamic wrappers: |
| 18 | + - `GetProperty<T>()`, `SetProperty()` |
| 19 | + - `CallMethod<T>()` |
| 20 | +- COM introspection (`ComTypeInspector`) |
| 21 | +- Excel selection utilities (`ComSelectionHelper`) |
| 22 | +- Safe release of COM objects |
| 23 | +- Compatible with: .NET 6, 7, 8, 9+ |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## π§ Examples |
| 28 | + |
| 29 | +### Get/Set COM Properties |
| 30 | + |
| 31 | +```csharp |
| 32 | +var excel = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application")!); |
| 33 | +ComInvoker.SetProperty(excel, "Visible", true); |
| 34 | + |
| 35 | +var workbooks = ComInvoker.GetProperty<object>(excel, "Workbooks"); |
| 36 | +var workbook = ComInvoker.CallMethod<object>(workbooks, "Add"); |
| 37 | +``` |
| 38 | + |
| 39 | +### Invoke COM Methods |
| 40 | + |
| 41 | +```csharp |
| 42 | +var sheet = ComInvoker.GetProperty<object>(workbook, "ActiveSheet"); |
| 43 | +var cell = ComInvoker.GetProperty<object>(sheet, "Cells"); |
| 44 | +ComInvoker.SetProperty(cell, "Item", new object[] { 1, 1 }, "Hello"); |
| 45 | +``` |
| 46 | + |
| 47 | +### Introspect COM Object |
| 48 | + |
| 49 | +```csharp |
| 50 | +var (methods, propsGet, propsSet) = ComTypeInspector.ListMembers(sheet); |
| 51 | +Console.WriteLine("Available methods:"); |
| 52 | +methods.ForEach(Console.WriteLine); |
| 53 | +``` |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +## β¨ Excel-Specific Helpers (Optional) |
| 58 | + |
| 59 | +Provided via the built-in `ComSelectionHelper`: |
| 60 | + |
| 61 | +| Method | Description | |
| 62 | +|--------|-------------| |
| 63 | +| `SelectCells(excel, sheet, "A1", "B3", "C5")` | Selects non-contiguous Excel cells | |
| 64 | +| `GetSelectedCellCoordinates(excel)` | Returns `(row, column)` for each selected cell | |
| 65 | +| `HighlightUsedRange(sheet)` | Highlights the used range with color | |
| 66 | + |
| 67 | +These helpers abstract away the quirks of Excel's COM object model. |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## π¦ NuGet Package |
| 72 | + |
| 73 | +Install via CLI: |
| 74 | + |
| 75 | +```bash |
| 76 | +dotnet add package ComAutoWrapper |
| 77 | +``` |
| 78 | + |
| 79 | +Or via Visual Studio NuGet UI. |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +## π» Requirements |
| 84 | + |
| 85 | +- Windows OS (COM-based) |
| 86 | +- .NET 6 / 7 / 8 / 9 |
| 87 | +- Microsoft Excel/Word must be installed |
| 88 | + |
| 89 | +> The library **does not embed Interop DLLs**. It uses late binding with proper error handling. |
| 90 | +
|
| 91 | +--- |
| 92 | + |
| 93 | +## π Related Project |
| 94 | + |
| 95 | +- [ComAutoWrapperDemo (GitHub)](https://github.com/pmonitor0/ComAutoWrapperDemo) |
| 96 | + WPF demo showcasing full Excel and Word automation using this wrapper. |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +## π Comparison: OpenXML vs COM Automation |
| 101 | + |
| 102 | +| Feature | OpenXML SDK | ComAutoWrapper | |
| 103 | +|--------|-------------|----------------| |
| 104 | +| Requires Excel Installed | β | β
| |
| 105 | +| Works on Locked/Password Files | β | β
| |
| 106 | +| Manipulate Active Excel Instance | β | β
| |
| 107 | +| Word Automation | β | β
| |
| 108 | +| File Size (DLL) | >10 MB | ~30 KB | |
| 109 | +| API Simplicity | Moderate | High (fluent & dynamic) | |
| 110 | +| Cell Selection / UI Interaction | β | β
| |
| 111 | +| UsedRange / Borders / Colors | β | β
| |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## π Acknowledgment |
| 116 | + |
| 117 | +This library is the result of an iterative collaboration between the author and ChatGPT. |
| 118 | +Special thanks to all early testers and contributors who shaped the API. |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | +## π License |
| 123 | + |
| 124 | +MIT |
0 commit comments