VBA - Disable Restrict Style Changes in Microsoft Word 2016 - vba

I want to disable this Restrict Style Changes in protected Microsoft Word documents with VBA Code.
https://helpdeskgeek.com/office-tips/restrict-editing-on-word-documents/
This code doesn't work for me:
Sub DisableCheckBox()
ActiveDocument.Unprotect
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True, enforcestylelock:=False
End Sub
Any ideas?
Thanks very much.
Edit:
OK. I try to explain my problem.
I have many documents. They have Text and Formular Fields to fill with variable short text. The documents are protected to fill only formulars fields and additionally - and this is the problem - there is activate “limit formating to a selection of styles.”
Complete Text with formular fields is formated to Arial 10 pt. Some formular fields are Arial 12 pt.
When user fill text in the protect document the text is Verdana 12 pt, because this is the default style.That’s the reason I want to deactivate the option “limit formating to a selection of styles.”
Next step, I try in my vba code in a new word document:
https://learn.microsoft.com/en-us/office/vba/api/word.document.protect
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True, enforcestylelock:=False Result: “limit formating to a selection of styles” not activated
Then I try in another new document:
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True, enforcestylelock:=True Result: “limit formating to a selection of styles” activated
The code works for new documents (checkbox “limit formating to a selection of styles” is on or off).
Now I try the code for my existing protected documents with activated option “limit formating to a selection of styles.”
ActiveDocument.Unprotect
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True, enforcestylelock:=False
Result: “limit formating to a selection of styles” is NOT deactivated
I don’t know why?
It is only necessary for me to disable the checkbox option “limit formating to a selection of styles.” with VBA (you can see the checkbox in the picture).
Thank you.
enter image description here

There seems to be a bug with removing the Style restrictions. You could work around that via code like:
Sub DisableCheckBox()
Dim Stl As Style
With ActiveDocument
On Error Resume Next
.Unprotect
On Error GoTo 0
For Each Stl In .Styles
Stl.Locked = False
Next
.Protect NoReset:=True, Type:=wdAllowOnlyFormFields
End With
End Sub
Do note that unless you capture and store the names of the previously allowed or disallowed Styles, the process can't be undone in code later on.

Related

Hide/show a specific page in VBA Word

Hello I have a 2 pages word document with shapes and text boxes on each page. I would like to be able to hide the 2nd page or to show it.
I know we could hide a sheet on excel like bellow but I didn't find out how to do it in word :
Worksheets("Sheet1").visible = False
So I tried with this macro :
Sub HidePage2()
ActiveWindow.DocumentMap = True
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
CommandBars("Navigation").Visible = False
End Sub
I thought I was hiding my page but in fact, it deleted it because if I replace the end of the code by the following code, it do not show my page number 2.
CommandBars("Navigation").Visible = True
I don't know how to fix my problem, could you help me?
Paragraphs and sections are Word VBA objects, pages are not. You can apply a bookmark to each group of paragraphs that you want to hide, plus the page break at the bottom of the page, if there is one. Then use code like this to show or hide it:
Sub HidePage2()
ActiveDocument.Bookmarks("Page2Bookmark").Range.Font.Hidden = True
End Sub
Sub ShowPage2()
ActiveDocument.Bookmarks("Page2Bookmark").Range.Font.Hidden = False
End Sub
Macropod's comment about the printer driver is relevant, because the pagination of the document can change if you have a different active printer. Then what you thought was on page 2 could partially be on page 1 or 3. You can minimise this problem by adding page breaks before and after page 2 and ensuring that page 2 is not completely full.
For example, without needing to bookmark the page beforehand:
Sub Demo()
ActiveDocument.Range.GoTo(What:=wdGoToPage, Name:="2").GoTo(What:=wdGoToBookmark, Name:="\page").Font.Hidden = True
End Sub

Unhighlighting text (and preserve all other font settings)

