Invoke code throwing error

Hi @SHYAM_KUMAR_SK

Invoke code is throwing the following error attached. Can someone tell me whats wrong.

Dim inputObj As Object = CType(inputPath, Object)
Dim outputObj As Object = CType(outputPath, Object)

Dim wordApp As New Microsoft.Office.Interop.Word.Application
wordApp.Visible = False

Dim doc As Microsoft.Office.Interop.Word.Document = wordApp.Documents.Open(FileName:=inputObj, ReadOnly:=True)
Dim newDoc As Microsoft.Office.Interop.Word.Document = wordApp.Documents.Add()

For Each para As Microsoft.Office.Interop.Word.Paragraph In doc.Paragraphs
Dim paraText As String = para.Range.Text.Trim()
If paraText.ToLower().Contains(“[extract]”) Then
Dim newPara As Microsoft.Office.Interop.Word.Paragraph = newDoc.Content.Paragraphs.Add()
newPara.Range.FormattedText = para.Range.FormattedText
newPara.Range.InsertParagraphAfter()
End If
Next

newDoc.SaveAs2(FileName:=outputObj)
newDoc.Close()
doc.Close()
wordApp.Quit()

Try this

Imports Microsoft.Office.Interop.Word
Imports System.Runtime.InteropServices

Module Module1
Sub ExtractParagraphsWithTag(inputPath As String, outputPath As String)
Dim wordApp As Application = Nothing
Dim doc As Document = Nothing
Dim newDoc As Document = Nothing

    Try
        wordApp = New Application()
        wordApp.Visible = False

        doc = wordApp.Documents.Open(FileName:=inputPath, ReadOnly:=True)
        newDoc = wordApp.Documents.Add()

        For Each para As Paragraph In doc.Paragraphs
            Dim paraText As String = para.Range.Text.Trim()
            If paraText.ToLower().Contains("[extract]") Then
                Dim newPara As Paragraph = newDoc.Content.Paragraphs.Add()
                newPara.Range.FormattedText = para.Range.FormattedText
                newPara.Range.InsertParagraphAfter()
            End If
        Next

        newDoc.SaveAs2(FileName:=outputPath)
    Catch ex As Exception
        Console.WriteLine("Error: " & ex.Message)
    Finally
        ' Close documents if opened
        If newDoc IsNot Nothing Then
            newDoc.Close(False)
            Marshal.ReleaseComObject(newDoc)
            newDoc = Nothing
        End If

        If doc IsNot Nothing Then
            doc.Close(False)
            Marshal.ReleaseComObject(doc)
            doc = Nothing
        End If

        ' Quit Word application
        If wordApp IsNot Nothing Then
            wordApp.Quit()
            Marshal.ReleaseComObject(wordApp)
            wordApp = Nothing
        End If

        ' Force garbage collection of COM objects
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Try
End Sub

Sub Main()
    Dim inputFilePath As String = "C:\path\to\your\input.docx"
    Dim outputFilePath As String = "C:\path\to\your\output.docx"

    ExtractParagraphsWithTag(inputFilePath, outputFilePath)
    Console.WriteLine("Extraction completed.")
End Sub

End Module

@Krithi1,

Add try catch in your code to get the exact issue in the code like this.

Try
Dim inputObj As Object = CType(inputPath, Object)
Dim outputObj As Object = CType(outputPath, Object)

Dim wordApp As New Microsoft.Office.Interop.Word.Application
wordApp.Visible = False

Dim doc As Microsoft.Office.Interop.Word.Document = wordApp.Documents.Open(FileName:=inputObj, ReadOnly:=True)
Dim newDoc As Microsoft.Office.Interop.Word.Document = wordApp.Documents.Add()

For Each para As Microsoft.Office.Interop.Word.Paragraph In doc.Paragraphs
Dim paraText As String = para.Range.Text.Trim()
If paraText.ToLower().Contains(“[extract]”) Then
Dim newPara As Microsoft.Office.Interop.Word.Paragraph = newDoc.Content.Paragraphs.Add()
newPara.Range.FormattedText = para.Range.FormattedText
newPara.Range.InsertParagraphAfter()
End If
Next

newDoc.SaveAs2(FileName:=outputObj)
newDoc.Close()
doc.Close()
wordApp.Quit()
Catch ex as Exception
    Console.WriteLine("Error:"+ex.Message)
End Try

This code will give you log in Output panel.

@ashokkarale

I already posted the screenshot in my description. Tahts exactly what i am getting even after giving Try catch

I sent you a private message. Its throwing errors. @SHYAM_KUMAR_SK

@SHYAM_KUMAR_SK

I am seeing the following errors.


I tested your code and did not get any error.
Are your input/output paths correct?
Cheers

The code from @SHYAM_KUMAR_SK can’t be indeed used in InvoceCode like this.
Module/Sub can’t be used in InvoceCode.
In InvokeCode you could use the code between
“Sub ExtractParagraphsWithTag” and “End Sub”

Cheers

@J0ska

Yes my input ans output file paths are correct. What else could be wrong?any suggestions?

Frankly, no idea. I just copy/pasted your code and it executed without error.
I am using Studio 24.10.11, System.Activities 24.10.7

You can add some debug output using Console.WriteLine() to see at what exact step your code fails.

Can you post here sample file you are processing?

Cheers

@J0ska

I did put Console.Writeline right before the Try.

When i ran even that first console.wroreline value didnt print, which tells me that as soon as it get to “invoke code” activity its failing. Its not even going into the code

1 Like

@J0ska

I upgraded the package and the code is now working. Thank you so much for helping on this.

Great! For my curiosity: Was it the System.Activities package? And what was the version causing the problem?

Cheers

@J0ska
Yes, I upgraded to the latest version.

@Krithi1
what was the version you upgraded FROM?

Cheers

@J0ska

I have one more thing that I need to do here.

The current code is only fetching the bullet points that has “EXTRACT” word in the bullet points. But now, I need to grab the Headers of those bullet points I am extracting. Each HEADER start with 6.1, 6.2,6.0.1 etc. followed by word “SPRINT” For Eg: 6.5 SPRINT 0001 Customer Support, 6.7 SPRINT FMO Support, 6.8.1 SPRINT Strategic Communications, 6.8.2 SPRINT Compliance Development, 6.10 SPRINT Integration Support etc

Under these HEADERS, the actual bullet points with “EXTRACT” keyword for which I already have the code that I posted above.

How can I add code to that? I think what I have to do is that, I need to split using “SPRINT” somewhere and then need to loop throw and write to the word. Can you please help me with that?

Maybe better create new thread for this.
Provide sample input and expected output.

Cheers