fuliorew.blogg.se

Vba word documents open read only
Vba word documents open read only










Provided the user has clicked the Yes button in the first message box, the next If statement checks whether the active document's path is an empty string ( "" double quotation marks with no characters between them). If the user clicks the Yes button, returning the value vbYes, the rest of the macro runs if not, execution branches to the End If statement just before the End Sub statement, and the macro ends without taking any action. The first If statement displays a message box to confirm that the user wants to run the macro. (This will be used for the macro to display this page again after reopening the document.) The intPage variable gets the page number of the end of the current selection. The strTitle variable receives the text to be displayed in the titlebar of each message box. The strDocFullName variable receives the document's name and path. The strDocName variable receives the document's name (without the path). The third group of lines assigns information to the variables. The fifth declaration creates an Integer variable, which is a variable that can contain only a whole number (either positive or negative). The first three declarations create String variables, which are variables that can hold text characters. The format is Dim variablename As variabletype. The second group of lines uses Dim statements to declare variables, or storage slots for data used in the macro. The first few lines are comments explaining what the macro does. Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=intPage "click No to discard the changes.", Buttons:=vbYesNo + vbQuestion, _ĪctiveDocument.Close SaveChanges:=wdDoNotSaveChangesĭocuments.Open FileName:=strDocFullName, ReadOnly:=True & vbCr & "Click Yes to save the changes " & _ If MsgBox(Prompt:="The document contains unsaved changes." & vbCr _ If Dialogs(wdDialogFileSaveAs).Show = 0 Then MsgBox Prompt:="This document has never been saved." & vbCr & vbCr _īuttons:=vbOKOnly + vbExclamation, Title:=strTitle If MsgBox(Prompt:="Close " & strDocName & " and reopen as read-only?", _īuttons:=vbYesNo + vbQuestion, Title:=strTitle) = vbYes Then IntPage = Selection.Information(wdActiveEndPageNumber) StrTitle = "Reopen Document As Read-Only" 'store the page number and return to it when the document is reopened 'close the active document and reopen it as read-only Here's what's happening: Macro to switch the active document to read-only status Example 8-4 Sub Reopen_Active_Document_As_Read_Only() Then statements, it's pretty straightforward.

#Vba word documents open read only windows#

Also, from Windows Explorer, you can right-click a Word document and choose "Open as Read-Only" from the shortcut menu.Įxample 8-4 probably looks impenetrable, but once you get the hang of the If. But you can create a macro that does this well enough (see Example 8-4). The Solution:Īs you say, there's no simple checkboxor other controlto switch an open document to read-only status. What I'd like is a box to check that tells Word I want to handle a document as "read only" after I've already opened it. I know I could open them as read-only from the Open dialog box by using the drop-down on the Open button, but I usually open them directly from email or from Explorer, so I don't have that choice. I receive lots of documents that I need to read but that I'm not supposed to change.










Vba word documents open read only