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
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
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
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”
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
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?