I'm working on Word 2010 plugin and I want to copy active document with track changes (http://office.microsoft.com/en-001/word-help/turn-track-changes-on-or-off-HA010370561.aspx) to XML format and later to send it somewhere else.
This is my code:
Microsoft.Office.Interop.Word.Document documentNew = new Microsoft.Office.Interop.Word.Document();
object missing = Type.Missing;
document.Range(ref missing, ref missing).Copy();
documentNew.Range(ref missing, ref missing).PasteAndFormat(Microsoft.Office.Interop.Word.WdRecoveryType.wdFormatOriginalFormatting);
Object xmlFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXML;
documentNew.SaveAs2(file, xmlFormat);
This works, but does not include track changes in duplicate document. Anybody have idea how to also include changes?
How about saving document to the new document in XML first, then opening up the new document and make whatever changes are required? I've tested this approach and it preserves the tracked changed without having to do anything special.
Copy and pasting into a new document is not going to preserve the original tracked changes.
So the code would be:
public static void SaveAsXMLAndDoSomethingElse() {
String fn = #"C:\Users\zbook\Desktop\Track test.docx";
String fn_xml = #"C:\Users\zbook\Desktop\Track test3.xml";
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Documents docs = app.Documents;
Document doc = docs.Open(fn, ReadOnly:true);
//bool b = doc.TrackFormatting; // for some reason this line bombs
doc.SaveAs2(fn_xml, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXML);
doc.Close(false);
Marshal.ReleaseComObject(doc);
// now open up fn_xml ... and do whatever
app.Quit(false);
Marshal.ReleaseComObject(docs);
Marshal.ReleaseComObject(app);
}
Related
I am currently creating a c# program which uses a template.dotx containing pre-defined building blocks (with bookmarks) to insert pages of content into a new document, all working as follows
Word._Application oWord;
Word._Document oDoc;
object oMissing = System.Reflection.Missing.Value;
object bAddress = "bAddress";
oWord = new Word.Application();
object oTemplate = _template;
oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,
ref oMissing, ref oMissing);
Word.Template objTmpl = (Word.Template)oDoc.get_AttachedTemplate();
Word.BuildingBlock objBB = objTmpl.BuildingBlockEntries.Item("PageBB");
Range where = oDoc.Range(oDoc.Content.End - 1, oDoc.Content.End - 1);
var orng = objBB.Insert(where, true);
orng.Bookmarks[bAddress].Range.Text = "Address";
I would like to do this in reverse, to open created file at a later date, and read through each bookmark to get the value.
The problem is when I open the resulting DocXCreatedFromTemplate.docx, the bookmarks have disappeared. Programmatically, I receive the bookmark not in collection error, but also by allowing word to open and checking manually the bookmarks are replaced with text but the bookmark reference has gone.
Is there any way to get round this?
I have worked this out, the following text replaces and deletes the bookmark
orng.Bookmarks[bAddress].Range.Text = "Address";
The solution was to re-add the bookmark afterwards using a method as follows
private void UpdateBookmarkWithoutDeleting(object bookmark, string text, _Document document)
{
var bookmarkRange = document.Bookmarks[bookmark].Range;
bookmarkRange.Text = text;
document.Bookmarks.Add(bookmark.ToString(), bookmarkRange);
}
I found the information at the following link (in VB):
https://wordmvp.com/FAQs/MacrosVBA/InsertingTextAtBookmark.htm
I need to produce and save a MS Word .doc file starting from a template (let's say C:\template.doc) and a datasource (let'say C:\datasource.doc)
I'm using MailMerge.Execute and if I let Word to be visible, I see the correct result file, but I can't realize to save this file in any way. The code is:
Microsoft.Office.Interop.Word.Application myWord = new Microsoft.Office.Interop.Word.Application();
Object oFalse = false; Object oTrue = true; Object oFileName = #"C:\merged.doc";
Word.Document myMailMergeDoc = myWord.Documents.Open(#"C:\template.doc");
myMailMergeDoc.MailMerge.OpenDataSource(Name: #"C:\datasource.doc");
myMailMergeDoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
myMailMergeDoc.MailMerge.Execute(oFalse);
try
{
myWord.Documents["template.doc"].Close(oFalse);
myWord.Documents.Save(oFileName);
myMailMergeDoc.SaveAs(oFileName);
myWord.ActiveDocument.SaveAs(oFileName);
}
catch(System.Runtime.InteropServices.COMException ex)
{ }
None of the three saving methods in the try block saves the file on the disk. What am I doing wrong?
Ok, I did it: I missed to install VBA features from Office setup, now the document is correctly saved
I want to copy the content of a document file to another keeping it formatted. I found a way to copy the whole document to a new one using File.Copy(file, newFile, true); but I need to append the content to another existing one.
Here is a quick code sample that should get you on your way:
using Word = Microsoft.Office.Interop.Word;
var wdApp = new Word.Application();
var documents = wdApp.Documents;
var doc = documents.Open(newFile);
doc.Content.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
doc.Content.InsertFile(file);
I am new to C#. I am attempting to simply use Microsoft.Office.Interop.Word
All is working except that the Merge Fields in the footer are not updating. I continue to get the mergecode text only («pname»).
Here is the important part of my code
private void getDoc()
{
String cdir = Directory.GetCurrentDirectory();
btnGetPoa.Visible = false;
var application = new Word.Application();
Object dir = #"../../templates/";
Directory.SetCurrentDirectory(dir.ToString());
var doc = new Word.Document();
var dirf = Directory.GetCurrentDirectory() + "\\poas.docx";
doc = application.Documents.Add(Template: dirf);
foreach (Word.Field fld in doc.Fields)
{
if(fld.Code.Text.Contains("pname"))
{
fld.Select();
application.Selection.TypeText(txtpName.Text.ToString());
}
}
object what = Word.WdGoToItem.wdGoToPage;
object which = Word.WdGoToDirection.wdGoToFirst;
object count = 3;
object missing = Missing.Value;
application.Selection.GoTo(ref what, ref which, ref count, ref missing);
application.Visible = true;
}
Any ideas why the Fields in the Footer are not merging?
Thank you.
The Document object addresses only the main document "story". So querying fields in that object doesn't "see" any fields in headers, footers, Shapes, etc. You need to specifically address these other objects. For a general method on how to address all StoryRange objects, look at the VBA Language Reference example for StoryRange and related. You'll also find discussions and code on this site.
For your specific request, I'm assuming that the document has only the one section and the standard footer - no first page footer or even page footer. (A Word document can have multiple sections and different types of headers and footers, but a "plain vanilla" document has only one of each.)
Word.Range rngFooter = doc.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
foreach (Word.Field fld in rngFooter.Fields)
{
}
I'm looking to replace a bookmark in a word document with the entire contents of another word document. I was hoping to do something along the lines of the following, but appending the xml does not seem to be enough as it does not include pictures.
using Word = Microsoft.Office.Interop.Word;
...
Word.Application wordApp = new Word.Application();
Word.Document doc = wordApp.Documents.Add(filename);
var bookmark = doc.Bookmarks.OfType<Bookmark>().First();
var doc2 = wordApp.Documents.Add(filename2);
bookmark.Range.InsertXML(doc2.Contents.XML);
The second document contains a few images and a few tables of text.
Update: Progress made by using XML, but still doesn't satisfy adding pictures as well.
You've jumped in deep.
If you're using the object model (bookmark.Range) and trying to insert a picture you can use the clipboard or bookmark.Range.InlineShapes.AddPicture(...). If you're trying to insert a whole document you can copy/paste the second document:
Object objUnit = Word.WdUnits.wdStory;
wordApp.Selection.EndKey(ref objUnit, ref oMissing);
wordApp.ActiveWindow.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);
If you're using XML there may be other problems, such as formatting, images, headers/footers not coming in correctly.
Depending on the task it may be better to use DocumentBuilder and OpenXML SDK. If you're writing a Word addin you can use the object API, it will likely perform the same, if you're processing documents without Word go with OpenXML SDK and DocumentBuilder. The issue with DocumentBuilder is if it doesn't work there aren't many work-arounds to try. It's open source not the cleanest piece of code if you try troubleshooting it.
You can do this with openxml SDK and Document builder. To outline here is what you will need
1> Inject insert key in main doc
public WmlDocument GetProcessedTemplate(string templatePath, string insertKey)
{
WmlDocument templateDoc = new WmlDocument(templatePath);
using (MemoryStream mem = new MemoryStream())
{
mem.Write(templateDoc.DocumentByteArray, 0, templateDoc.DocumentByteArray.Length);
using (WordprocessingDocument doc = WordprocessingDocument.Open([source], true))
{
XDocument xDoc = doc.MainDocumentPart.GetXDocument();
XElement bookMarkPara = [get bookmarkPara to replace];
bookMarkPara.ReplaceWith(new XElement(PtOpenXml.Insert, new XAttribute("Id", insertKey)));
doc.MainDocumentPart.PutXDocument();
}
templateDoc.DocumentByteArray = mem.ToArray();
}
return templateDoc;
}
2> Use document builder to merge
List<Source> documentSources = new List<Source>();
var insertKey = "INSERT_HERE_1";
var processedTemplate = GetProcessedTemplate([docPath], insertKey);
documentSources.Add(new Source(processedTemplate, true));
documentSources.Add(new Source(new WmlDocument([docToInsertFilePath]), insertKey));
DocumentBuilder.BuildDocument(documentSources, [outputFilePath]);