MS Word Document Creation

  • Thread starter Thread starter John Smith
  • Start date Start date
J

John Smith

Hey folks,

I'm creating an MS Word document in a C# Windows application. Because the
client base has so many different versions of Word they are using, I opted
to go with late binding to create the document.

**Everything works great, but I can't figure out how to do 2 things:
1) Double Spacing
2) Insert a Page Break

Anyone got any sample code or know of a good page on the web that shows how
to do this?



(BTW...Posted similar message last week without a response. Sorry for the
double post)
 
John,

In order to insert a page break, you have to get a range of text where
you want to insert the page break, and then make the call:

// range is the range of text.
range.InsertBreak(wdPageBreak);

I'm not sure which enumeration wdPageBreak is from (or if it is defined
as a static variable on a type), but that's the value you need.

As for the double spacing, you want to take the range of text, and do
this:

// Double space the range of text:
range.ParagraphFormat.LineSpacingRule = wdLineSpaceDouble;

Once again, you will have to find the type that wdLineSpaceDouble is
associated with.

Hope this helps.
 
Thanks a lot Nicholas! Do you know how to do it for late-binding though?
I'm having a hard time finding any examples of this. I'm not sure what
string to pass to InvokeMember to get the Range. And then once I have the
range, I'm not sure what string to pass to InvokeMember to tell it to format
for double spacing or page breaks.

To give you an idea of how I'm doing it, here is how I create the document,
make it visible, set the font size, set it to bold, and write out some text.
It's different than standard MS Word Automation and neccessary because it
needs to work with Office 97 and up:

Type WordType = Type.GetTypeFromProgID("Word.Application");
Object WordApp = Activator.CreateInstance(WordType);
WordType.InvokeMember("Visible", BindingFlags.SetProperty, null, WordApp,
new object[]{true});
Object WordDoc = WordApp.GetType().InvokeMember( "Documents",
BindingFlags.GetProperty, null, WordApp, null );

WordDoc = WordDoc.GetType().InvokeMember( "Add", BindingFlags.InvokeMethod,
null, WordDoc, null );

Object WordSelection = WordApp.GetType().InvokeMember( "Selection",
BindingFlags.GetProperty, null, WordApp, null );

Object WordFont = WordSelection.GetType().InvokeMember( "Font",
BindingFlags.GetProperty, null, WordSelection, null );
WordType.InvokeMember("Size", BindingFlags.SetProperty, null, WordFont, new
object[]{10});
WordType.InvokeMember("Bold", BindingFlags.SetProperty, null, WordFont, new
object[]{true});

WordType.InvokeMember("TypeText", BindingFlags.InvokeMethod, null,
WordSelection, new object[]{"MY WONDERFUL TEXT"});




Nicholas Paldino said:
John,

In order to insert a page break, you have to get a range of text where
you want to insert the page break, and then make the call:

// range is the range of text.
range.InsertBreak(wdPageBreak);

I'm not sure which enumeration wdPageBreak is from (or if it is defined
as a static variable on a type), but that's the value you need.

As for the double spacing, you want to take the range of text, and do
this:

// Double space the range of text:
range.ParagraphFormat.LineSpacingRule = wdLineSpaceDouble;

Once again, you will have to find the type that wdLineSpaceDouble is
associated with.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


John Smith said:
Hey folks,

I'm creating an MS Word document in a C# Windows application. Because the
client base has so many different versions of Word they are using, I opted
to go with late binding to create the document.

**Everything works great, but I can't figure out how to do 2 things:
1) Double Spacing
2) Insert a Page Break

Anyone got any sample code or know of a good page on the web that shows
how
to do this?



(BTW...Posted similar message last week without a response. Sorry for the
double post)
 
John,

Woah, that is a lot of late bound calls. Also, you are going to have a
lot of references hanging around until the first GC occurs (and a lot of
instances of Word hanging around as a result).

I would suggest using VB.NET for your late bound calls. You can declare
your type as object, and then type the calls as you would normally. Since
it is a late bound call, you won't get intellisense, but it's better than
having to do all of these calls.

Also, to release the references, you have to remember that for every
object that is returned by every property, you should also pass it to the
static ReleaseComObject method on the Marshal class.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

John Smith said:
Thanks a lot Nicholas! Do you know how to do it for late-binding though?
I'm having a hard time finding any examples of this. I'm not sure what
string to pass to InvokeMember to get the Range. And then once I have the
range, I'm not sure what string to pass to InvokeMember to tell it to
format
for double spacing or page breaks.

To give you an idea of how I'm doing it, here is how I create the
document,
make it visible, set the font size, set it to bold, and write out some
text.
It's different than standard MS Word Automation and neccessary because it
needs to work with Office 97 and up:

