Tech Eval

G

Greg

I have been being schooled here of late on writing clean
code. I don't know if this is a crime, but my normal
practice is to find what I want to do in a google search
start hacking. Several months ago I was looking for the
code to delete all comments in a documment. This is what I
found:

'Dim lngCount, lngIndex As Long
'With ActiveDocument.Comments
' lngCount = .Count
' If lngCount > 0 Then
' For lngIndex = lngCount To 1 Step -1
' .Item(lngIndex).Delete
' Next
' End If
'End With

I was looking at this today and thought I would probe why
it appeared so involved. As the idea is to just delete
all comments that it could be simplier. Here is my
hacking:

With ActiveDocument.Comments
Do Until .Count = 0
.Item(.Count).Delete
Loop
End With

Both variants work just fine. Mine seems simplier, but is
it clean?

Thanks for your time.
 
J

Jonathan West

Greg said:
I have been being schooled here of late on writing clean
code. I don't know if this is a crime, but my normal
practice is to find what I want to do in a google search
start hacking. Several months ago I was looking for the
code to delete all comments in a documment. This is what I
found:

'Dim lngCount, lngIndex As Long
'With ActiveDocument.Comments
' lngCount = .Count
' If lngCount > 0 Then
' For lngIndex = lngCount To 1 Step -1
' .Item(lngIndex).Delete
' Next
' End If
'End With

I was looking at this today and thought I would probe why
it appeared so involved. As the idea is to just delete
all comments that it could be simplier. Here is my
hacking:

With ActiveDocument.Comments
Do Until .Count = 0
.Item(.Count).Delete
Loop
End With

Both variants work just fine. Mine seems simplier, but is
it clean?

Yours will be a bit slower, because you access the object model every time
round the loop to get the .Count property. If you are deletign all comments,
you only need to find out how many there are once.

The original code is going backwards and deletign the last comment every
time. In fact, faster still is to go round the loop the appropriate number
of times and delete the *first* comment every time (of those comemnts thart
remain in the document. So the following code should go a bit faster still

Dim lngIndex As Long
With ActiveDocument.Comments
For lngIndex = 1 to .Count
.Item(1).Delete
Next
End With
 
G

Greg

Jonathan,

Copy all. Thanks.
-----Original Message-----



Yours will be a bit slower, because you access the object model every time
round the loop to get the .Count property. If you are deletign all comments,
you only need to find out how many there are once.

The original code is going backwards and deletign the last comment every
time. In fact, faster still is to go round the loop the appropriate number
of times and delete the *first* comment every time (of those comemnts thart
remain in the document. So the following code should go a bit faster still

Dim lngIndex As Long
With ActiveDocument.Comments
For lngIndex = 1 to .Count
.Item(1).Delete
Next
End With

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top