Word Document.Content.Paste results in "Server threw an exception"

N

Nabeel Moeen

Hi,

Following is the code i'm using to copy contents of one word document to
another:

object missingValue = Type.Missing;
Document newDocument = _app.Documents.Add(ref missingValue, ref
missingValue, ref missingValue, ref falseObject);
_document.Content.Copy();

if (newDocument == null)
throw new ApplicationException("Could not create Word document.");

newDocument.Content.Paste();


I'm using C#, .NET 1.1 and tried it on both Office XP and OFcice 2003.
the problem is, it works fine on my machine, but on all the other machines
the call to "newDocument.Content.Paste()" throws the "Server threw an
exception" error
Err.Code 80010105
:|
I can't figure out why its working on my machine and none of the other
machine ...or the cause of the error.
Any suggestions on what the problem might be?

(I've tried Microsoft PIA for Office XP too)
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?TmFiZWVsIE1vZWVu?=,

..Paste can do weird things, sometimes. You might try something like this, no
guarantees, though:

word.Range rng = _document.Content;
//You want wdCollapseStart here, but I haven't
//memorized the C# syntax
rng.Collapse 1;
rng.Paste();

You might also try a variation on your code that stops just before the line
in question, with the Word window visible and accessible. See if you can
manually perform the paste operation. If you can't, Word might give you a
message telling you why.
Following is the code i'm using to copy contents of one word document to
another:

object missingValue = Type.Missing;
Document newDocument = _app.Documents.Add(ref missingValue, ref
missingValue, ref missingValue, ref falseObject);
_document.Content.Copy();

if (newDocument == null)
throw new ApplicationException("Could not create Word document.");

newDocument.Content.Paste();


I'm using C#, .NET 1.1 and tried it on both Office XP and OFcice 2003.
the problem is, it works fine on my machine, but on all the other machines
the call to "newDocument.Content.Paste()" throws the "Server threw an
exception" error
Err.Code 80010105
:|
I can't figure out why its working on my machine and none of the other
machine ...or the cause of the error.
Any suggestions on what the problem might be?

(I've tried Microsoft PIA for Office XP too)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
N

Nabeel Moeen

I have a hunch its a problem with the "newDocument" object being returned by
the Documents.Add method.
How can i verify that the newDocument is a valid Document object? is there a
property that i could try printing on the console or something similar?

and any ideas why Documents.Add might be causing the problem if it is?
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?TmFiZWVsIE1vZWVu?=,
I have a hunch its a problem with the "newDocument" object being returned by
the Documents.Add method.
How can i verify that the newDocument is a valid Document object? is there a
property that i could try printing on the console or something similar?

and any ideas why Documents.Add might be causing the problem if it is?
The one thing that comes to mind is that the document might not be fully
initialised when you try sending the paste. You might try putting a Do-Loop in
your code right after the .Add that checks whether the document count prior to
the Add has been incremented by one, and don't leave the loop until it has.

If that still gives you ambiguous results, you could also try something like
this
Word.Range rng = newDocument.Content;
rng.Text = "abc";
rng.Text = "";
rng.Paste();

Possibly, "activating" the range in the document could make a difference.

If you still get the same problems... Have you checked whether you have any
trouble doing the paste manually? There is third-party software out there that
interferes with the clipboard in Office applications. Try starting both Windows
and Word in Safe Mode and see if things behave any differently.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
N

Nabeel Moeen

thanx for the responses Cindy,

the problem seemed to be the 4th parameter passed to the Documents.Add
method, i.e. "false".

I was able to resolve the problem by passing Type.Missing as the fourth
parameter.

Regards,
Nabeel Moeen
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?TmFiZWVsIE1vZWVu?=,
the problem seemed to be the 4th parameter passed to the Documents.Add
method, i.e. "false".

I was able to resolve the problem by passing Type.Missing as the fourth
parameter.
Interesting. Yes, I guess it would make sense that Word would want the
document to be visible before pasting, as it relies very much on being
able to lay out the result on-screen.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
E

Evan Stone

Interesting. Yes, I guess it would make sense that Word would want the
document to be visible before pasting, as it relies very much on being
able to lay out the result on-screen.

Word needs the document to be *visible* for a successful paste?

you've got to be kidding me.

great.


evan k. stone | software engineer
 
E

Evan Stone

Hi Cindy,

I'm a little confused here... In the following example, wouldn't it actually
be pasting into the source document?
word.Range rng = _document.Content;
//You want wdCollapseStart here, but I haven't
//memorized the C# syntax
rng.Collapse 1;
rng.Paste();

In which case, is it actually newDocument that you want to create the range
from (instead of _document), since that's where the data's being pasted to,
or are you suggesting to collapse the source document before doing the
Copy()?

I'm just wondering if the Collapse should happen with the source or
destination Range object.

thx.

evan k. stone | software engineer
 
C

Cindy M -WordMVP-

Hi Evan,
I'm a little confused here... In the following example, wouldn't it actually
be pasting into the source document?
Yes said:
In which case, is it actually newDocument that you want to create the range
from (instead of _document), since that's where the data's being pasted to,
or are you suggesting to collapse the source document before doing the
Copy()?
Yes, there should be two ranges (unless the purpose were to copy the content
and insert a duplicate at the front of the original). One source range, and
one target range (the new document).

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top