I have the following code that works separately perfect, but when I combined both code parts (comments and text box char/words counting parts) together it pops out the following error WHEN I ADD ANY COMMENT:
445: Object doesn't support this action
Line: If TypeName(shp) <> "GroupObject" And shp.TextFrame2.TextRange.Characters.Text <> "" Then
I cant figure out WHY? I cant imagine any other method than this. Below I have added the combined code. I hope somebody have the right answer to this issue.
Code
Sub CountCharWorBOXCMT()
'Counts Comments and Text Box Chars and Words
Dim wks As Worksheet, cmt As Comment, lCommentch, lCommentwords, lTxtBoxChar, lTxtBoxCharWords As Long
Dim shp As Shape, sMsg, lTxtBoxStr As String
Application.ScreenUpdating = False
lCommentch = 0
lCommentwords = 0
lTxtBoxChar = 0
lTxtBoxCharWords = 0
For Each wks In ActiveWorkbook.Worksheets
'Count Char/Words from txtbox
For Each shp In wks.Shapes
'Count Chars from Text Box
If TypeName(shp) <> "GroupObject" Then
lTxtBoxChar = lTxtBoxChar + shp.TextFrame.Characters.Count
End If
'Count Words from Text Box
If TypeName(shp) <> "GroupObject" And shp.TextFrame2.TextRange.Characters.Text <> "" Then
lTxtBoxStr = shp.TextFrame2.TextRange.Characters.Text
lTxtBoxCharWords = lTxtBoxCharWords + Len(Trim(lTxtBoxCharStr)) - Len(Replace(Trim(lTxtBoxCharStr), " ", "")) + 1
End If
'Count Char/Words from comments
For Each cmt In wks.Comments
lCommentch = lCommentch + Len(cmt.Text)
lCommentwords = lCommentwords + Len(Trim(cmt.Text)) - Len(Replace(Trim(cmt.Text), " ", "")) + 1
Next cmt
Next shp
Next wks
sMsg = Format(lTxtBoxChar, "### ### ##0") & " chars from Text Box" & vbCrLf
sMsg = sMsg & Format(lTxtBoxCharWords, "### ### ##0") & " words from Text Box" & vbCrLf & vbCrLf
sMsg = sMsg & Format(lCommentch, "### ### ##0") & " chars from Comments" & vbCrLf
sMsg = sMsg & Format(lCommentwords, "### ### ##0") & " words from Comments" & vbCrLf & vbCrLf
MsgBox Prompt:=sMsg, Title:="Grand Total"
End Sub
Display More
I assume that shp.TextFrame2.TextRange.Characters.Text part made this issue, but why if it works separately? Any suggestions?