|
|
|
date: Wed, 23 Jan 2008 16:54:16 -0500,
group: microsoft.public.word.vba.beginners
back
Copying document portions
Hi,
I'm writing an add-in for Word 2007 in C#. I saw an add-ins newsgroup,
but I'm a beginner at this, and I think my question more pertains to the
object model for Word, etc... I am trying to copy a section of a document to
a new document, formatting and all. This is the code I currently have:
public static Document[] SplitDocsOnSectionBreaks( Document doc )
{
Application app = Globals.ThisAddIn.Application;
Sections sections = doc.Sections;
object missing = Type.Missing;
Document[] ret = new Document[ sections.Count ];
int x = 0;
// walk through the separate sections of the document copying the entire
contents of
// the section into a new document
foreach( Section curSec in sections )
{
Document newDoc = app.Documents.Add( ref missing, ref missing, ref
missing, ref missing );
object zero = (int)0;
newDoc.Range( ref zero, ref zero ).FormattedText =
curSec.Range.FormattedText;
ret[ x++ ] = newDoc;
}
return ret;
}
On some documents this works perfectly, but on other documents the
formatting is incorrect in the new documents (wrong font and font size). Am
I going about this the wrong way? I could use the clipboard I suppose, but
I would prefer not to if it can be avoided.
Thank you in advance for your help,
Chris
date: Wed, 23 Jan 2008 16:54:16 -0500
author: Chris Ellis
Re: Copying document portions
Hi Chris
For an explanation of what is happening here, see
Why does text change format when I copy it into another document?
http://www.ShaunaKelly.com/word/styles/FormatOfTextChanges.html
I would suggest creating copies of your file and deleting the bits you
don't want, rather than trying to copy bits of text into a new document.
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
"Chris Ellis" wrote in
message news:%23H4$%23pgXIHA.4440@TK2MSFTNGP06.phx.gbl...
> Hi,
>
> I'm writing an add-in for Word 2007 in C#. I saw an add-ins
> newsgroup, but I'm a beginner at this, and I think my question more
> pertains to the object model for Word, etc... I am trying to copy a
> section of a document to a new document, formatting and all. This is
> the code I currently have:
>
> public static Document[] SplitDocsOnSectionBreaks( Document doc )
> {
> Application app = Globals.ThisAddIn.Application;
> Sections sections = doc.Sections;
> object missing = Type.Missing;
> Document[] ret = new Document[ sections.Count ];
> int x = 0;
>
> // walk through the separate sections of the document copying the
> entire contents of
> // the section into a new document
> foreach( Section curSec in sections )
> {
> Document newDoc = app.Documents.Add( ref missing, ref missing, ref
> missing, ref missing );
> object zero = (int)0;
>
> newDoc.Range( ref zero, ref zero ).FormattedText =
> curSec.Range.FormattedText;
> ret[ x++ ] = newDoc;
> }
>
> return ret;
> }
>
> On some documents this works perfectly, but on other documents the
> formatting is incorrect in the new documents (wrong font and font
> size). Am I going about this the wrong way? I could use the
> clipboard I suppose, but I would prefer not to if it can be avoided.
>
> Thank you in advance for your help,
> Chris
>
date: Fri, 25 Jan 2008 08:58:22 +1100
author: Shauna Kelly
|
|