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!

Restrict editing in Word 2010 and VBA

Status
Not open for further replies.

JasonEnsor

Programmer
Sep 14, 2010
193
GB
Hi Guys,

I am currently looking at improving an old VBA Word document I set up, where users fill in a userform and the documents front page bookmarks are populated with the data. At the end of the first page we have a slogan (this is always on the last line). What I would like to do is add a section break after this slogan then lock the first page so that it can not be edited. I know I can do this manually but I am looking at generating the documents from excel as we produce a fair amount of them.

So any ideas on how I would insert a section break and then lock the first page to be restricted with a password and set as read only?

Any Pointers or ideas would be appreciated

J.
 
I can do this manually
So, which code is generated when you do this manually while the macro recorder is running ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV,

Apologies for not adding the code.

Recording the Macro gives me this. I tend to do most of my VBA in excel so I am not that hot on the Word Document model.
Code:
' Inserts Page Break
    Selection.InsertBreak Type:=wdSectionBreakNextPage

' Used Cursor to move down the page slightly to create a selection
    Selection.MoveDown Unit:=wdLine, Count:=27, Extend:=wdExtend

' Set everyone to be able to edit page 2 onwards
    Selection.Editors.Add wdEditorEveryone

' Set protection
    ActiveDocument.Protect Password:="test", NoReset:=False, Type:= _
    wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=False

Many Thanks

J.
 
move down the page slightly
How would you quantify that? Don't believe that Slightly is in the Word Object Model as a Method. ;-)

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
...or as a wdSlightly literal.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Maybe that's where I am going wrong... I thought 'Slighty' was a valid Word operation... It would make the Object model so much easier in my opinion. As would Application.DoWhatIWant()
 
Do you already have some code in Excel generating the document ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV,

The current system I have is to aid generating exam papers where by certain formatting has be adhered to, so my first attempt at this was to setup a template with the correct formatting, then upon opening the document a userform opens up asking for details of the exam paper that then populates the bookmarks in the template. This means the Academics don't have to format the exam papers.

As some academics seem to avoid using the system correctly, my next improvement was to generate the exam paper with the front page all pre-populated and then lock it down so all they have to do is enter the questions. All of the information for these papers come from a PeopleSoft report in to an excel document, so I was just going to use some pre-existing code I have from another project to loop through excel and create the word document based on the template, populate the data, lock it down and send it to them....in the hopes it will encourage them to use the system more as the workload has been reduced for them even more.

I have been looking at trying to use VSTO for this and create a custom backstage view that will track the status of the document, as the exam paper goes through several types of moderation, but at the moment I am more focused on locking down the front cover as that seems to be where they make the mistakes or edit details that shouldn't be edited.

So I guess the answer is I do have code to generate the documents, but I have not implemented it yet.

J.
 
Hey Guys,

Just an update to say i figured this out, was actually pretty simple. As I had created a template i placed my section break on the template, before saving it. Then i used VBA to go to the start of the second page, Select everything until the end of the page, then apply the protection.

I have included my code below incase someone else ever wants to do this.

Code:
Option Explicit

Sub LockFirstPage()

	Dim docPages As Range

	Selection.GoTo what:=wdGoToPage, which:=wdGoToAbsolute, Count:=2
	Set docPages = Selection.Range
	docPages.End = Selection.Bookmarks("\Page").Range.End
	docPages.Select
	Selection.Editors.Add wdEditorEveryone
	ActiveDocument.Protect wdAllowOnlyReading, False, "password", False, False


End Sub

Regards

J.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top