Asked by:
Find highlighted table captions in Word

Question
-
I have written some code that searches a Word document for highlighted sections. Shown here:
using System; using Microsoft.Office.Interop.Word; using Word = Microsoft.Office.Interop.Word; using System.IO; using System.Text.RegularExpressions; namespace FindTest { class Program { static void parser(string docxFilename) { Word.Document thisDoc; Word.ApplicationClass wordObject = new Word.ApplicationClass(); wordObject.Visible = false; object file = docxFilename; object objTrue = true; object objFalse = false; object missing = System.Type.Missing; // open word document thisDoc = wordObject.Documents.Open(ref file, ref missing, ref objTrue, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref objFalse, ref missing, ref missing, ref missing, ref missing); Range highRange = thisDoc.Range().Duplicate; // highlighted range highRange.Find.Highlight = 1; highRange.Find.Forward = true; highRange.Find.Wrap = WdFindWrap.wdFindStop; object replace = Word.WdReplace.wdReplaceNone; bool result; object str = ""; result = highRange.Find.Execute(ref str, ref missing, ref missing, ref missing, ref missing, ref missing, ref objTrue, ref missing, ref objTrue, ref str, ref replace, ref missing, ref missing, ref missing, ref missing); while (result == true) { Console.WriteLine(highRange.Text.Trim()); result = highRange.Find.Execute(ref str, ref missing, ref missing, ref missing, ref missing, ref missing, ref objTrue, ref missing, ref objTrue, ref str, ref replace, ref missing, ref missing, ref missing, ref missing); } thisDoc.Close(ref objFalse, ref missing, ref missing); wordObject.Quit(ref objFalse, ref missing, ref missing); return; } static void Main(string[] args) { parser(@"C:\Some\Document.docx"); Console.ReadLine(); } } }
This code works fine until it encounters a highlighted table caption. I can't post images or links (but I have uploaded a screenshot of my example document to the forum server and it is file 1122284 if that helps). Basically, there are three tables in the document and the first two have captions. The first table's caption is "Table 1 - Table Caption" and the second table's caption is "Table 2 - Another table caption." but the trailing period is not highlighted.
This is my output I get from my application:
Table 1 - Table Captio n Some normal text Centered Text Table 2 - Another table captio n Not actually a caption
So you can see, a table caption that is highlighted is split into two matches: (1) everything except the last highlighted character and (2) the last highlighted character. Note that the last highlighted character does not have to be the last caption character.
Note also that Word's Advanced Find exhibits the same behavior as the C# code.
I just discovered that when searching backwards rather than forwards, this issue no longer manifests.
Is this expected? Is there a workaround?
- Edited by FamousJameous Monday, September 11, 2017 10:51 PM Added additional information
- Moved by Zhanglong WuMicrosoft contingent staff Tuesday, September 12, 2017 1:55 AM
Monday, September 11, 2017 8:23 PM
All replies
-
Hi FamousJameous,
Thank you for posting here.
According to you question is more related to Word, I will move it to Word for Developers forum for suitable support.
The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions.
Best Regards,
Wendy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Tuesday, September 12, 2017 1:53 AM -
Hi FamousJameous,
I could reproduce your issue both using code and Word's Advanced Find function.
Word object model does not provide anything for this issue.
I would suggest you go to File->Feedback to submit a feedback for this issue.
As a workaround, you could search result backwards and use an array to record the search result.
Then you could get search results in the array from back to front.
Best Regards,
Terry
Wednesday, September 13, 2017 8:39 AM -
Thanks for the response Terry.
What version of Word did you use? I have tried 2010 and 2013 but don't have anything newer. I want to make sure this hasn't been fixed already.
Searching backwards is what I ended up doing for now. I was hoping that was temporary, but it looks like I may have to stick with that.
Thanks again,
James
Wednesday, September 13, 2017 2:23 PM -
The following works fine with VBA:
Sub Demo()
Application.ScreenUpdating = False
Dim StrOut As String
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Format = True
.Forward = True
.Highlight = True
.Wrap = wdFindStop
.MatchWildcards = False
.Execute
End With
Do While .Find.Found
StrOut = StrOut & vbCr & .Text
If .Information(wdWithInTable) = True Then
If .End = .Cells(1).Range.End - 1 Then
.End = .Cells(1).Range.End
.Collapse wdCollapseEnd
If .Information(wdAtEndOfRowMarker) = True Then
.End = .End + 1
End If
End If
End If
If .End = ActiveDocument.Range.End Then Exit Do
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
MsgBox StrOut
End SubNote the additional logic (which you may not need) for handling content in tables and at the end of the document.
Cheers
Paul Edstein
[MS MVP - Word]Wednesday, September 13, 2017 11:20 PM