Sort range in table

V

VBA Neophyte

I have a macro creating a document with a table containing multiple header
rows. I want to sort on the other rows. To exclude the header rows, I define
and then select a range containing these other rows. But when I try to sort
the selection I get a message that the document is protected (which it's
not). Here's the error:
"Run-time error 4605: This method or property is not available because the
document is a protected document."
See code below. The Selection.Sort statement is the highlighted error.
Any idea why? Or, is there a better way to sort a range in a table? (I've
tried just sorting with a range, same result.)

Here's the code:
With ActiveDocument
Set sortRange = Range(Start:=.Tables(1).Rows(7).Range.Start, _
End:=.Tables(1).Rows.Last.Range.End)
sortRange.Select
Selection.Sort ExcludeHeader:=False, FieldNumber:="Column 2", _
SortFieldType:=wdSortFieldAlphaNumeric, _
SortOrder:=wd SortOrderDescending, _
FieldNumber2:="Column 1", _
SortFieldType2:=wdSortFieldNumeric, _
SortOrder2:=wd SortOrderAscending
End With
 
L

Lene Fredborg

I tested you code on a table and did not get any warning unless I protected
the document. Could it be that you have turned on a type of protection by
incident (directly in the document or in code running before the sort code)?

You could try to insert the following line before the sort code:
msgbox activedocument.ProtectionType

In case the document is not protected, the value should be -1.

The ProtectionType values are:
-1 = wdNoProtection
0 = wdAllowOnlyRevisions
1 = wdAllowOnlyComments
2 = wdAllowOnlyFormFields
3 = wdAllowOnlyReading

As far as I can see, your code will run both in case of ProtectionType -1
and 0 but not in case of ProtectionType 1, 2 or 3 (which results in error
4605).

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
V

VBA Neophyte

Thank you very much for the suggestion. The result is very strange. I did as
you suggested, and got the ProtectionType "-1". I feel that there may be
some other underlying error preventing the sort, but wrongly described as a
4605.

However, thanks for running the code, at least I know it works syntactically.
 

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