Skip to main content

Rename WorkSheet in Excel using VB Code || UiPath



We are going to write the full Vb code, so before doing so you have to set these settings,
- Using Invoke Code Activity where you will write this code.
- Use these arguments just like it mentioned in below picture.
- Copy the code below and Paste it into your invoke code activity.


Try
app = New Microsoft.Office.Interop.Excel.ApplicationClass
workbook= app.Workbooks.Open(Filename.Trim)
RenameSuccessful=True
 For Each worksheet In workbook.Sheets
        If worksheet.Name = Sheetname.Trim Then
            sheet_found = True
            Exit For
        Else
            sheet_found = False
        End If
    Next

    If sheet_found = True Then
worksheet = CType(workbook.Worksheets(Sheetname.Trim),Microsoft.Office.Interop.Excel.Worksheet)
worksheet.Name = NewWorkSheetName
workbook.Save
Else
RenameSuccessful=False
ErrorMsg="Sheet " & Sheetname.ToString & " not found in "& System.IO.Path.GetFileName(Filename).ToString &" File"
End If
   
workbook.Close()
     app.Quit()
Catch ex As Exception
RenameSuccessful = False
ErrorMsg = ex.Message
Finally
 gc.Collect()
 gc.WaitForPendingFinalizers()
End Try

Happy Automation!!

Comments

Post a Comment