Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a macro to select text in a protected Word document

Status
Not open for further replies.

Designware

Technical User
Sep 24, 2002
202
Hi,

I am trying to use a macro to select, copy and paste a specific piece of data in a protected document. The data area is the 2nd unprotected area from the top of this document. While I'm in the protected document, I can manually click CTRL-HOME to take me to the top of the document, and then either the down-arrow, or the tab-key to take me to the desired area, and the entire data area is ALREADY selected. I am trying to duplicate this with a macro.

The very simple macro I have so far is:

Sub Macro1()
Selection.HomeKey Unit:=wdStory
Selection.MoveDown
End Sub

This gets me to the beginning of the desired data area, but nothing is selected. I have also tried "Selection.TypeText Text:=vbTab" in place of "Selection.MoveDown", but I get an error message.

I have also tried adding a Shift-End(Selection.EndKey Unit:=wdLine, Extend:=wdExtend) after the "Selection.MoveDown" to attempt to get the entire data area selected (as it does when you're manually keying it in). However, this simply takes the cursor to the end of the data area without selecting any text.

How do I get this data area selected for subsequent copying and pasting? The data area is NOT a defined number of characters. The user can type as much or as little as they wish into that area.

Thanks in advance for your help.

DSI
 


hi,

in your macro

1) UNPROTECT

2) do yer stuff

3) PROTECT

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi Skip,

According to the OP, the required data are already in an unprotected 'area' (ie Section). So, to copy, it's just a matter of:
ThisDocument.Sections(#).Range.Copy
where # is the Section number.

Cheers
Paul Edstein
[MS MVP - Word]
 
So, for example:
Code:
Sub UnprotectedSection2()
ActiveDocument.Sections(2).Range.Copy
Documents.Add
ActiveDocument.Range.Paste
End Sub
copies Section 2 ("The data area is the 2nd unprotected area from the top of this document") to a new document.

55,687.00 hours down....
22 hours to go

tick-tick tick-tick tick-tick
 
Thank you for the responses.

I tried:

ActiveDocument.Sections(2).Range.Copy

and I get the error message

"The requested member of the collection does not exist."

I tried several numbers in the selections parameter with the same problem. Do I have to name/number the sections? If so, where? I went into the properties and found the bookmark meta-tag. I placed the number 2 in there, but same result. I didn't see anything else in properties where I could name it.

Thanks again.

Dale
 


in the Immediate Window enter
Code:
?ActiveDocument.Sections.Count
That is the nuber of Section Objects are in the ActiveDocument

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Ummm. If Sections(2) does not exist...then it does not exist.

Something is wrong.

I have tested the code and it does work. I will shortly be off-line from this forum (I am retiring), so I will not be able to help anymore, but I am sure someone will be able to do so for you.

Perhaps go through your document, and list out for us exactly what Sections are protected, and what are not.

55,687.00 hours down....
<10 hours to go

tick-tick tick-tick tick-tick
 
Thanks for the responses.

fumei, I hope you enjoy your retirement and thanks for the help!

SkipVought, when I did the ?ActiveDocument.Sections.Count it told me there was only 1 section.

FYI, the protected document is a FORM that was created, where we added controls (i.e. text boxes) to the document. Once we protect the entire document, only these controls are able to be modified.

There are probably 60+ controls in this document.

I appreciate any further thoughts.

Thanks.

Dale
 
Hi Dale,

Let's look at what you first posted:
The data area is the 2nd unprotected area from the top of this document. While I'm in the protected document, I can manually click CTRL-HOME to take me to the top of the document, and then either the down-arrow, or the tab-key to take me to the desired area, and the entire data area is ALREADY selected.

What you have described here is only possible if the document has Sections; otherwise there could not be a '2nd unprotected area from the top' when you're 'in the protected document'.

But now you have told us:
ActiveDocument.Sections.Count it told me there was only 1 section.
If that is so, then EITHER:
1. All of the document is protected and there is no '2nd unprotected area from the top'; OR
2. None of the document is protected (which is why you can select someting in what you describe as the '2nd unprotected area from the top'.





Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top