Thanks to 2 posts (here and here), I know how to highlight text of a textbox in PowerPoint with VBA code.
However, the problem of unhighlighting text remains unsolved. I tried to set properties of a non-highlighted textbox to TextRange2.Font (e.g. .TextFrame2.TextRange.Font.Highlight.SchemeColor = -2) but receive errors when trying so (The typed value is out of range).
Can someone help to solve this issue, please?
Additionally, when changing the highlight color
(e.g. TextRange2.Font.Highlight.RGB = RGB(255, 255, 175)) the formatting of my textbox changes, so the font is changing its color from my preset white to black and the font size gets smaller. Is there any way to preserve the original settings for the textbox? Is this happening due to the access of .TextRange2 and not .TextRange?
Thanks for your help!
In PowerPoint 2019/365 it is possible to remove highlight by using built-in Mso "TextHighlightColorPickerLicensed".
This code sample illustrates how to unhighlight text in selected shapes. It finds Runs containing highlighting, selects them and removes highlight by programmatically invoking Command Bar "Highlight" button.
Preconditions: PowerPoint 2019 or 365. Presentation must be opened with window.
Option Explicit
Sub UnhighlightTextInSelectedShape()
Dim sh As Shape
For Each sh In ActiveWindow.Selection.ShapeRange
UnhighlightTextInShape sh
Next
End Sub
Sub UnhighlightTextInShape(sh As Shape)
On Error GoTo Finish
Dim highlightIsRemoved As Boolean
Dim tf As TextFrame2
Set tf = sh.TextFrame2
Do
Dim r As TextRange2
highlightIsRemoved = True
For Each r In tf.TextRange.Runs
If r.Font.Highlight.Type <> msoColorTypeMixed Then
' Indicate that text contains highlighting
highlightIsRemoved = False
' The text to un-highlight must be selected
r.Select
If Application.CommandBars.GetEnabledMso("TextHighlightColorPickerLicensed") Then
' This Mso toggles highlighting on selected text.
' That is why selection must contain highlight of the same type
Application.CommandBars.ExecuteMso ("TextHighlightColorPickerLicensed")
' Unhighlighting May invalidate number of runs, so exit this loop
Exit For
Else
Exit Do
End If
End If
Next
Loop Until highlightIsRemoved
Finish:
If Not highlightIsRemoved Then
MsgBox "Unhighlighting is not supported"
End If
End Sub
Sometimes Application.CommandBars.ExecuteMso() method gives access to features not available via PowerPoint API.
The MsoId is displayed in tooltip text in PowerPoint options window:

VBA code for unhiding a bookmarked text is not working

I've created an ActiveX dropdown list and each option is linked to a bookmark for the text. Under the ActiveX controls there are the bookmarks (R1 andR2), hidden.
When I hit the btnselect button, all the other bookmarks, except the selected one, get deleted and the selected one becomes visible.
In the bookmark R2
I have a MacroButton for showing/hiding another text (CollapseMentiuniReclamant). When clicking the button it runs either Expand1 sub or Collapse1 sub, but the bookmark CollapseMentiuniReclamant doesn't show up.
I've simplified the document and codes as much as possible. Link to the document - https://wetransfer.com/downloads/1caea3c5d3b05e226e8b8f6a29760ad220190522071742/15db59.
The vba code is:
Private Sub btnselect_Click()
If ComboBox1.Value = "1" Then
Bookmarks("R1").Range.Font.Hidden = False
Bookmarks("R2").Range.Font.Hidden = False
Bookmarks("R2").Range.Delete
End If
If ComboBox1.Value = "2" Then
Bookmarks("R1").Range.Font.Hidden = False
Bookmarks("R1").Range.Delete
Bookmarks("R2").Range.Font.Hidden = False
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = True
End If
End Sub
Sub Expand1()
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Collapse1").Insert _
Where:=Selection.Range
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = False
End Sub
Sub Collapse1()
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Expand1").Insert _
Where:=Selection.Range
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = True
End Sub
Update: I've simplified the last part of code and the problem still persists:
Sub Expand1()
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = False
End Sub
I've even removed the button entirely and ran the macro from View Macros Tab and it's not working.
Why doesn't CollapseMentiuniReclamant show up?
It's not showing up because what you're trying to hide/unhide isn't inside the bookmarked range. In any event, you should be inserting/deleting the content, not simply toggling it's hidden property. Making something hidden is no guarantee it won't be seen or printed (even if not seen), as those settings depend on how the end user has Word configured.

