Tips for MS Word 2000

Macro to Save & Name New Documents Automatically

by Cheryl Flanders ?/small> Wed, Sep 04, 2002, 17:11:38 Reply   Forum

When you start a new document in Word, it is a good idea to save and name it as soon as you begin working. Since you are going to have to select File / Save As and give the file a name anyway, create a macro to prompt you for the name as soon as the document opens.

To create the macro:

1. Select Tools / Macro / Macros and type in AutoNew.
2. Click the arrow at the right side of the Macros Available In list box and select Normal.dot (Global Template).
3. Click Create and type in (or copy and paste directly from this post) the two-line macro below between the name of the macro and End Sub:

FName$ = InputBox$("What do You Want to Name This File?", "New File")
ActiveDocument.SaveAs FName$, wdFormatDocument

4. Go to File and click save to Normal, then File and click option to return to Word.
5. Now when you open a new document with Ctrl + N (or your mouse) Word will automatically present a dialog box for your document name.
6. Enter a name without the extension. Word will automatically append the extension .doc to your filename.
7. Press Enter to close the dialog box and save the document. The Title Bar will reflect the new name.

The macro name AutoNew will automatically run the macro; there is no need to assign a keyboard shortcut.

NOTE: Files are automatically saved to the last Folder you had open. When you open Word and save the first document, Save defaults to My Documents (unless you changed the default to another folder location). When you go to File/Open and select another folder in the Look In box, the default Save is now to that folder.

If you don't want the macro to save in the current folder selected, just click on Cancel, then End and save as you normally would in the chosen folder.

