Re: border macro

105 views
Skip to first unread message

Govert van Drimmelen

unread,
Feb 15, 2013, 7:48:59 AM2/15/13
to Excel-DNA
Hi Rex,

When using the C# 4 (or VB.NET0 dynamic COM support, the constants
defined in the type library are not available. So you either have to
replace it with the actual values, or define your own type with the
constant values.

If you use a tool like ILSpy (http://ilspy.net/) you can copy the
values from an interop assembly, so you don't have to retype
everything.

If using .NET 4, you can reference the PIA assemblies, but mark then
as "Embed Interop Types" so you need no redistribute them.

An alternative is to reference an interop assembly in your code, and
use that instead of the dynamic support. If you do a lot of COM
interop and need to target various Excel versions, you might look at
the NetOffice (http://netoffice.codeplex.com) interop assemblies. The
are fairly compatible with the real Primary Interop Assemblies, but
cover all the Excel versions, with Intellisense to show you what
features are available under what Excel versions.


I can't say which of these approaches is the most common - they all
have a place depending on what you are doing. If it were me, I'd
probably decide like this:

* If you are doing only a bit of COM interop, stick with 'dynamic' and
define constants of your own, maybe copy out of the interop assembly
using ILSpy.
* If you do more, and can target Excel 2007+, or 2010+, and can stick
to common functionality across versions, reference the Excel 2010
interop and pick 'Embed Interop Types'.
* If you need to target both old Excel and new Excel, and need to vary
your code to fit the Excel version, use the NetOffice assemblies and
pack into your .xll to keep the distribution easier.

-Govert


On Feb 15, 12:24 am, Rex Reed <rexnfx...@gmail.com> wrote:
> I'm trying to figure out how to set my borders in a macro. This is what the
> vba would look like
>
> With ActiveSheet.Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(15, 15))
>
> With .Borders(xlEdgeTop)
>     .LineStyle = xlContinuous
>     .ColorIndex = 0
>     .TintAndShade = 0
>     .Weight = xlThin
> End With
> With .Borders(xlEdgeBottom)
>     .LineStyle = xlContinuous
>     .ColorIndex = 0
>     .TintAndShade = 0
>     .Weight = xlThin
> End With
> With .Borders(xlEdgeRight)
>     .LineStyle = xlContinuous
>     .ColorIndex = 0
>     .TintAndShade = 0
>     .Weight = xlThin
> End With
> With .Borders(xlEdgeLeft)
>     .LineStyle = xlContinuous
>     .ColorIndex = 0
>     .TintAndShade = 0
>     .Weight = xlThin
> End With
> With .Borders(xlInsideHorizontal)
>     .LineStyle = xlContinuous
>     .ColorIndex = 0
>     .TintAndShade = 0
>     .Weight = xlThin
> End With
> With .Borders(xlInsideVertical)
>     .LineStyle = xlContinuous
>     .ColorIndex = 0
>     .TintAndShade = 0
>     .Weight = xlThin
> End With
>
> End With
>
> I'm trying to do this with C#:
>
> dynamic range = app.ActiveSheet.Range(app.ActiveSheet.Cells(1,
> 1),app.ActiveSheet.Cells(results.Count + 2, 13));
> range.Borders(xlEdgeTop).LineStyle = xlContinuous;
> range.Borders(xlEdgeTop).ColorIndex = 0;
> range.Borders(xlEdgeTop).TintAndShade = 0;
> range.Borders(xlEdgeTop).Weight = xlThin;
> .
> .
> .
>
> But this C# won't compile with all of the excel enumeration references. Is
> it common practice to track down the integers associated with these
> enumeration values? I'd like to have them in here for readability purposes.
> I guess I could define my own enumeration... Just trying to learn the most
> common approach. Is it possible or am I going about this in the wrong
> manner?
Reply all
Reply to author
Forward
0 new messages