I am try to automate a process for changing the document templates of word files.
If the templates are similar structure, ie they both use heading1, then when the document is linked to the new template, it works.
However, the template structure is completely different, heading1 is no longer used, it is now section1. How can I change these section titles with code? Something along the lines of if(heading1) rename to section1;
I am using Interop.Word to perform these operations.
Below is the code I'm using:
public string UpdateDocumentWithNewTemplate(string document, string theme, string template, Word.Application wordApp)
{
try
{
object missing = System.Reflection.Missing.Value;
Word.Document aDoc = null;
object notReadOnly = false;
object isVisible = false;
wordApp.Visible = false;
// create objects from variables for wordApp
object documentObject = document;
// open existing document
aDoc = wordApp.Documents.Open(ref documentObject, ref missing, ref notReadOnly, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible,
ref missing, ref missing, ref missing, ref missing);
aDoc.Activate();
// set template and theme to overwrite the existing styles
aDoc.CopyStylesFromTemplate(template);
aDoc.ApplyDocumentTheme(theme);
aDoc.UpdateStyles();
// save the file with the changes
aDoc.SaveAs(ref documentObject, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
// close the document
aDoc.Close(ref missing, ref missing, ref missing);
if (aDoc != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(aDoc);
aDoc = null;
return documentObject.ToString();
}
catch (Exception exception)
{
return "Error: " + exception;
}
}
For the specific example you need to first import the styles from the other template, then do a Find/Replace to replace the styles applied. I see from your code that you've got the first part (aDoc.CopyStylesFromTemplate(template); aDoc.ApplyDocumentTheme(theme); aDoc.UpdateStyles();).
What many don't realize about Word's Find/Replace functionality is that it can also work with formatting. The best way to get the necessary syntax is to record a successful Find/Replace in a macro, then port the VBA to C#. In the UI:
Ctrl+H to open the Replace dialog box
With the cursor in the "Find what" box, click "More" then "Format" and choose "Style"
Select the name of the style you want to find and have replaced
Click in the "Replace with" box
Use Format/Style, again, to choose the style you want to use
Click "Replace All".
Here's the result I get:
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.styles("Heading 1")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.styles("section2")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.CorrectHangulEndings = False
.HanjaPhoneticHangul = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
You should use Range, not Selection. So the C# code would look something like the following code block. Note how
I get the Range of the entire document
Create a Find object for the Range and use that
To reference Styles for the Find; I show two possibilities
You can list almost all the properties for Find before using Find.Execute. It would also be possible to create object objects for each of these, with only one necessary for true and false then list these "by ref" in Find.Execute. As far as I know, this is simply a matter of personal preference. I did it this way to the most literal "translation" of the VBA to C# code.
In any case, Find.Execute "remembers" these settings, so ref missing can then be used for all the parameters you don't set specifically. In this case, only the "replace all" command is used specifically in the method.
Word.Document doc = wdApp.ActiveDocument;
Word.Range rngFind = doc.Content;
Word.Find fd = rngFind.Find;
fd.ClearFormatting();
Word.Style stylFind = doc.Styles["Heading 1"];
fd.set_Style(stylFind);
fd.Replacement.ClearFormatting();
fd.Replacement.set_Style(doc.Styles["section2"]);
fd.Text = "";
fd.Replacement.Text = "";
fd.Forward = true;
fd.Wrap = Word.WdFindWrap.wdFindStop;
fd.Format = true;
fd.MatchCase = false;
fd.MatchWholeWord = false;
fd.MatchByte = false;
fd.CorrectHangulEndings = false;
fd.HanjaPhoneticHangul = false;
fd.MatchWildcards = false;
fd.MatchSoundsLike = false;
fd.MatchAllWordForms = false;
object replaceAll = Word.WdReplace.wdReplaceAll;
object missing = Type.Missing;
fd.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceAll, ref missing, ref missing, ref missing, ref missing);
Related
I am trying to reverse document paragraphs with the following code:
using Word = Microsoft.Office.Interop.Word;
object filePath = #"input.docx";
Word.Application app = new();
app.Visible = false;
object missing = System.Type.Missing;
object readOnly = false;
object isVisible = false;
Word.Document doc = app.Documents.Open(
ref filePath,
ref missing, ref readOnly, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible, ref missing,
ref missing, ref missing, ref missing);
try
{
Word.Range cachedPara2 = doc.Paragraphs[2].Range.Duplicate;
doc.Paragraphs[2].Range.FormattedText = doc.Paragraphs[1].Range.FormattedText;
doc.Paragraphs[1].Range.FormattedText = cachedPara2.FormattedText;
doc.SaveAs(#"output.docx");
}
finally
{
doc.Close();
app.Quit();
}
I expect this:
but the actual result is this:
How to get expectations?
UPDATE
With the answer below, I was able to get the expected result for my first case.
Now, in another case, I wanna do the following:
Unfortunately, I couldn't quite figure it out how .Collabse() method works. I am trying to do it with .InsertParagraphAfter():
doc.Paragraphs[2].Range.InsertParagraphAfter();
doc.Paragraphs[3].Range.FormattedText = doc.Paragraphs[5].Range.FormattedText;
doc.Paragraphs[5].Range.FormattedText = doc.Paragraphs[2].Range.FormattedText;
doc.Paragraphs[2].Range.Delete();
Where does this empty paragraph come from? How avoid it?
A range object does not have any content itself, it merely points to the location of the content, rather like a set of map co-ordinates.
What you need to do is add the content of the second paragraph before the first, which will create a new first paragraph. You can then delete what is now the third paragraph. For example:
Word.Range target = doc.Paragraphs[1].Range;
target.Collapse wdCollapseStart;
target.FormattedText = doc.Paragraphs[2].Range.FormattedText;
doc.Paragraphs[3].Range.Delete;
I used to highlight the 'word' using this code.It is used inside a 'for each' loop which loops through collection of strings.
But the issue is after the all the words are highlighted .. if we try to change any one word in the document all the highlight removed automatically.
word.Find find = rng.Find;
find.Wrap = word.WdFindWrap.wdFindContinue;
find.Font.UnderlineColor = word.WdColor.wdColorRed;
find.HitHighlight(
FindText: wd,
MatchCase: true,
TextColor:word.WdColor.wdColorRed,
MatchWholeWord: true,
HighlightColor: word.WdColor.wdColorLightYellow
);
By design, HitHighlight only leaves the highlight until the document is edited - this is how the Find task pane works when the user does a non-Advanced Find.
If you want a permanent highlight, then you need to do this a bit differently, by using Replacement.Highlight = true, as in the following example.
Word.Document doc = wdApp.ActiveDocument;
Word.Range rng = doc.Content;
Word.Find f = rng.Find;
object oTrue = true;
object missing = Type.Missing;
//Find and highlight
wdApp.Options.DefaultHighlightColorIndex = Word.WdColorIndex.wdPink;
f.ClearFormatting();
f.Replacement.Highlight = -1;
f.Text = "the";
f.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, Word.WdFindWrap.wdFindStop, ref oTrue, ref missing, Word.WdReplace.wdReplaceAll,
ref missing, ref missing, ref missing, ref missing);
VBA equivalent for interested VBA readers:
Sub FindXAndHighlight()
Dim rng As word.Range
Set rng = ActiveDocument.content
Options.DefaultHighlightColorIndex = wdPink
With rng.Find
.Replacement.Highlight = True
.Execute findText:="the", Replace:=wdReplaceAll
End With
End Sub
I am trying to create a Word document with checkboxes in it using Microsoft.Office.Interop.Word. I have used the following references to do so:
Create a Word document in C#
Create and edit a CheckBox in Word with c#
I have successfully generated a Word document with a checkbox but unfortunately it is disabled. What I am trying to achieve is to have a checkbox that can be checked/unchecked.
In the screenshot below, you can see that I have 3 checkboxes. The 1st one is generated using Microsoft.Office.Interop.Word and the 2nd and 3rd ones were created manually in Word 2016. The first one cannot be marked as checked/unchecked while the 2nd and 3rd ones behave just like a normal checkboxes
And this is the code I used to generate the Word document.
private void btnCreateWordInterop_Click(object sender, EventArgs e)
{
Word._Application word_app = new Word.ApplicationClass();
word_app.Visible = true;
object missing = Type.Missing;
Word._Document word_doc = word_app.Documents.Add(ref missing, ref missing, ref missing, ref missing);
Word.Paragraph para = word_doc.Paragraphs.Add(ref missing);
para.Range.Text = "Chrysanthemum Curve";
object style_name = "Heading 1";
para.Range.set_Style(ref style_name);
para.Range.InsertParagraphAfter();
//Microsoft.Office.Interop.Word.Range range =
para.Range.Collapse(ref missing);
Word.FormField checkBox = word_doc.FormFields.Add(para.Range, Word.WdFieldType.wdFieldFormCheckBox);
para.Range.InsertAfter(" Checkbox generated by Microsoft.Office.Interop.Word");
// Save the document.
object filename = #"C:\Users\Username\Desktop\InteropWord.docx";
word_doc.SaveAs(ref filename, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing);
//Close.
object save_changes = false;
word_doc.Close(ref save_changes, ref missing, ref missing);
word_app.Quit(ref save_changes, ref missing, ref missing);
MessageBox.Show("Saved");
}
How can I make the generated checkbox enabled?
Instead of using FormFields I'd recommend using Content Controls for this. These are more 'User Friendly' and easier to work with in general.
Change this line:
Word.FormField checkBox = word_doc.FormFields.Add(para.Range, Word.WdFieldType.wdFieldFormCheckBox);
Using a Content Control it would be something like (from the top of my head)
Word.ContentControl checkbox = para.Range.ContentControls.Add(Word.WdContentControlType.wdContentControlCheckBox);
I am looking to compare two word documents and I found a solution online that compares word documents using c#. When the code is executed, it opens three applications (the original document, the document that needs to be compared, and a third document that shows the results). Is there a way for these three application to not open up and instead return a boolean to the console?
objective: Display the comparison of the two documents in a console application as either 'the same' or 'different'
using System;
using System.IO;
using Microsoft.Office.Interop.Word;
class Program
{
static void Main()
{
Application wordApp = new Application();
wordApp.Visible = true;
object wordTrue = (object)true;
object wordFalse = (object)false;
object fileToOpen = #"C:\doc1.docx";
object missing = Type.Missing;
Document doc1 = wordApp.Documents.Open(ref fileToOpen,
ref missing, ref wordFalse, ref wordFalse, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref wordTrue, ref missing,
ref missing, ref missing, ref missing);
object fileToOpen1 = #"C:\doc2.docx";
Document doc2 = wordApp.Documents.Open(ref fileToOpen1,
ref missing, ref wordFalse, ref wordFalse, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
Document doc = wordApp.CompareDocuments(doc1, doc2, WdCompareDestination.wdCompareDestinationNew, WdGranularity.wdGranularityWordLevel,
true, true, true, true, true, true, true, true, true, true, "", true);
//Something like the following:
if (doc1 != doc2)
{
Console.WriteLine("They are not same");
}
else
{
Console.WriteLine("They are identical");
}
Console.ReadKey();}}
Have you tried set Visible = false?
I think this is why your word documents pops up.
I did some word docs manipulation in the past, and by setting visible property to false, you can work with the doc without showing it. (winword.exe will execute anyway)
Your code is using interops like Microsoft.Office.Interop.Word, so you can hide the windows using :
wordApp.Visible = false;
and Finally :
wordApp.Quit();
A faster approach is however, reading them in byte. you can compare the two byte arrays to figure out how similar they are.
I would like to use a word file in my visual studio project for edits and return the file.
Microsoft.Office.Interop.Word.Document tempDoc = null;
try
{
object missing = System.Reflection.Missing.Value;
Application wordApp = new Application();
//I have a copy on C: and this works.
object useFileName = "C:\\WordFile.doc";
object readOnly = false;
object isVisible = false;
wordApp.Visible = false;
tempDoc = wordApp.Documents.Open(ref useFileName, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref isVisible, ref missing, ref missing,
ref missing, ref missing);
...
}
return tempDoc;
How do I reference the word document in my project (Content/Documents/WordFile)?
Maybe using Server.MapPath which specifies the relative or virtual path to map to a physical directory.
Server.MapPath("~/Content/Documents/WordFile")
You can get the location on the executing code from
System.Reflection.Assembly.GetExecutingAssembly().Location;
Alternatively place the document location in the config file.