Join
Sign in
Search Options
Search Everything
Search Development Tools
Home
Blog
Resources
Forums
About Open XML
More ...
Home
»
Forums
»
Development Tools
»
.Net
»
Copy portions of a Word doc to another, new, Word doc
Re: Copy portions of a Word doc to another, new, Word doc
Development Tools
Discussions about working with Open XML using a wide range of development tools
Get this RSS feed
Details
1
Reply
0
Subscribers
Posted
over 2 years ago
Options
Subscribe via RSS
Share this
.Net
Copy portions of a Word doc to another, new, Word doc
rated by 0 users
This post has
1 Reply |
0
Followers
bruccutler
20
Posted by
bruccutler
on
Wed, Mar 3 2010 5:28 PM
rated by 0 users
Post Points: 20
Copy portions of a Word doc to another, new, Word doc
Hello,
I am very new to OpenXML (1 day).
I am trying to copy portions of one Word document to another Word document.
I'm using the following code snippet to do so. It seems to be getting some, but not all, of the data.
Am I on the right track?
Thanks for your help.
- Bruce
private bool CopyRequirements(string sourceFile)
{
// Search for each requirement by looking for the ID:Requirement tag
string sourceFolder = System.IO.Path.GetDirectoryName(sourceFile);
Body sourceBody = mainSourcePart.Document.Body;
Body mainTargetBody = null;
OpenXmlElement element = sourceBody.FirstChild;
WordprocessingDocument myTargetDoc = null;
MainDocumentPart mainTargetPart = null;
bool fileOpen = false;
string fullRequirementName;
do
{
switch (element.GetType().Name)
{
case "Paragraph":
if (element.InnerText == "ID: Name")
{
// found the header
element = element.NextSibling();
fullRequirementName = element.InnerText;
if (myTargetDoc != null)
{
mainTargetPart.Document.Save();
myTargetDoc.Close();
myTargetDoc = null;
}
// create a new document
int pos = fullRequirementName.LastIndexOf(":");
string requirementID = fullRequirementName.Substring(0, pos);
string newPath = sourceFolder + "\\\\" + requirementID + ".docx";
myTargetDoc = WordprocessingDocument.Create(newPath, WordprocessingDocumentType.Document);
// Add a new main document part.
mainTargetPart = myTargetDoc.AddMainDocumentPart();
//Create Document tree for simple document.
mainTargetPart.Document = new Document();
//Create Body (this element contains other elements that we want to include
mainTargetBody = new Body();
//Create paragraph
Paragraph paragraph = new Paragraph();
Run run_paragraph = new Run();
// we want to put that text into the output document
Text text_paragraph = new Text(fullRequirementName);
//Append elements appropriately.
run_paragraph.Append(text_paragraph);
paragraph.Append(run_paragraph);
mainTargetBody.Append(paragraph);
mainTargetPart.Document.Append(mainTargetBody);
fileOpen = true;
}
else if (fileOpen)
{
mainTargetBody.Append((Paragraph)element.Clone());
}
break;
case "Table":
if (fileOpen)
{
mainTargetBody.Append((Table)element.Clone());
}
break;
default: // I need to figure which other types there are and how to add them
MessageBox.Show("Unknown type: " + element.GetType().Name, "Test");
break;
}
}while ((element = element.NextSibling()) != null);
if (myTargetDoc != null)
{
mainTargetPart.Document.Save();
myTargetDoc.Close();
myTargetDoc = null;
}
return true;
}
Intergen
2610
Posted by
Intergen
on
Thu, Apr 15 2010 6:47 AM
rated by 0 users
Post Points: 5
Re: Copy portions of a Word doc to another, new, Word doc
Hi there
You might want to check whether the variable that holds the copied data has enough capacity to hold all the data. If data type you chose can't hold all the copied data, some of the data will be trucked.
Hope it helps.
Regards,
Page 1 of 1 (2 items)