Excel Header and Footer Font from c#

  • Thread starter Thread starter John hoffman
  • Start date Start date
J

John hoffman

Hello,

I am trying to set the MS Excel header and footer font
using automation from C#.

I can set the text using:

oPageSetup.LeftHeader = "Header Text";

but how do you set the Font size????


TIA,

John Hoffman
 
I don't know how to do that, but I have a way to discover how excel does
things:
start excel, start recording a macro, look what the code does and then use
that code in your automation project. Simple but it works often.

Steven.
 
Hi John,

Thank you for posting in MSDN managed newsgroup!

You can join the font type with the header size. I list sample code below:
'Code begin
ActiveSheet.PageSetup.LeftHeader = "&""Arial Unicode MS,Regular""Header Text"
'Code end

You can append the head text or foot text after "&""font type name""<header or foorter text>".

Furthermore, you can also try the method Steven suggested which will be very fast for you to create the macro code.

Please feel free to let me know if you have any further questions.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello Wei-Dong,

The sample code you sent:
ActiveSheet.PageSetup.LeftHeader = "&""Arial Unicode
MS,Regular""Header Text"

below is NOT valid C# syntax. (I believe it is the VB
macro code.)

I have tried
ActiveSheet.PageSetup.LeftHeader = "&" + "Arial Unicode
MS,Regular" + "Header Text";

but that just gives me the literal text in the header.

Any help would be appreciated.

TIA,

John Hoffman

-----Original Message-----
Hi John,

Thank you for posting in MSDN managed newsgroup!

You can join the font type with the header size. I list sample code below:
'Code begin
ActiveSheet.PageSetup.LeftHeader = "&""Arial Unicode MS,Regular""Header Text"
'Code end

You can append the head text or foot text after "&""font
type name" said:
Furthermore, you can also try the method Steven
suggested which will be very fast for you to create the
macro code.
 
Hi John,

Thank you for replying!

In C#, the string ["&""Arial Unicode MS,Regular""Header Text"] will be thought invalid by the complier for so many quotation in this string. We can
use StringBuilder class to read this string and stringbuild class will transfer the string into one valid format which C# compiler prefers automatically.
I write the sample codes for you.

//Code begin
//the stringbuilder class is in the System.Text namespace
using System.Text;
...
try
{
Excel.Worksheet objWS;
StringBuilder str = new StringBuilder();

// the ["&""Arial Unicode MS,Regular""Header Text"] will be transferred into
// ["&\"Arial Unicode MS,Regular\"Header Text"] by stringBuilder internally
// which is valid in C#
str.Append(@"&""Arial Unicode MS,Regular""Header Text");

objWS = (Excel.Worksheet)this.ThisApplication.ActiveSheet;
objWS.PageSetup.LeftFooter = str.ToString();
}
catch(System.Exception ex)
{
MessageBox.Show(ex.Message);
}
//Code end

Furthermore, in your project, I'd suggest you can also use stringbuilder to create your footer and header string very easily with the Append
method.

In addition, in Visual Basic.Net and VBA, we can directly pass the ["&""Arial Unicode MS,Regular""Header Text"] string for the footer and header.

Please feel free to let me know if you have any further questions.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top