473,324 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

MS Word Document Creation

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)
Nov 16 '05 #1
5 6673
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]
- mv*@spam.guard.caspershouse.com
"John Smith" <js@no.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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)

Nov 16 '05 #2
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]" <mv*@spam.guard.caspershouse.com> wrote in
message news:#h**************@TK2MSFTNGP15.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com
"John Smith" <js@no.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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)


Nov 16 '05 #3
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]
- mv*@spam.guard.caspershouse.com

"John Smith" <js@no.com> wrote in message
news:eL**************@tk2msftngp13.phx.gbl...
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]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:#h**************@TK2MSFTNGP15.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com
"John Smith" <js@no.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
> 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)
>
>



Nov 16 '05 #4
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:O3*************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"John Smith" <js@no.com> wrote in message
news:eL**************@tk2msftngp13.phx.gbl...
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]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:#h**************@TK2MSFTNGP15.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com
"John Smith" <js@no.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
> 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)
>
>



Nov 16 '05 #5
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]
- mv*@spam.guard.caspershouse.com

"John Smith" <js@no.com> wrote in message
news:eA**************@TK2MSFTNGP09.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:O3*************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"John Smith" <js@no.com> wrote in message
news:eL**************@tk2msftngp13.phx.gbl...
> 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]" <mv*@spam.guard.caspershouse.com>
> wrote
> in
> message news:#h**************@TK2MSFTNGP15.phx.gbl...
>> 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]
>> - mv*@spam.guard.caspershouse.com
>>
>>
>> "John Smith" <js@no.com> wrote in message
>> news:%2****************@tk2msftngp13.phx.gbl...
>> > 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)
>> >
>> >
>>
>>
>
>



Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: josepe | last post by:
Hi, I want to do a dinamicly generation of a word doc in my ASP page. I do it with the next code: <% Response.ContentType = "application/msword" Response.AddHeader "content-disposition",...
1
by: Devhead | last post by:
how do i always have word app on top after i open it. i tries Document.Activate() but i think what causing the problem is that i have a message dialog confirmation the document creation and that is...
0
by: CognitiveFlux | last post by:
Hi All, I'm pretty new to .Net, not to mention XML, so I truly appreciate any advice! I have user data that I want to selectively insert into a MS Word document in XML. Given the various...
0
by: Niyazi | last post by:
Hi, I created application that store the data in SQL SERVER that reside on network. The client also use this application to access the resources provided with application. But is the client want...
2
by: jmar | last post by:
I am updating a VB4.0 quote generation program to VB.net. The old program takes user inputs, performs calculations, saves the data to Access databases and uses Crystal Reports 5.0 to generate a...
10
by: mc | last post by:
I've know that Microsoft don't currently recommend this process, what I'm trying to find out is why? This is a feature that I've used (with Classic ASP) in the past with a great deal of success....
0
by: hula | last post by:
Hello i have some general questions on document creation/editing. First of what i need to implement: the customer that can be modified from him. He must be able to underline, change fonts,...
0
by: PracticalApps | last post by:
I looked to find a canned solution to create a Word document in my application and just couldn't find anything that just gets to the point. I would think, and I may be making too strong of an...
3
by: Mufasa | last post by:
I need to write a windows program in C# that will display a word document on the screen without having Word loaded on the machine. How can I do this? TIA - J.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.