Get index of underlined text in Word Paragraph (Microsoft.Office.Interop.Word)

UPDATE 2:
I got it figured out. I was trying to get the underline value but was using find which will not return the value but whether or not the find method was successful. I’ve changed it so that I’m actually getting the value of the underline which doesn’t need the find method:

pRange = rParagraphs(10).Range
cRange = pRange.Characters
if cRange[15].Underline == 1:
	test4 = "Underlined"
else:
	test4 = "Not Underlined"
test5 = cRange[15].Text

UPDATE 1:
I managed to get each character with formatting but when I try to get the underline status for each character it is always returning 9999999 even if it is ran in a paragraph with no underlines at all.

pRange = rParagraphs(12).Range
cRange = pRange.Characters
test4 = cRange[14].Find.Font.Underline
test5 = cRange[14].Text	

ORINIGAL POST:
I need to know what parts of paragraphs in a word document need to be underlined. I’m not super familiar with Microsoft.Office.Interop.Word. The closest I’ve been able to come is determining if the paragraph as no text underlined(0), all text underlined(1), or some text underlined(9999999). This obviously doesn’t help me if there is underlined text in the middle of a paragraph.

Here is what I have right now. Sorry for the messy code, I’m just testing a bunch of stuff in the word module before I actually try putting anything together.

# Load the Python Standard and DesignScript Libraries
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import System

clr.AddReference("Microsoft.Office.Interop.Word")
import Microsoft.Office.Interop.Word as Word
import re

# The inputs to this node will be stored as a list in the IN variables.
wordDoc = IN[0]


missing = System.Type.Missing


word_application = Word.ApplicationClass()
word_application.visible = False

document = word_application.Documents.Open(wordDoc, missing, True)

# Place your code below this line

wdoc = document.Range()
#test = Word.Characters(0)
# Assign your output to the OUT variable.

rText = wdoc.Text

rParagraphs = wdoc.Paragraphs

pCount = rParagraphs.Count

pContent = []

test4 = ""

######Working with lists Below

#pFormat = rParagraphs(1).Range.ListFormat.ListLevelNumber
#pFormat = rParagraphs(1).Range.ListFormat.ListString
#pFormat = rParagraphs(1).Range.ListFormat.ListType

######Getting Underline Below
# at least 1 underline = 9999999. This will be the result for letter lists.
# all underline = 1. Entire paragraph needs to be underlined.
# No underline = 0. Entire paragraphne

pFormat = rParagraphs(6).Range.Underline

######Get Paragraph text and check if it's a MANUAL letter list

#pFormat = rParagraphs(1).Range.Text
#if re.match("^.[.]\t",pFormat):
#	test4 = "Letter list"
#else:
#	test4 = "NOT a letter list"



######Getting paragraph text
#pContent.append(rParagraphs(1).Range.Text)

#for i in range(pCount-1):
	#pContent.append(rParagraphs(i+1).Range.Text)

word_application.Quit()
word_application = None


OUT = rParagraphs, rText, pContent, pFormat, test4

Below is some stuff I’ve tried from a bunch of searches I’ve done online. Note: a lot of this code is incomplete and causes errors.

#####Find Underline in paragraph
pFormat = rParagraphs(10).Range.Select()

pFormat.Find.Font.Underline
 

test4 = pFormat.Range.text
#pFormat = rParagraphs(10)
#formatSearch = 

#pFormat.Find.Execute("", , , , , , , , , , , , , , )


#pFormat = rParagraphs(10).Range
#pFormat.Find.Font.Underline = Word.WdUnderline.wdUnderlineSingle
#test4 = pFormat.Find.Font.Underline = Word.WdUnderline.wdUnderlineSingle
#pFormat.Find.Execute("")
#pFormat.Find.Font.Underline
1 Like