VBA: Code not running after ToggleFormsDesign

I have the following code in VBA (MS Word), that is meant to run after I click in a button, named cmdFormPreencher inserted in my Document:
Private Sub cmdFormPreencher_Click()
'
If ActiveDocument.FormsDesign = False Then
ActiveDocument.ToggleFormsDesign
End If
'
ThisDocument.cmdFormPreencher.Select
ThisDocument.cmdFormPreencher.Delete
ActiveDocument.ToggleFormsDesign
'
UserForm2.Show
End Sub
The purpose of the code above is to delete that button inserted in my document.
But when I run the code only the button is selected. When I tried to figure out what is happening by debugging, it showed me the code runs until ActiveDocument.ToggleFormsDesign and not running the code remaining
Is this a bug of VBA, or am I doing something wrong? If so, how can I get around this problem?
Thanks!
Note: The ActiveX button is not in Header and Footer. The Text Wrap is set to In Front of Text
Edit:
When I try to run a macro, activating FormDesign, Selecting the ActiveX button and then deleting, I get this code:
Sub Macro1()
'
' Macro1 Macro
'
'
ActiveDocument.ToggleFormsDesign
ActiveDocument.Shapes("Control 52").Select
Selection.ShapeRange.Delete
ActiveDocument.ToggleFormsDesign
End Sub
But when I run this code nothing happens...
This is by design. When an Office application is in Design Mode code should not run on an ActiveX object that's part of the document.
I take it this is an ActiveX button and in that case, it's a member of the InlineShapes or Shapes collection - Word handles it like a graphic object. It should be enough to delete the graphical representation, which you can do by changing it to display as an icon instead of a button.
For example, for an InlineShape:
Sub DeleteActiveX()
Dim ils As word.InlineShape
Set ils = ActiveDocument.InlineShapes(1)
ils.OLEFormat.DisplayAsIcon = True
ils.Delete
End Sub
You just have to figure out how to identify the InlineShape or Shape. You could bookmark an InlineShape; a Shape has a Name property.
EDIT: Since according to subsequent information provided in Comments you have a Shape object, rather than an InlineShape, the following approach should work:
Dim shp As word.Shape
Set shp = ActiveDocument.Shapes("Shape Name") 'Index value can also be used
shp.Delete
Note that Word will automatically assign something to the Shape.Name property, but in the case of ActiveX controls these names can change for apparently no reason. So if you identify a control using its name instead of the index value it's much better to assign a name yourself, which Word will not change "on a whim".
Activate Design Mode.
Click on the control to select it
Go to the VB Editor window
Ctrl+G to put the focus in the "Immediate Window"
Type the following (substituting the name you want), then press Enter to execute:
Selection.ShapeRange(1).Name = "Name to assign"
Use this Name in the code above

Hide commandbutton to print document via a created button

This is my first post on here, I will try to be as clear as possible :)
I'm creating a Microsoft Word form for users to fill in, this form is protected and only the forms can be filled in the rest of the document is protected with the password: "mypass"
I want to have a button on the document it self wich prints the active document. What I did is create a print button into a “drawing” Textbox ( Insert | Textbox ) as stated here.
This Print button must be hidden so it's not visible on the document when it's printed.
Here is the code:
Private Sub CommandButton1_Click()
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:="mypass"
End If
With ActiveDocument
.Shapes(1).Visible = msoFalse
.PrintOut Copies:=1
.Shapes(1).Visible = msoTrue
End With
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, Password:="mypass"
End Sub
When I click the print button in protective mode nothing happens, when I turn off protective mode I get this error:
Error 4641 at run.
ToolsProtectDocument statement is currently disabled
To summarize:
The Print button I created does not work when protective mode is enabled.
When protective mode is disabled I get the error that the ToolsProtectDocument statement is currently disabled.
I want to have a procted form that can be filled in and be printed from the form itself without the print button being visable on the printed form.
Does anyone have a clue?