Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Word
application.errors
conversions
docmanagement
drawing.graphics
formatting.longdocs
international
internet.assistant
mail
mailmerge.fields
menustoolbars
newusers
numbering
oleinterop
pagelayout
printingfonts
setup.networking
spelling.grammar
tables
vba.addins
vba.beginners
vba.customization
vba.general
vba.userforms
web.authoring
word6-7macros
word97vba
  
 
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

Re: Copying document portions   
Thank you so much Shauna,

    I simply used my source document as the template for the new document in 
the Documents.Add method.  Then I copied over the entire text in the new 
document with the portion of the source, and that seems to have worked like 
a charm.  If this is not an acceptable way to do this, please let me know. 
Otherwise it appears that I am back up and running.  Thank you again for 
your help.

Chris
date: Fri, 25 Jan 2008 11:25:00 -0500   author:   Chris Ellis

Re: Copying document portions   
Hi Chris

Yes, that method should work for your purposes.

As a more general principle, I don't like creating a new document based 
on an old one (and I don't like creating a template based on an old 
document, either). The reason is that documents tend to acquire 'junk' 
over time. The best solution is to base every document on a really nice, 
clean template. Then your documents don't inherit junk. (A place I once 
worked had a template with a problem in it. The template had been used 
to produce hundreds of procedures. Every procedure document had the same 
problem. Ouch.)

But in your case, this sounds like a good solution.

Hope this helps.

Shauna Kelly.  Microsoft MVP.
http://www.shaunakelly.com/word




"Chris Ellis"  wrote in 
message news:%23X3oW72XIHA.5160@TK2MSFTNGP05.phx.gbl...
> Thank you so much Shauna,
>
>    I simply used my source document as the template for the new 
> document in the Documents.Add method.  Then I copied over the entire 
> text in the new document with the portion of the source, and that 
> seems to have worked like a charm.  If this is not an acceptable way 
> to do this, please let me know. Otherwise it appears that I am back up 
> and running.  Thank you again for your help.
>
> Chris
>
date: Sun, 27 Jan 2008 19:23:40 +1100   author:   Shauna Kelly

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us