Type WordType = Type.GetTypeFromProgID("Word.Application");
Object WordApp = Activator.CreateInstance(WordType);
WordType.InvokeMember("Visible", BindingFlags.SetProperty, null, WordApp,
new object[]{true});
Object WordDoc = WordApp.GetType().InvokeMember( "Documents",
BindingFlags.GetProperty, null, WordApp, null );

WordDoc = WordDoc.GetType().InvokeMember( "Add",
BindingFlags.InvokeMethod,
null, WordDoc, null );

Object WordSelection = WordApp.GetType().InvokeMember( "Selection",
BindingFlags.GetProperty, null, WordApp, null );

Object WordFont = WordSelection.GetType().InvokeMember( "Font",
BindingFlags.GetProperty, null, WordSelection, null );
WordType.InvokeMember("Size", BindingFlags.SetProperty, null, WordFont,
new
object[]{10});
WordType.InvokeMember("Bold", BindingFlags.SetProperty, null, WordFont,
new
object[]{true});

WordType.InvokeMember("TypeText", BindingFlags.InvokeMethod, null,
WordSelection, new object[]{"MY WONDERFUL TEXT"});




in
message news:#[email protected]...
John,

In order to insert a page break, you have to get a range of text
where
you want to insert the page break, and then make the call:

// range is the range of text.
range.InsertBreak(wdPageBreak);

I'm not sure which enumeration wdPageBreak is from (or if it is defined
as a static variable on a type), but that's the value you need.

As for the double spacing, you want to take the range of text, and do
this:

// Double space the range of text:
range.ParagraphFormat.LineSpacingRule = wdLineSpaceDouble;

Once again, you will have to find the type that wdLineSpaceDouble is
associated with.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


John Smith said:
Hey folks,

I'm creating an MS Word document in a C# Windows application. Because the
client base has so many different versions of Word they are using, I opted
to go with late binding to create the document.

**Everything works great, but I can't figure out how to do 2 things:
1) Double Spacing
2) Insert a Page Break

Anyone got any sample code or know of a good page on the web that shows
how
to do this?



(BTW...Posted similar message last week without a response. Sorry for the
double post)
 
Is there a better way? There's not much documentation out there on late
bounding Word, but all the examples I could find do it like this.

Can't use VB.Net though because it's part of requirements that it must all
be built in C#.

I'll make sure to take care of the GC.

Just wish MS had some sort of an MSDN article on this as my problem must be
a fairly common one. It's a client with 20 employees on Office 97, 10 on
Office 2000, 5 on Office XP...thus requiring the late bounding.





Nicholas Paldino said:
John,

Woah, that is a lot of late bound calls. Also, you are going to have a
lot of references hanging around until the first GC occurs (and a lot of
instances of Word hanging around as a result).

I would suggest using VB.NET for your late bound calls. You can declare
your type as object, and then type the calls as you would normally. Since
it is a late bound call, you won't get intellisense, but it's better than
having to do all of these calls.

Also, to release the references, you have to remember that for every
object that is returned by every property, you should also pass it to the
static ReleaseComObject method on the Marshal class.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

John Smith said:
Thanks a lot Nicholas! Do you know how to do it for late-binding though?
I'm having a hard time finding any examples of this. I'm not sure what
string to pass to InvokeMember to get the Range. And then once I have the
range, I'm not sure what string to pass to InvokeMember to tell it to
format
for double spacing or page breaks.

To give you an idea of how I'm doing it, here is how I create the
document,
make it visible, set the font size, set it to bold, and write out some
text.
It's different than standard MS Word Automation and neccessary because it
needs to work with Office 97 and up:

Type WordType = Type.GetTypeFromProgID("Word.Application");
Object WordApp = Activator.CreateInstance(WordType);
WordType.InvokeMember("Visible", BindingFlags.SetProperty, null, WordApp,
new object[]{true});
Object WordDoc = WordApp.GetType().InvokeMember( "Documents",
BindingFlags.GetProperty, null, WordApp, null );

WordDoc = WordDoc.GetType().InvokeMember( "Add",
BindingFlags.InvokeMethod,
null, WordDoc, null );

Object WordSelection = WordApp.GetType().InvokeMember( "Selection",
BindingFlags.GetProperty, null, WordApp, null );

Object WordFont = WordSelection.GetType().InvokeMember( "Font",
BindingFlags.GetProperty, null, WordSelection, null );
WordType.InvokeMember("Size", BindingFlags.SetProperty, null, WordFont,
new
object[]{10});
WordType.InvokeMember("Bold", BindingFlags.SetProperty, null, WordFont,
new
object[]{true});

WordType.InvokeMember("TypeText", BindingFlags.InvokeMethod, null,
WordSelection, new object[]{"MY WONDERFUL TEXT"});




in
message news:#[email protected]...
John,

In order to insert a page break, you have to get a range of text
where
you want to insert the page break, and then make the call:

// range is the range of text.
range.InsertBreak(wdPageBreak);

