Invoke code - working with Word document

Hi!

My Studio: 21.10.4
System.Activities: 22.10.4

First of all, I wanted to change properties of Word document (Title and Author). I was trying with Invoke code activity:

image

But in invoke code object Word.Document doesn’t have .BuiltInDocumentProperties property

I imported namespaces: Microsoft.Office.Interop.Word and Microsoft.Office.Core

When I check options available for my doc object it shows:

image

I was able to do it with VBScript, which looks like that:

dim FilePath, Title

FilePath = (WScript.Arguments(0))
Title = (WScript.Arguments(1))

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open(FilePath)
objDoc.BuiltInDocumentProperties("Title").Value = Title
objDoc.BuiltInDocumentProperties("Author").Value = "XYZ"
objDoc.Save
objWord.Quit

Set objDoc = Nothing
Set objWord = Nothing

System.Runtime.Interopservices.Marshal.ReleaseComObject(objDoc)
System.Runtime.Interopservices.Marshal.ReleaseComObject(objWord)

And this code works. So my questions:

  1. Why I can’t do it with invoke code?
  2. Do you see anything that I could improve with my code?

Second thing is function to read all the text in Word document. UiPath do not offer this option with their activities. My goal is to read entire text (including footers and headers). I don’t mind about order.

So this time I worked with Invoke code (it worked):

textFromWordDocument = String.Empty
wordDocument = wordApp.Documents.Open(filePath) ' open word
textFromWordDocument = wordDocument.Content.Text ' read text

'read headers and footers, add it to text

For Each sekcja As Microsoft.Office.Interop.Word.Section  In wordDocument.Sections
	For Each naglowek As HeaderFooter In sekcja.Headers
		textFromWordDocument = textFromWordDocument + naglowek.Range.Text
		Next
	For Each stopka As HeaderFooter In sekcja.Footers
		textFromWordDocument = textFromWordDocument + stopka.Range.Text
		Next
Next

'save and close
wordDocument.Close()
wordApp.Quit()

'release from memory
Marshal.ReleaseComObject(wordDocument)
Marshal.ReleaseComObject(wordApp)

Arguments:

Question:

  1. Do you see anything that I could improve with read text code?