I need to modify the following code so it executes from a specific line in my word document instead of executing from the beginning.
Dim formula As OMath
For Each formula In ActiveDocument.OMaths
formula.Range.Font.TextColor = RGB(255, 0, 0)
Next
For example, I have a document with 1000 lines. Some of the lines have equations ( OMath
type). I want to run the code to change the color of equations starting from line 600 to end of the document.
Thanks,
You can use Range.Information(wdFirstCharacterLineNumber)
property to get the line number.
More Info Here - This will give you the line number on that current page.
Edit: You may need to use the Absolute Line Number
Code:
Sub getline()
Dim formula As OMath
For Each formula In ActiveDocument.OMaths
With formula.Range
If GetAbsoluteLineNum(formula.Range) > 600 Then .Font.TextColor = RGB(255, 0, 0)
End With
Next
End Sub
Function GetAbsoluteLineNum(r As Range) As Integer
Dim i1 As Integer, i2 As Integer, Count As Integer, rTemp As Range
r.Select
Do
i1 = Selection.Information(wdFirstCharacterLineNumber)
Selection.GoTo what:=wdGoToLine, which:=wdGoToPrevious, Count:=1, Name:=""
Count = Count + 1
i2 = Selection.Information(wdFirstCharacterLineNumber)
Loop Until i1 = i2
r.Select
GetAbsoluteLineNum = Count
End Function
Update: Select the Area in which you want to perform the task and run the below code
Dim formula As OMath
For Each formula In Selection.OMaths
formula.Range.Font.TextColor = RGB(255, 0, 0)
Next
The code below demonstrates how it's possible to start from within a document and perform an from a certain page number to the end of the document.
The InputBox
gets the page number from the user (to understand what the arguments are, look at the Help topic). A Range
object is declared and set to the entire document content, then GoTo
puts the focus of the Range
on the specified page. The End
of the Range
is then extended to the end of the document and the loop runs only on that Range
.
Sub StartFromWithinDoc()
Dim pgNum As Long
Dim formula As OMath
Dim rng As Word.Range
pgNum = InputBox("On what page do you want to start?", "Update Maths", "1")
Set rng = ActiveDocument.content
rng.GoTo What:=wdGoToPage, Count:=pgNum
rng.End = ActiveDocument.content.End
For Each formula In rng.OMaths 'ActiveDocument.OMaths
formula.Range.Font.TextColor = RGB(255, 0, 0)
Next
End Sub
I figured out the solution:
Sub FormatOMaths()
Dim cursorPosition As Long
Dim formula As OMath
cursorPosition = ActiveDocument.Bookmarks("\startOfSel").Start
For Each formula In ActiveDocument.Range(cursorPosition, ActiveDocument.Content.End).OMaths
formula.Range.Font.TextColor = RGB(255, 0, 0)
Next
End Sub
With this code, you click from where you want to execute the code and goes all the way to the end of document.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.