I'm not sure which enumeration wdPageBreak is from (or if it is defined
as a static variable on a type), but that's the value you need.

As for the double spacing, you want to take the range of text, and do
this:

// Double space the range of text:
range.ParagraphFormat.LineSpacingRule = wdLineSpaceDouble;

Once again, you will have to find the type that wdLineSpaceDouble is
associated with.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hey folks,

I'm creating an MS Word document in a C# Windows application.
Because
the
client base has so many different versions of Word they are using, I opted
to go with late binding to create the document.

**Everything works great, but I can't figure out how to do 2 things:
1) Double Spacing
2) Insert a Page Break

Anyone got any sample code or know of a good page on the web that shows
how
to do this?



(BTW...Posted similar message last week without a response. Sorry
for
the
double post)
 
John,

Personally, I would argue to relax that requirement. I'm not saying
that all of the code should be in VB.NET, but for something of this nature,
it makes it MUCH easier to maintain, instead of having all of those late
bound calls to do it. You can place the routines that handle the automation
of word in that file, and only for that. Considering the nature of .NET,
that kind of restriction is foolish, and limiting (and I'd be willing to
speak to whomever came up with that requirement).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

John Smith said:
Is there a better way? There's not much documentation out there on late
bounding Word, but all the examples I could find do it like this.

Can't use VB.Net though because it's part of requirements that it must all
be built in C#.

I'll make sure to take care of the GC.

Just wish MS had some sort of an MSDN article on this as my problem must
be
a fairly common one. It's a client with 20 employees on Office 97, 10 on
Office 2000, 5 on Office XP...thus requiring the late bounding.





in
message news:[email protected]...
John,

Woah, that is a lot of late bound calls. Also, you are going to have a
lot of references hanging around until the first GC occurs (and a lot of
instances of Word hanging around as a result).

I would suggest using VB.NET for your late bound calls. You can declare
your type as object, and then type the calls as you would normally.
Since
it is a late bound call, you won't get intellisense, but it's better than
having to do all of these calls.

Also, to release the references, you have to remember that for every
object that is returned by every property, you should also pass it to the
static ReleaseComObject method on the Marshal class.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

John Smith said:
Thanks a lot Nicholas! Do you know how to do it for late-binding though?
I'm having a hard time finding any examples of this. I'm not sure what
string to pass to InvokeMember to get the Range. And then once I have the
range, I'm not sure what string to pass to InvokeMember to tell it to
format
for double spacing or page breaks.

To give you an idea of how I'm doing it, here is how I create the
document,
make it visible, set the font size, set it to bold, and write out some
text.
It's different than standard MS Word Automation and neccessary because it
needs to work with Office 97 and up:

Type WordType = Type.GetTypeFromProgID("Word.Application");
Object WordApp = Activator.CreateInstance(WordType);
WordType.InvokeMember("Visible", BindingFlags.SetProperty, null, WordApp,
new object[]{true});
Object WordDoc = WordApp.GetType().InvokeMember( "Documents",
BindingFlags.GetProperty, null, WordApp, null );

WordDoc = WordDoc.GetType().InvokeMember( "Add",
BindingFlags.InvokeMethod,
null, WordDoc, null );

Object WordSelection = WordApp.GetType().InvokeMember( "Selection",
BindingFlags.GetProperty, null, WordApp, null );

Object WordFont = WordSelection.GetType().InvokeMember( "Font",
BindingFlags.GetProperty, null, WordSelection, null );
WordType.InvokeMember("Size", BindingFlags.SetProperty, null, WordFont,
new
object[]{10});
WordType.InvokeMember("Bold", BindingFlags.SetProperty, null, WordFont,
new
object[]{true});

WordType.InvokeMember("TypeText", BindingFlags.InvokeMethod, null,
WordSelection, new object[]{"MY WONDERFUL TEXT"});




"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
in
message John,

In order to insert a page break, you have to get a range of text
where
you want to insert the page break, and then make the call:

// range is the range of text.
range.InsertBreak(wdPageBreak);

I'm not sure which enumeration wdPageBreak is from (or if it is
defined
as a static variable on a type), but that's the value you need.

As for the double spacing, you want to take the range of text, and do
this:

// Double space the range of text:
range.ParagraphFormat.LineSpacingRule = wdLineSpaceDouble;

Once again, you will have to find the type that wdLineSpaceDouble is
associated with.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hey folks,

I'm creating an MS Word document in a C# Windows application. Because
the
client base has so many different versions of Word they are using, I
opted
to go with late binding to create the document.

**Everything works great, but I can't figure out how to do 2 things:
1) Double Spacing
2) Insert a Page Break

Anyone got any sample code or know of a good page on the web that shows
how
to do this?



(BTW...Posted similar message last week without a response. Sorry for
the
double post)
 
Back
Top