Hide commandbutton to print document via a created button - vba

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?

Related

Save the values in a userform in vba so they show up on userform after a user saves, exits, and reopen's the doc?

I have multiple userform pages connected to a word document that a user can simply fill out in a form like fashion to populate/generate a document, however I'm having an issue if for instance on userform page 1 a user completes it and clicks the "save/next page" command button and then on the next page they decide to exit out the form and complete the remaining portion at another time since the document is saved and the questions on page 1 have been fully answered and populated in the word doc.
The issue is when the user re-open's the document they are often confused about why the answers they filled out previously aren't still inputted into the userform (They are in the word document still), but it doesn't show the users answers in the text/combo boxes on the userform. Anyone have any solution to this?
Here is an example of the code Word MVP Graham Mayor uses to take data already in document variables to reinitialize a form:
Private Sub UserForm_Initialize()
Set oVars = ActiveDocument.Variables
Caption = "My Example Form"
Label1.Caption = "Forename"
Label2.Caption = "Surname"
Label3.Caption = "Phone Number"
CommandButton1.Caption = "OK"
CommandButton2.Caption = "Cancel"
On Error Resume Next
'Read the values from the document variables if they exist
TextBox1.value = oVars("var1").value
TextBox2.value = oVars("var2").value
TextBox3.value = oVars("var3").value
End Sub

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.

userForm disabling other workbooks

Does anyone know how to enable editing on other workbooks while UserForm is opened? Below makro that I've put into workbook
Private Sub Workbook_Open()
If Environ("USERNAME") <> "name.name" Then
Worksheets("EQUIPMENT").Visible = xlVeryHidden
Worksheets("EQUIPMENTFULL").Visible = xlVeryHidden
Worksheets("SITES").Visible = xlVeryHidden
Worksheets("EMPLOYEES").Visible = xlVeryHidden
Worksheets("NEW").Visible = True
Else: For i = 1 To Worksheets.Count
Worksheets(i).Visible = True
Next i
End If
UserForm.Show
End Sub
It is possible to create modeless (as well as modal) dialog boxes. Modeless dialog boxes allow the user to continue to work in the application while the form is still displayed.
By default all userforms are displayed as modal which means that the user must close the userform before they can contine to use the application. When a UserForm is modal, the user must supply information or close the userform before using any other part of the application. No subsequent code is executed until the userform is hidden or unloaded.
Although other forms in the application are disabled when a userform is displayed, other applications are not.
When the UserForm is modeless, the user can view other forms or windows without closing the UserForm.
Userform1.Show ([modal])
with:
VBA.FormShowConstants.vbModal = 1
VBA.FormShowConstants.vbModeless = 0
(Taken verbatim from https://bettersolutions.com/vba/userforms/modeless.htm)

Change print method from PDF to default printer for a form printout

Hi I am trying to get the print out of a userform. While printing its always prompting to save the userform (a prompt box is being displayed). I don't want it.
I want to get the printout directly after choosing the printer without saving it.
Code below:
Private Sub CommandButton2_Click()
CommandButton2.Visible = False
CommandButton1.Visible = False
Application.Dialogs(xlDialogPrinterSetup).Show
Me.PrintForm
CommandButton2.Visible = True
CommandButton1.Visible = True
End Sub
You may not like this answer, because it involves more workt, but you probably want the userform to add the data into a formatted worksheet and then use the printout method of the worksheet.
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/sheets-printout-method-excel
Worksheets.("yourWorksheet").printout _
activeprinter:= yourPDFprinter, _
PrintTofFile:= True, _
PrToFileName:= filePathAndName
If you want to do it through the UserForm, you're probably going to end up making a bunch of Windows API calls.
At the end of your code you can force a workbook to close without saving any changes, type the following code in a Visual Basic module of that workbook:
Sub Auto_Close()
ThisWorkbook.Saved = True
End Sub
Because the Saved property is set to True, Excel responds as though the workbook has already been saved and no changes have occurred since that last save.
EDIT:
Now I know what you're after I am going to presume you've created a button press for your form, so I think something like https://www.mrexcel.com/forum/excel-questions/544657-specifying-printer-printing-userform-using-vba.html . This will help you with what you want, as it will show you how to change the default print method, form PDF.

Hide textbox while printing

I have a textbox with two command buttons inside that I don't want to show when printing. I've placed the code listed below in the FilePrint and FilePrintDefault Word Commands. If I print using the quick print button, it works perfectly and doesn't print. However, if I print using Ctrl+P and bring up the print dialog, the textbox prints. How can I set it up so that no matter how you print, the textbox won't print? This is on a form letter that several people will use, so I can't just change the Word print settings for everyone, which is why I went with the macro.
Sub FilePrint() ' ' FilePrint Macro ' Prints the active document '
With ActiveDocument
.Shapes(1).Visible = msoFalse
.PrintOut Background:=False
.Shapes(1).Visible = msoTrue
End With
End Sub
Sub FilePrintDefault() ' ' FilePrintDefault Macro ' Prints the active document using the current defaults '
With ActiveDocument
.Shapes(1).Visible = msoFalse
.PrintOut Background:=False
.Shapes(1).Visible = msoTrue
End With
End Sub
You might use the Word application's DocumentBeforePrint event for this purpose. This is where MS explains how to set it up. https://msdn.microsoft.com/en-us/library/office/ff821218.aspx
Actually, in order to display the text boxes again after printing, you use the event procedure to call your existing procedures (be sure to disable their calling the event procedure in a loop) and then cancel the printing. So, by whichever way you initialise the printing process the event procedure takes control, runs your two procedures once, and cancels everything else.