(Quoted from Tips 'N Techniques in MS Word 97/2000/2002)

~
 

MS Word - Form Flaw Gets a Fix.
Using a Simple VBA Procedure to Correct the Unlock Flaw.
 More of this Feature
• Microsoft Support
• Simple Solution
 
 
 Related Resources
• Using Word Forms
• Sample Forms
• Downloadable Samples

Microsoft Word has a nice feature for developing simple templates that include the facility to setup a document as a form. Users are able to insert their input into a controlled layout, created by the developer.
Sounds great, but should a user require to make a minor modification to the layout, the act of unlocking and locking a form causes the form to lose all current data. This is frustrating for the developer and user.
The solution:
Try this macro:

1. Open the VBA interface. ( Alt- F11 )

2. In the Project Window, right click on your document and insert a module.

2. Copy the following into the Module code window.

 

Sub ProtectForm()

' ******************************************
' ProtectForm Macro.
' Toggles protection for the active document
' when the Protect Form button on the forms
' toolbar is clicked.
' ******************************************

If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
Else
ActiveDocument.Unprotect Password:=""
End If

End Sub
Sub ToolsProtectUnprotectDocument()

' ******************************************
' ToolsProtectUnprotectDocument Macro
' Sets protection for the active document
' when Protect Document or Unprotect Document
' is clicked on Tools menu.
' ******************************************

If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
Else
ActiveDocument.Unprotect Password:=""
End If

End Sub
 

 

This code will address change the actions of the unprotect / protect button on the form toolbar so that all data is not lost by using this feature.

To access a blank form, simply open the template again.

For more information, please read Q181108 from Microsoft support.

WD97: Form Fields Lose Text When Protected for Forms

 
The information in this article applies to:
 

SUMMARY

This article describes methods that allow you to do the following:

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:


For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:

Method 1: Alter the Protect/Unprotect Command Functionality

The following Microsoft Visual Basic for Applications macros (Sub procedures) protect your form without causing you to lose the text that you entered into a form field. The macros can be stored in the actual form template to allow you to manually unprotect and reprotect the form while preserving the form field contents.

The following three macros can be used to ensure that your form field values are not reset to their defaults when you reprotect the form.

NOTE: The name of this macro must be ProtectForm.

Sub ProtectForm()

   ' ******************************************
   ' ProtectForm Macro.
   ' Toggles protection for the active document
   ' when the Protect Form button on the forms
   ' toolbar is clicked.
   ' ******************************************

   If ActiveDocument.ProtectionType = wdNoProtection Then
      ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
   Else
      ActiveDocument.Unprotect Password:=""
   End If

End Sub

The following sample Visual Basic macro protects the active document without displaying the Protect dialog box. When you run this macro, it will reprotect the active document while maintaining previous form field values.

NOTE: The name of this macro must be ToolsProtectUnprotectDocument.

Sub ToolsProtectUnprotectDocument()

   ' ******************************************
   ' ToolsProtectUnprotectDocument Macro
   ' Sets protection for the active document
   ' when Protect Document or Unprotect Document
   ' is clicked on Tools menu.
   ' ******************************************

   If ActiveDocument.ProtectionType = wdNoProtection Then
      ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
   Else
      ActiveDocument.Unprotect Password:=""
   End If

End Sub

The following sample Visual Basic macro allows you to specify which sections to protect while maintaining previous form field values. You can assign this macro to a toolbar button or menu.

Sub ProtectNoReset()

   Dim pDoc As Dialog
   Dim x As Integer
   On Error GoTo ProtectNoResetErr

   ' If the document is protected,
   If ActiveDocument.ProtectionType <> wdNoProtection Then

      ' Unprotect the document.
      ActiveDocument.Unprotect

   End If

   ' Display the Protect Dialog box.
   Set pDoc = Dialogs(wdDialogToolsProtectDocument)
   x = pDoc.Display

   ' If Cancel was chosen, exit this procedure.
   If x = 0 Then Exit Sub

   ' Protect the document.
   ActiveDocument.Protect Password:=pDoc.DocumentPassword, _
      NoReset:=True, Type:=2

ProtectNoResetErr: 'NOTE: This line MUST be left aligned.
   If Err <> 0 Then MsgBox Err.Description

End Sub

Method 2: Create a Macro to Protect/Unprotect Your Document

The following examples protect the active document for forms without resetting the contents of the form fields. Create the macro and assign the macro to a key, menu, or toolbar button for easy access.

Sub ProtectIt()

   If ActiveDocument.ProtectionType = wdNoProtection Then
      ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
   End If

End Sub

Method 3: Unprotect, Check Spelling or Update a Field, Reprotect a Document

Because form field text is formatted for No Proofing, you can use the following macro to do the following:

You can use this macro as an On Exit macro for the last form field, so you can check the spelling or update a field before you save the form.

Sub FormsSpellCheck()

   ' If document is protected, Unprotect it.
   If ActiveDocument.ProtectionType <> wdNoProtection Then
      ActiveDocument.Unprotect Password:=""
   End If

   ' Set the language for the document.
   Selection.WholeStory
   Selection.LanguageID = wdEnglishUS

   ' Perform Spelling/Grammar check.
   If Options.CheckGrammarWithSpelling = True Then
      ActiveDocument.CheckGrammar
   Else
      ActiveDocument.CheckSpelling
   End If

   ' ReProtect the document.
   If ActiveDocument.ProtectionType = wdNoProtection Then
      ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
   End If

End Sub

IMPORTANT NOTE: There are some differences in Visual Basic for Applications between Microsoft Word 97 for Windows and Microsoft Word 2000 due to the Enabled Language Settings feature in Microsoft Office 2000. One of the differences is noted in the above macro. To correctly check the spelling of a document after setting the LanguageID, you must set the NoProofing property in Word 2000. For additional information about how to do this in Microsoft Word 2000, click the article number below to view the article in the Microsoft Knowledge Base:

WD2000: How to Retain Information Typed into a Form Field When You Protect a Form

 
The information in this article applies to:
 

SUMMARY

This article describes methods that allow you to do the following:

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:


For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:

Method 1: Alter the Protect/Unprotect Command Functionality

The following Microsoft Visual Basic for Applications macros (Sub procedures) protect your form without causing you to lose the text that you entered into a form field. The macros can be stored in the actual form template to allow you to manually unprotect and reprotect the form while preserving the form field contents.

The following three macros can be used to ensure that your form field values are not reset to their defaults when you reprotect the form.
 

NOTE: The name of this macro must be ProtectForm.

Sub ProtectForm()

   ' ******************************************
   ' ProtectForm Macro.
   ' Toggles protection for the active document
   ' when the Protect Form button on the forms
   ' toolbar is clicked.
   ' ******************************************
   If ActiveDocument.ProtectionType = wdNoProtection Then
      ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
         NoReset:=True
   Else
      ActiveDocument.Unprotect Password:=""
   End If

End Sub

The following sample Visual Basic macro protects the active document without displaying the Protect dialog box. When you run this macro, it reprotects the active document while maintaining previous form field values.

NOTE: The name of this macro must be ToolsProtectUnprotectDocument.

Sub ToolsProtectUnprotectDocument()

   ' ******************************************
   ' ToolsProtectUnprotectDocument Macro
   ' Sets protection for the active document
   ' when Protect Document or Unprotect Document
   ' is clicked on Tools menu
   ' ******************************************
   If ActiveDocument.ProtectionType = wdNoProtection Then
      ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
         NoReset:=True
   Else
      ActiveDocument.Unprotect Password:=""
   End If

End Sub

The following sample Visual Basic macro allows you to specify which sections to protect while maintaining previous form field values. You can assign this macro to a toolbar button or menu.

Sub ProtectNoReset()

   Dim pDoc As Dialog
   Dim x As Integer
   On Error GoTo ProtectNoResetErr

   ' If the document is protected,
   If ActiveDocument.ProtectionType <> wdNoProtection Then

      ' Unprotect the document.
      ActiveDocument.Unprotect

   End If

   ' Display the Protect Dialog box.
   Set pDoc = Dialogs(wdDialogToolsProtectDocument)
   x = pDoc.Display

   ' If Cancel was chosen, exit this procedure.
   If x = 0 Then Exit Sub

   ' Protect the document.
   ActiveDocument.Protect Password:=pDoc.DocumentPassword, _
      NoReset:=True, Type:=2

ProtectNoResetErr: 'NOTE: This line MUST be left aligned.

If Err <> 0 Then MsgBox Err.Description

End Sub

Method 2: Create a Macro to Protect/Unprotect Your Document

The following examples protect the active document for forms without resetting the contents of the form fields. Create the macro and assign the macro to a key, menu, or toolbar button for easy access.

If ActiveDocument.ProtectionType = wdNoProtection Then
   ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
      NoReset:=True
End If

Method 3: Unprotect, Check Spelling or Update a Field, Reprotect a Document

Because form field text is formatted for No Proofing, you can use the following macro to:

You can use this macro as an On Exit macro for the last form field so you can check the spelling or update a field before you save the form.

Sub FormsSpellCheck()

   ' If document is protected, Unprotect it.
   If ActiveDocument.ProtectionType <> wdNoProtection Then
      ActiveDocument.Unprotect Password:=""
   End If

   ' Set the language for the document.
   Selection.WholeStory
   Selection.LanguageID = wdEnglishUS
   Selection.NoProofing = False

   ' Perform Spelling/Grammar check.
   If Options.CheckGrammarWithSpelling = True Then
      ActiveDocument.CheckGrammar
   Else
      ActiveDocument.CheckSpelling
   End If

   ' ReProtect the document.
   If ActiveDocument.ProtectionType = wdNoProtection Then
      ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
         NoReset:=True
   End If

End Sub

IMPORTANT NOTE: There are some differences in Visual Basic for Applications between Microsoft Word 2000 and Microsoft Word 97 for Windows due to the Enabled Language Settings feature in Microsoft Office 2000. One of the differences is noted in the above macro. To correctly check the spelling of a document after setting the LanguageID, you must set the NoProofing property in Word 2000. However, if you attempt to run this macro in Microsoft Word 97 for Windows, you will receive the following error message:
 

Compile error:

Method or data member not found

For additional information about how to do this in Microsoft Word 97 for Windows, click the article number below to view the article in the Microsoft Knowledge Base:

REFERENCES

For additional information about getting help with Visual Basic for Applications, click the article numbers below to view the articles in the Microsoft Knowledge Base:


 

 

Hosted by www.Geocities.ws

1