Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
project is done in C# and relates to modifying existing Word document.
How I can get text between two existing bookmarks, then iterate text line by line and change font size for each line. The goal is to have in the end;
text in first line to be for example font size 14, bolded;
text in second line, no bold, text size 12;
third and other lines, no bold, text size 10;


Best regards,

What I have tried:

I tried accessing text between two bookmarks and every explanation online mention using Bookmarks.get_Item, but get_Item method does not exist in Visual Studio 2019 in C#.
Posted
Updated 17-Jan-23 15:38pm
v2
Comments
Graeme_Grant 14-Jan-23 0:59am    
post your code

I have not worked with C# and Word, so I would need to use Google Search: c# word bookmark[^].

There are a number of promising solutions found, including this one: Using C# to programmatically find, and populate bookmarks in a Word 2010 Add-In [solved] - StackOverflow[^]
 
Share this answer
 
Well...
a bookmark is added into specific range in a document. So, you can get the range of bookmark1, then get the range of bookmark2 and finally you can select range between them.

Imagine, i've got below document content:
Bookmark1


Some specific text between 2 bookmarks.





Text between 2 bookmarks ends here.

Bookmark2


See VBA code:
VBA
Option Explicit

Sub Test()
    Dim oDoc As Document
    Dim oRng1 As Range, oRng2 As Range, oBtwRng As Range
    
    Set oDoc = ThisDocument
    Set oRng1 = oDoc.Bookmarks("Bookmark1").Range
    Set oRng2 = oDoc.Bookmarks("Bookmark2").Range

    Set oBtwRng = oDoc.Range(oRng1.End, oRng2.Start)
    oBtwRng.Select
    
    Debug.Print oRng1.Text, oRng2.Text, Selection.Text
    
    Set oRng1 = Nothing
    Set oRng2 = Nothing
    Set oDoc = Nothing
    
End Sub


Above code produces:
Some specific text between 2 bookmarks.





Text between 2 bookmarks ends here.
 
Share this answer
 
Hello,
thanks for help, but it does not work for me, unless I did not convert it correctly from VBA to C#:
Here is my converted piece of code:
Word.Range oRng1 = wordDocument.Bookmarks["start_from_here"].Range;
Word.Range oRng2 = wordDocument.Bookmarks["end_here"].Range;

Word.Range btwRange = wordDocument.Range(oRng1.End, oRng2.Start);

MessageBox.Show("Range between is " + btwRange.Text);


It flags as an error first line
Word.Range oRng1 = wordDocument.Bookmarks["start_from_here"].Range;
and it says "requested member of collection does not exist. And bookmark does exist as I can see it in document.
Anyway, thanks for trying.
First offered solution is using get_Item methos, but I simply do not have it in Visual Studio 2019.
Regards,
 
Share this answer
 
Comments
Ralf Meier 18-Jan-23 5:52am    
how did you have adressed the Document itself ?
Richard Deeming 19-Jan-23 6:35am    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution and post a comment.

Do not post your comment as another "solution" to your question.
MihaljKeri 19-Jan-23 22:59pm    
wordApplication = new Word.Application();
wordDocument = wordApplication.Documents.Open(@"C:\DocumentName.docx");
wordDocument.Activate();
Regards,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900