I have a piece of text tagged with ##ABC, so it looks like this:
Some text ##ABCtext to be found##ABC some text
I need to find and remove the ##ABCtext to be found##ABC with interop. So far, I've come up wit the following code, which, however, seems to do nothing:
Microsoft.Office.Interop.Word.Range rng = document.Range();
rng.Find.ClearFormatting();
rng.Find.Replacement.ClearFormatting();
rng.Find.MatchWildcards = true;
rng.Find.Text = "##ABC(.*?)##ABC";
rng.Find.Replacement.Text = "";
rng.Find.Forward = true;
rng.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindStop;
rng.Find.Execute(Replace: Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll);
What am I missing?
Related
I am trying to put data into cells using closedxml and according to their site we have to set parameters like this on the cells {{TestName}} but it won't work for me.
This is my code and I am not sure where I am going wrong.
const string outputFile = #"\\Reports\Output\Test.xlsx";
var template = new XLTemplate(#"\\Reports\Test.xlsx");
var test = new models.Test();
test.id = 1;
test.TestName = "ASDF";
test.TestScore = "303";
template.AddVariable(test) //also tried doing it like this.
template.AddVariable("TestScore", "232");
template.AddVariable("TestName", "TESTNAME");
template.SaveAs(outputFile);
Process.Start(new ProcessStartInfo(outputFile) { UseShellExecute = true });
And my excel template looks like this.
Solved it!
Just add template.Generate(); after adding the variables into the template.
The following code finds instances of the word "Family" in a Word document. It selects and deletes the instances. The code works fine, but I want to find all instances of only highlighted words.
public void FindHighlightedText()
{
const string filePath = "D:\\COM16_Duke Energy.doc";
var word = new Microsoft.Office.Interop.Word.Application {Visible = true};
var doc = word.Documents.Open(filePath);
var range = doc.Range();
range.Find.ClearFormatting();
range.Find.Text = "Family";
while (range.Find.Execute())
{
range.Select();
range.Delete();
}
doc.Close();
word.Quit(true, Type.Missing, Type.Missing);
}
Set the Find.Highlight property to true.
Interop uses the same objects and methods that are available to VBA macros. You can find the actions, properties you need to perform a task by recording a macro with those steps and inspecting it.
Often, but not always, the properties match the UI. If something is a property in the general Find box, it's probably a property in the Find interface as well.
For example, searching only for highlighted words produced this macro :
Selection.Find.ClearFormatting
Selection.Find.Highlight = True
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Which can be translated to :
range.Find.ClearFormatting();
range.Find.Highlight=1;
...
while(range.Find.Execute())
{
...
}
I want to replace the text content of bookmarks without loosing the bookmark.
foreach(Bookmark b in document.Bookmarks)
{
b.Range.Text = "newtext"; // text is set in document but bookmark is gone
}
I tried to set the new Range of the bookmark before the Text setting but I still have the same problem.
I also tried to re-add the bookmark with document.Bookmarks.Add(name, range); but I can't create an instance of range.
I had to readd the bookmarks and save the range temporarily. I also had to add a list of processed items to evade an endless loop.
List<string> bookmarksProcessed = new List<string>();
foreach (Bookmark b in document.Bookmarks)
{
if (!bookmarksProcessed.Contains(b.Name))
{
string text = getTextFromBookmarkName(b.Name);
var newend = b.Range.Start + text.Length;
var name = b.Name;
Range rng = b.Range;
b.Range.Text = text;
rng.End = newend;
document.Bookmarks.Add(name, rng);
bookmarksProcessed.Add(name);
}
}
Looks like you solved your problem, but here is a cleaner way to do it:
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;
using System.Text.RegularExpressions;
using Word = Microsoft.Office.Interop.Word;
//declare and get the current document
Document extendedDocument = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
List<string> bookmarksProcessed = new List<string>();
foreach(Word.Bookmark oldBookmark in extendedDocument.Bookmarks)
{
if(bookmarksProcessed.Contains(oldBookmark.Name))
{
string newText = getTextFromBookmarkName(oldBookmark.Name)
Word.Range newRange = oldBookmark.Range;
newRange.End = newText.Length;
Word.Bookmark newBookmark = extendedDocument.Controls.AddBookmark(newRange, oldBookmark.Name);
newBookmark.Text = newText;
oldBookmark.Delete();
}
}
Code isn't tested but should work.
With the above approach, you still lose the bookmark before it is added back in. If you really need to preserve the bookmark, I find that you can create an inner bookmark that wraps around the text (a bookmark within bookmark). After having the inner bookmark, you simply need to do:
innerBookmark.Range.Text = newText;
After the text replacing, the inner bookmark is gone and the outer bookmark is preserved. No need to set range.End.
You can create the inner bookmark manually or programmatically depending on your situation.
I have the following code being used with Word 2016 installed, referencing Microsoft Word 16.0 Object Library:
private void RefreshFootnoteNumbering(FileManagement.FileManager FileManager)
{
Console.WriteLine(DateTime.Now.ToString() + " Refreshing footnotes DOCX");
// Opening and saving in word generates the required element
var Word = GetWordApp();
try
{
var DocxPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.ChangeExtension(FileManager.HtmlFileLocation, "docx"));
Console.WriteLine(DateTime.Now.ToString() + "\tOpening document");
var Doc = GetWordDoc(Word, DocxPath);
try
{
// Fails on these lines below (both cause the same exception)
Doc.Footnotes.NumberingRule = InteropWord.WdNumberingRule.wdRestartPage;
Doc.Footnotes.Location = InteropWord.WdFootnoteLocation.wdBottomOfPage;
Doc.SaveAs2(DocxPath, InteropWord.WdSaveFormat.wdFormatXMLDocument, AddToRecentFiles: false, EmbedTrueTypeFonts: true);
}
finally
{
Doc.Close();
Doc = null;
}
}
finally
{
Word.Quit();
Word = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
This works for most documents, however for some I get the following exception:
System.Runtime.InteropServices.COMException was unhandled
ErrorCode=-2146823680
HResult=-2146823680
HelpLink=wdmain11.chm#37376
Message=Value out of range
Source=Microsoft Word
StackTrace:
at Microsoft.Office.Interop.Word.Footnotes.set_NumberingRule(WdNumberingRule prop)
Other interop functions (iterating/manipulating fields, sections etc) work fine, it seems to be just altering footnotes in this way that have an issue. Altering them from within Word itself works fine.
Has anyone encountered this issue before? Any work arounds or alternatives?
I've tried recording a macro, and it gave this VBA code:
With ActiveDocument.Range(Start:=ActiveDocument.Content.Start, End:= _
ActiveDocument.Content.End).FootnoteOptions
.Location = wdBottomOfPage
.NumberingRule = wdRestartContinuous
.StartingNumber = 1
.NumberStyle = wdNoteNumberStyleArabic
.NumberingRule = wdRestartPage
.LayoutColumns = 0
End With
If I run this macro, I get the same error (value out of range, error number 4608) on the .Location line, whether I run from the debugger, or just view macros -> run.
I've also tried to translate that VBA into C# code:
var Options = Doc.Range(Doc.Content.Start, Doc.Content.End).FootnoteOptions;
Options.Location = InteropWord.WdFootnoteLocation.wdBottomOfPage;
Options.NumberingRule = InteropWord.WdNumberingRule.wdRestartPage;
However, this gives the same error.
Still not sure of the exact cause (possibly something further up in my code creating different sections); still not clear on why it worked when word recorded the macro, but not when running it.
Anyway, I managed to alter the C# code to the below, which seems to do the job and actually works!
foreach(InteropWord.Footnote FootNote in Doc.Footnotes)
{
FootNote.Reference.FootnoteOptions.NumberingRule = InteropWord.WdNumberingRule.wdRestartPage;
FootNote.Reference.FootnoteOptions.Location = InteropWord.WdFootnoteLocation.wdBottomOfPage;
}
Using this code:
var avgOrderAmountHeaderCell = (Range)_xlSheetDelPerf.Cells[DEL_PERF_COLUMN_HEADER_ROW, AVG_ORDER_AMOUNT_COLUMN];
avgOrderAmountHeaderCell.Value2 = String.Format("Avg Order{0}Amount", Environment.NewLine);
avgOrderAmountHeaderCell.WrapText = true;
var avgPackageCountHeaderCell = (Range)_xlSheetDelPerf.Cells[DEL_PERF_COLUMN_HEADER_ROW, AVG_PACKAGE_AMOUNT_COLUMN];
avgPackageCountHeaderCell.Value2 = String.Format("Avg Package{0}Count", Environment.NewLine);
avgPackageCountHeaderCell.WrapText = true;
...I get this result (column header on left looks fine, while the one on the right has an "empty line" between the second and third wrapped line):
Why is this seemingly arbitrary inconsistent implementation of text wrapping taking place, and more importantly, how can I force no "blank lines" to appear between one line of text and another?