Share via

MACRO- Protecting a form

Anonymous
2017-01-10T17:22:29+00:00

I recently created a template with macros. I Created a fill-able form  word template that opens a new document every-time it open. I want to protect my template so no one can edit it.  but for some reason i am not able to  do so. I am having trouble with the macro and the protecting form

How can i add protection to the document and to the macro so there is no error??

Sub AutoNew()

Order = System.PrivateProfileString("C:\Settings.Txt", _

        "MacroSettings", "Order")

If Order = "" Then

    Order = 1

Else

    Order = Order + 1

End If

System.PrivateProfileString("C:\Settings.txt", "MacroSettings", _

        "Order") = Order

ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "00#")

ActiveDocument.SaveAs FileName:="S:\DBS\ACSkyways work order\work order" & Format(Order, "00#")

End Sub

please help!!

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2017-01-11T02:11:36+00:00

    Further to Jay's response, if you insert a { Docvariable Order } in place of the bookmark and then replace

    ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "00#")

    ActiveDocument.SaveAs FileName:="S:\DBS\ACSkyways work order\work order" & Format(Order, "00#")

    by

    With ActiveDocument

       .Variables("Order").Value = Format(Order, "00#")

       .PrintPreview

       .ClosePrintPreview   .SaveAs FileName:="S:\DBS\ACSkyways work order\work order" & Format(Order, "00#")

    End With

    you can then apply the protection to the template itself, rather than using code to do it.

    Was this answer helpful?

    0 comments No comments
  2. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2017-01-11T01:38:15+00:00

    Between the ActiveDocument.Bookmarks statement and the ActiveDocument.SaveAs statement, insert this statement:

    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, Password:=""

    If you want to have a password that's required to turn off the protection, include it between the quote marks.

    Note that the template should not be protected for forms, because that would prevent the insertion of the order number at the bookmark. The protection must be turned on after that insertion, but before the document is saved.

    Was this answer helpful?

    0 comments No comments