Excel Automation - Check comment present in cell, if there then append text with previous text

kiss4344

Member
Hi - Appending comment in excel cell works fine with below code with the help of @Sachin_Kharmale , but my new requirement is to check whether comment already there in cell.

If it's already there I need to append new text with existing comment, else I need to add new comment in that cell.

Dim str As String
Dim FinalComment As String
str=GetWorkbook(handle, "").ActiveSheet.Range(cellref,cellref).Comment.text

GetWorkbook(handle, "").ActiveSheet.Range(cellref,cellref).Comment.Delete
FinalComment= str & Comment
GetWorkbook(handle, "").ActiveSheet.Range(cellref,cellref).AddComment(FinalComment)


@Sachin_Kharmale can you guide us how to check whether comment is already there in that cell, if not we need to add new comment with text else need to append new text with existing text.
 

Sachin_Kharmale

Active Member
Hi Kiss,

To check whether comment is already there in that cell, if not we need to add new comment with text else need to append new text with existing text.
Replace your existing code stage code with bellow code

Dim str As String = Nothing
Dim FinalComment As String = Nothing
Dim ErrorMessage As String = ""
Try
str=GetWorkbook(handle, "").ActiveSheet.Range(cellref,cellref).Comment.text
Catch ex As Exception

End Try
IF str = Nothing Then
GetWorkbook(handle, "").ActiveSheet.Range(cellref,cellref).AddComment(Comment)
Else
str=GetWorkbook(handle, "").ActiveSheet.Range(cellref,cellref).Comment.text
GetWorkbook(handle, "").ActiveSheet.Range(cellref,cellref).Comment.Delete
FinalComment= str & Comment
GetWorkbook(handle, "").ActiveSheet.Range(cellref,cellref).AddComment(FinalComment)

End IF

I Hope it will help you.
 
Top