Regex expression to find underlined words in Word document?

Hi,

How do I write the Regex to find string with underscore ?

Eg.

underline

Thank you

The below regular expression will return the word matches “_” -

/\b\w*[_]\w*\b/g
1 Like

Hi @KarthikByggari,

Thanks for your reply!

However, is this regular expression able to return word that are underlined ?

You mean underline. I thought underscore. Sorry for the confusion. I gave regex if word contains underscore not underlined.

Hi @KarthikByggari,

Sorry I mixed up between underline and underscore! :sweat_smile:
Do you know any Regex that able to return word that has been underlined in Words Document ?

Thank you

I am not sure about the Regex, but if it is word, with a simple macro you can find the words which are underlined in a word document.

Please use Invoke VBA activity.
The below is a sample code (may or may not work). Please check online for exact code.

Sample VBA code -

Sub GetUnderLineWords()
    Dim myDoc           As Document: Set myDoc = ActiveDocument ' Change as needed
    Dim aRange          As Range: Set aRange = myDoc.Content ' Change as needed
    Dim sRanges         As Variant: Set sRanges = myDoc.StoryRanges
    Dim sentence        As Object
    Dim w               As Object

    For Each sentence In sRanges
        For Each w In sentence.Words
            If w.Font.Underline <> wdUnderlineNone Then
                w.Font.Underline = wdUnderlineNone
                Debug.Print w.Text
            End If
        Next w
    Next sentence

    'Clean Up
    Set myDoc = Nothing
    Set aRange = Nothing
    Set sRanges = Nothing
    Set w = Nothing
    Set sentence = Nothing
End Sub

Hi @KarthikByggari,

Thanks for your suggestion!
I will try to use Invoke VBA activity and get back to you! :smile:

However, I have one more question. If the string contains word and number, can I still use the Invoke VBA activity?

Thank you

Yes. It works with both.

Regards,
Karthik Byggari

Hi @KarthikByggari,

After I write the VBA code in Excel, i exported the file and the file is in .cls file format

I put the file in Invoke VBA activity inside Excel Application Scope

However it show this error

vba_code

Does this mean I have to enable the access ?

Thank you

In order for this activity to work, Trust Access to the VBA project object model must be enabled from Excel ( File > Options > Trust Center > Trust Center Settings > Macro Settings > Select the Trust Access to the VBA project object model check box).

Hi @KarthikByggari,

Thanks for you help!

It shows this error after I enabled the access

invoke

Hi @KarthikByggari,

Invoke VBA activity is it only can run against excel file ?
As I am using Word Document

Am I able to do the VBA code in Word Document and run in UiPath ?

Thank you

Hi,

UiPath currently doesn’t support running macros directly in word as we did for Excel.

You have to look for alternatives to run the macro in word document.

Please check below thread (it may help you) -

Regards,
Karthik Byggari

Hi @KarthikByggari,

Thank you for your reply! :smile:

I have tried another activity which is Invoke Code
Can you help me check if using that activity is ok ?

I will also try out the given link that you gave

Thank you

Hi @Ying_Zhen_Lee

I have an another idea.
I think you can trigger the code in word from Excel (Execute Macro Activity) using the following code.
Please try.

Sub Sample()
    Dim wdApp As Object, newDoc As Object
    Dim strFile As String

    strFile = "C:\Some\Folder\MyWordDoc.dotm"

    '~~> Establish an Word application object
    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")

    If Err.Number <> 0 Then
        Set wdApp = CreateObject("Word.Application")
    End If
    Err.Clear
    On Error GoTo 0

    wdApp.Visible = True

    Set newDoc = wdApp.Documents.Add(strFile)

    Call wdApp.Run("YHelloThar", "Hello")

End Sub

Regards,
Karthik Byggari

Hi @KarthikByggari,

Thank you for your suggestion! :smile:

However I am not sure of what does the code mean.
Can you explain what does the codes mean?

Thank you

Hi @KarthikByggari,

I have tried using Execute Macro activity.

This is my workflow

However there is an error.

Is it because my Word Document extension is .docx ?

Thank you