Invoke VB.Net Code

Hello Guys,

I’m trying to invoke this vb.net code to delete the blank pages of a word document. but when I’m trying to execute it by using invoke code it gives me some errors.

but when I execute in the visual studio it works perfectly.

this is my code

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim App As Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application()

   Dim oDoc As Document = App.Documents.Open("E:\REbirth\Zimply\Pdf\Wiki Doc\Internal RPA Project- version -1.1.docx")

   Try
       For Each paragraph As Paragraph In oDoc.Paragraphs
           If paragraph.Range.Text.Trim() = String.Empty Then
               paragraph.Range.Select()
               App.Selection.Delete()
           End If
       Next
       oDoc.Save()
       oDoc.Close()

   Catch ex As ArgumentException

   End Try

End Sub

these are the errors -

Kindly requesting your help on this

Hi,

First: fix your code like so:

    Dim App As Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application()
    Dim oDoc As Microsoft.Office.Interop.Word.Document = App.Documents.Open("your word file")

    For Each paragraph As Microsoft.Office.Interop.Word.Paragraph In oDoc.Paragraphs
            If paragraph.Range.Text.Trim() = String.Empty Then
                paragraph.Range.Select()
                App.Selection.Delete()
            End If
    Next

    oDoc.Save()
    oDoc.Close()

Secondly: remove the Word Application Scope. Not needed.

Thirdly: add an assign activity before the invoke code - bug - otherwise it won’t compile.

Fourthly: add Word reference in your projects main.xaml file in the section where other refs are, like:

<AssemblyReference>Microsoft.Office.Interop.Word</AssemblyReference>

4 Likes

Hey @PaulNel Thanks a lot for the help. But i don’t understand Third ( what to assign for assign activity ?) and Fourth steps.

Would you be able to attach a sample file.

Thanks again :slight_smile:

Attaching the project.

The assign activity is just to work around a bug in uipath. Read about it on the forum somewhere. You can assign whatever, an assign activity must just be in the project.

Search for “Microsoft.Office.Interop.Word” in main.xaml. You’ll see where the reference was added,

Main.zip (2.0 KB)

1 Like

Hi Paul,

I am “baby” using invoke VB… Could you support me with this Function? I need check Excel files with VBA… How to invoke this function ?

Private Function WbkHasVBA(ByVal wb As Workbook) As Boolean
'returns true if workbook contains VBA, else false.
'Code must not be locked.
'Set reference to Microsoft VBA Extensibility 5.5

Dim VBComp As VBIDE.VBComponent
Dim VBMod As VBIDE.CodeModule
Dim nLines As Long, sMod As String
     
'get each module one at a time
For Each VBComp In wb.VBProject.VBComponents
    Set VBMod = VBComp.CodeModule
    nLines = VBMod.CountOfLines
        If nLines <> 0 Then
            sMod = VBMod.Lines(1, nLines)
            'check for significant code entries
            If ContainsVBAKeyWords(sMod) Then
               WbkHasVBA = True
               Exit For
            End If
        End If
Next VBComp

Set VBComp = Nothing
Set VBMod = Nothing

End Function