Using VBA to prevent image printing in Word .doc - vba

I set up a resume which has an 8.5 by 11 image in the background (could set it up to be a watermark if need be). Now i would like to get it set up so that it will not print the background image automatically so that the employer doesn't have to jump through hoops. After looking online I noticed that this is probably something that must be set up using VBA and templates. Any insite or anyone willing to tackle this it would be greatly appreciated.
Pretty much i just want my Word document to not print images or watermarks with out having the person who prints it set anything up.(doesn't have to be both)

Do you really need VBA for this?
To disable printing of background colors and images in Word 2010, simply follow these steps
Click on File | Option
You will get a "Word Options" Dialog Box.
Under Display Tab, simply uncheck "Print BackGround Colors and Images"

I use the following (short) procedure as a ribbon extension for showing/hiding logo's in document headers so users can toggle before print/pdf creation.
Sub ShowHideLogoInHeader
local errorhandler (not critical)
On Local Error GoTo ErrHandler
dim shaperange
Dim myStory As ShapeRange
Set myStory = ActiveDocument.StoryRanges(wdFirstPageHeaderStory).ShapeRange
myStory.Visible = Not myStory.Visible
Exit Sub
ErrHandler:
report error (separate function) in debug
ReportError "modRibbon - ToonOfVerbergLogo"
Err.Clear
End Sub

Do not know why but this semes to work just fine.
Public WithEvents appWord As Word.Application
Private Sub Document_Open()
Set appWord = Application
' Not sure if your image is a shape or inlineshape, so...
If ThisDocument.Shapes.Count Then
' First Shape is now visible
ThisDocument.Shapes(1).Visible = msoTrue
ElseIf ThisDocument.InlineShapes.Count Then
' First inlineshpae has medium brightness
ThisDocument.InlineShapes.Item(1).PictureFormat.Brightness = 0.5
End If
End Sub
Private Sub appWord_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
Dim intResponse As Integer
intResponse = MsgBox("This document contains a background image. " & _
"Would you like to hide it before printing?", vbYesNo, _
"Hide Background Image?")
If intResponse = vbYes Then
hide_images
ElseIf intResponse = vbNo Then
show_images
End If
End Sub
Sub hide_images()
Application.DisplayStatusBar = True
With ActiveWindow
.DisplayHorizontalScrollBar = True
.DisplayVerticalScrollBar = True
.DisplayLeftScrollBar = False
.StyleAreaWidth = CentimetersToPoints(0)
.DisplayVerticalRuler = True
.DisplayRightRuler = False
.DisplayScreenTips = True
With .View
.ShowAnimation = True
.Draft = False
.WrapToWindow = False
.ShowPicturePlaceHolders = False
.ShowFieldCodes = False
.ShowBookmarks = False
.FieldShading = wdFieldShadingWhenSelected
.ShowTabs = False
.ShowSpaces = False
.ShowParagraphs = False
.ShowHyphens = False
.ShowHiddenText = False
.ShowAll = True
.ShowDrawings = True
.ShowObjectAnchors = False
.ShowTextBoundaries = False
.ShowHighlight = True
End With
End With
With Options
.UpdateFieldsAtPrint = False
.UpdateLinksAtPrint = False
.DefaultTray = "Druckereinstellungen verwenden"
.PrintBackground = True
.PrintProperties = False
.PrintFieldCodes = False
.PrintComments = False
.PrintHiddenText = False
.PrintDrawingObjects = False
.PrintDraft = False
.PrintReverse = False
.MapPaperSize = True
End With
With ActiveDocument
.PrintPostScriptOverText = False
.PrintFormsData = False
End With
End Sub
Sub show_images()
With Options
.UpdateFieldsAtPrint = False
.UpdateLinksAtPrint = False
.DefaultTray = "Druckereinstellungen verwenden"
.PrintBackground = True
.PrintProperties = False
.PrintFieldCodes = False
.PrintComments = False
.PrintHiddenText = False
.PrintDrawingObjects = True
.PrintDraft = False
.PrintReverse = False
.MapPaperSize = True
End With
With ActiveDocument
.PrintPostScriptOverText = False
.PrintFormsData = False
End With
End Sub
Cheers
mARTin

Related

How to send focus to a text box of a VBA form during its initialization/activate event?

I have a VBA form in Corel. Behaving exactly like a similar one in Excel...
Initially, when the form initialize event used to contain only some lines of code, the simple ending line me.txtCsv.Setfocus used to send the focus on it. I mean, it appeared being in edit mode with the cursor blinking inside.
After a period of time, after the application became complex, I am not able to send the focus to the text box in discussion.
I know that Activate event goes last and I also have in it the line me.txtCsv.Setfocus. But without expected result. Inside the Initialization event code I inserted that line Debug.Print Me.ActiveControl.Name & " - 1", changing 1 in 2, 3 up to 6 in many places, including the last line and all the time the name of the text box in discussion (txtCsv) appears in Immediate Window.
So, the control in discussion is the activate one, but the cursor is not inside it when the form is loaded.
TabStop is set to True. I set the TabIndex to 0.
The control is enabled and not blocked. I have created a new simple form with three text boxes and it works well.
I mean the text box which I want to send the focus, has the focus when the form is loaded, keeping a similar code in its Initialize or Activate events.
I compared all properties of the two forms and all text box controls and they are the same...
When I send the focus from another control on the form, the text box in discussion receives it.
It does not receive the focus (anymore) only when the form is shown, the focus being sent by Initialize or Activate evens.
Events code:
Private Sub UserForm_Activate()
Me.txtCsv.SetFocus
End Sub
Private Sub UserForm_Initialize()
Dim P As Printer, i As Long, NrImp As Long, prDefault As String, strJustEngr As String
Dim Printers() As String, n As Long, s As String, boolFound As Boolean
Dim strEng As String, MEngr As Variant, m As Variant, el As Variant, defSize As String
Dim strDropbox As String
boolOpt = True: boolFound = False
Me.cbPrinters.Clear
If Me.chkNewStyle.Value = True Then boolNewStyle = True
prDefault = Application.Printers.Default.Name
strEng = GetSetting(ECA_K, ECA_set, ECA_Engr, "No settings...")
If strEng <> "No settings..." Then
boolSelectedEngravers = True ' only adding engraver is possible...
MEngr = Split(strEng, "|")
'Incarcare in combo:
Me.cbPrinters.Clear
For Each el In MEngr
m = Split(el, ":")
Me.cbPrinters.AddItem m(0)
If m(0) = prDefault Then
boolFound = True
defSize = m(1)
End If
Next
Me.cbPrinters.Value = Me.cbPrinters.List(0)
With Me.btChoosePrinters
.Caption = "Add an Engraver"
.ControlTipText = "Add another Engraver(must be installed)"
End With
Me.btEliminatePrinters.Enabled = True
Me.lblPrinters.Caption = "Engravers: "
Me.cbPrinters.ControlTipText = "Select Engraver to be used!"
Else
Printers = GetPrinterFullNames()
For n = LBound(Printers) To UBound(Printers)
Me.cbPrinters.AddItem Printers(n)
If Printers(n) = prDefault Then boolFound = True
Next n
boolSelectedEngravers = False
End If
Debug.Print Me.ActiveControl.Name & " - 1"
If boolFound Then
Me.cbPrinters.Value = prDefault
Else
Me.lblStatus.Caption = "The default printer (""" & prDefault & """) is not a laser Engraver..."
End If
If GetSetting(ECA_K, ECA_set, "LowRAM", "No settings...") <> "No settings..." Then
boolLowRAM = CBool(GetSetting(ECA_K, ECA_set, "LowRAM", "No settings..."))
End If
If boolLowRAM = True Then
Me.chkLowRAM.Value = True
Else
Me.chkLowRAM.Value = False
End If
Debug.Print Me.ActiveControl.Name & " - 2"
'Direct engrave setting:
Dim strDirectEngrave As String
strDirectEngrave = GetSetting(ECA_K, ECA_set, ECA_Direct_Engrave, "Nothing")
If strDirectEngrave <> "Nothing" Then
Me.chkDirectEngrave.Value = CBool(strDirectEngrave)
If CBool(strDirectEngrave) = True Then
boolDirectEngrave = True
Else
boolDirectEngrave = False
End If
End If
'_______________________________________
strJustEngr = GetSetting(ECA_K, ECA_set, ECA_Just_Engrave, "Nothing")
If strJustEngr <> "Nothing" Then
'Application.EventsEnabled = False
boolChangeEngr = True
Me.chkJustEngrave.Value = CBool(strJustEngr)
boolChangeEngr = False
'Application.EventsEnabled = True
If CBool(strJustEngr) = True Then
Me.chkDirectEngrave.Enabled = True
boolJustEngrave = True
Me.frLocFoldPath.Enabled = True
Else
Me.frLocFoldPath.Enabled = False
Me.chkDirectEngrave.Enabled = False
End If
End If
Debug.Print Me.ActiveControl.Name & " - 3"
If boolSelectedEngravers Then
Application.EventsEnabled = False
Me.btGo.ForeColor = RGB(45, 105, 7)
Me.txtCsv.BackColor = RGB(153, 255, 51)
Me.btGo.Enabled = False
Me.txtCsv.SetFocus
Application.EventsEnabled = True
End If
strDropbox = GetSetting(ECA_K, ECA_set, ECA_Dropbox, "No value")
If strDropbox <> "No value" Then
If CBool(strDropbox) = True Then
Me.chkDropbox.Value = True
End If
End If
AllRefresh
Me.chkCloseDoc.Value = True
Me.txtCsv.SetFocus
Debug.Print Me.ActiveControl.Name & " - 4"
End Sub
Private Sub AllRefresh()
Application.Optimization = False
Application.EventsEnabled = True
If Documents.Count > 0 Then
ActiveWindow.Refresh
ActiveDocument.PreserveSelection = True
End If
Application.Refresh
End Sub
Is there something else, crossing your mind, to be tested?
In the meantime I did some more tests, respectively:
I created a new project (.GMS file) and I imported the form in discussion.I started commenting all the Initialize event code, except the last two code lines.
It didn't set the focus! Commenting everything, letting only the Activate event code, it worked.
I started to un-comment lines in Initialize event code and I found a line not allowing the focus to be sent to that text box.
Setting the value of the combo: Me.cbPrinters.Value = Me.cbPrinters.List(0), moving it in the Activate event code, before the part pointing to txtCSV, worked well.
Now, I tried to do the same in the original form and it does not work...
The above question has been solved by Disabling followed by Enabling of the text box in discussion, but only doing that in Form Activate event. It did not work in Initialize event...
Private Sub UserForm_Activate()
Me.txtCsv.Disable: Me.txtCsv.Enable
Me.txtCsv.SetFocus
End Sub

Setting Form Property .AllowEdits on Subforms does not seem to work

I am using the following code as event handler for the button cmd_Edit on my main form:
Private Sub cmd_Edit_Click()
If intCanEdit = False Then
If MsgBox("Sollen vorhandene Prozeduren verändert werden ?", vbYesNo, "Frage") = vbNo Then Exit Sub
Me.AllowEdits = True
Me.AllowAdditions = True
Dim sbfrm As Control
For Each sbfrm In Me.Controls
With sbfrm
Select Case .ControlType
Case acSubform
.Form.AllowEdits = True
.Form.AllowAdditions = True
End Select
End With
Next sbfrm
intCanEdit = True
Else
Me.AllowEdits = False
Me.AllowAdditions = False
For Each sbfrm In Me.Controls
With sbfrm
Select Case .ControlType
Case acSubform
**.Form.AllowEdits = False**
.Form.AllowAdditions = False
End Select
End With
Next sbfrm
intCanEdit = False
End If
cmd_Edit.Caption = IIf(intCanEdit, "Click to Save", "Click to Edit")
cmd_Edit.BackColor = IIf(intCanEdit, vbRed, vbGreen)
End Sub
The form loads with intCanEdit set to False. When i click the button once (setting it to true) everything works as expected, when i click it again (setting it to false again) i get an error (Runtime error 2455) with the Debugger sending me to the line i marked with asterisks in the above code.
Does anybody have an idea why i can set the property to True with my code, but get an error when i try to set the same property back to False? :(

Word 2013 VBA IF-THEN-ELSE ... If statement not working

I'm trying to create a form where a user can tick a box and paragraphs of text are displayed
I've used the code below, however when I click out of design mode the text disappears (as expected) but when I click the check box it doesn't reappear
Private Sub CHECKbutane_Click()
If (Bookmarks("TEXT_Butane").Range.Font.Hidden = True) Then
Bookmarks("TEXT_Butane").Range.Font.Hidden = False
Else
Bookmarks("TEXT_Butane").Range.Font.Hidden = True
End If
End Sub
When you work with using the Hidden property to hide/show text, make sure that the display of Hidden text is turned off in the Word UI and the display of all non-printing options must also be turned off. The individual optinos are in File/Options/Display; all non-printing characters can be toggled on/off using the "backwards P" in the Home tab.
Of course, if this is a macro to be used by others, no one wants to have to continually go into File/Options/Display to change these settings. Here's a macro that turns on the individual settings for everything except hidden text if the non-printing characters are being displayed.
The display of non-printing characters if then turned off and the display of hidden text is turned on/off according to the state of the checkbox.
Private Sub CheckBox1_Click()
Dim vw As Word.View
Dim bChecked As Boolean
Dim bkm As Word.Bookmark
'If the user is currently viewing non-printing characters
'make sure these are turned on individually so that
'not displaying Hidden text does not affect these settings.
Set vw = Application.ActiveWindow.View
If vw.ShowAll = True Then
vw.ShowParagraphs = True
vw.ShowObjectAnchors = True
vw.ShowTabs = True
vw.ShowHyphens = True
vw.ShowOptionalBreaks = True
vw.ShowSpaces = True
End If
vw.ShowAll = False
vw.ShowHiddenText = False
bChecked = Me.CheckBox1.Value
Set bkm = ActiveDocument.Bookmarks("TEXT_Butane")
If bChecked Then
bkm.Range.Font.Hidden = False
Else
bkm.Range.Font.Hidden = True
End If
End Sub
If this were a professional application, you'd want to store the individual "Show" settings and re-apply them when this document is no longer the active document. But that's very advanced programming...
I got the following code to work for me.
So you could essentially:
Public Hide As Boolean
Sub CHECKbutane_Click()
Bookmarks("TEXT_Butane").Range.Select
If Hide Then
Hide = False
Call Hide
Else
Hide = True
Call Unhide
End If
End Sub
Sub Unhide()
With Selection.find
.text = ""
.Format = True
.Font.Hidden = True
.Replacement.Font.Hidden = False
.MatchWildcards = False
End With
Do While Selection.find.Execute
Selection.Font.Hidden = False
Selection.MoveRight 1
Loop
End Sub
Sub Hide()
With Selection.find
.text = ""
.Format = True
.Font.Hidden = False
.Replacement.Font.Hidden = True
.MatchWildcards = False
End With
Do While Selection.find.Execute
Selection.Font.Hidden = True
Selection.MoveRight 1
Loop
End Sub

Visual Basic "Do While" loop

I am coding a basic program that allows users to calculate useful data on a shape (2d and 3d) based on information such as radius length, side length, height, etc.
I would like to create a settings MENU that allows users to set how many decimal places they would like the answer to go to, so what I did was
Private Sub btnSettings_Click(sender As Object, e As EventArgs) Handles btnSettings.Click
If btnSettings.Text = "Settings" Then
btnSettings.Text = "Back"
ElseIf btnSettings.Text = "Back" Then
btnSettings.Text = "Settings"
End If
Do While btnSettings.Text = "Back"
'makes IO elements invisible
lblEnter.Visible = False
lblEnter2.Visible = False
lblData1.Visible = False
lblData2.Visible = False
lblData3.Visible = False
lblAnswer1.Visible = False
lblAnswer2.Visible = False
lblAnswer3.Visible = False
txtEnter.Visible = False
txtEnter2.Visible = False
btnClearTxt1.Visible = False
lblEnter3.Visible = False
txtEnter3.Visible = False
chkBox.Visible = False
btnCalculate.Visible = False
btnClear.Visible = False
'Makes shape selection elements invisible
picCircle.Visible = False
picSquare.Visible = False
picTriangle.Visible = False
rdoCircle.Visible = False
rdoSquare.Visible = False
rdoTriangle.Visible = False
btn3D.Visible = False
'Changes texts on necessary elements
lblSelectShape.Text = "Settings"
Loop
End Sub
As you can see, when "btnSettings" shows the text "Settings", the elements on the screen are VISIBLE to the user, however as soon as "btnSettings" changes text to "Back" (indicating the user is inside the settings menu) all the elements on the screen disappear, making room for the elements the settings menu will have. However, while debugging the program whenever I hit the settings button the program crashes.
Any help? Thanks
Use a Select Case for this:
Select Case shapeType
Case shapeType.Circle
'logic for this
'...
End Select

ActiveX List Boxes will not "Size and Move" with their parent cells

I'm new at VBA so sorry in advance if this is a silly question. I have a Worksheet with ActiveX List boxes. The worksheet also has Toggle Switches. The toggle switches are set up to Hide Rows and ActiveX boxes when not depressed and Show Rows and ActiveX boxes when depressed. I'd like to save the file with all of the Toggle switches not depressed so that the user can un-hide only the rows and boxes that they need. Everything works properly until I save the file with all rows hidden. After the save all of the boxes change locations. I've tried setting the boxes to "Move and Size with cell", "Move but don't size with cell", and "Don't more or Size with cell" in the preferences. The same thing happens with all options. Below is my toggle switch code. Is there something in there causing this to happen?
Private Sub ToggleButton1_Click()
If ToggleButton1.Value = True Then
'This area contains the things you want to happen
'when the toggle button is not depressed
Range("101:183").EntireRow.Hidden = False
Sheet1.Range("94:144").EntireRow.Hidden = False
'This hides the listboxes since they can not move and
'size with cells
Sheet11.OLEObjects("ListBox1").Visible = True
Sheet11.OLEObjects("ListBox2").Visible = True
Sheet11.OLEObjects("ListBox3").Visible = True
Sheet11.OLEObjects("ListBox4").Visible = True
Sheet11.OLEObjects("ListBox5").Visible = True
Sheet11.OLEObjects("ListBox6").Visible = True
Sheet11.OLEObjects("ListBox7").Visible = True
Sheet11.OLEObjects("ListBox8").Visible = True
Sheet11.OLEObjects("ListBox9").Visible = True
Sheet11.OLEObjects("ListBox10").Visible = True
Sheet11.OLEObjects("ListBox11").Visible = True
Sheet11.OLEObjects("ListBox12").Visible = True
Sheet11.OLEObjects("ListBox13").Visible = True
Sheet11.OLEObjects("ListBox14").Visible = True
Sheet11.OLEObjects("ListBox15").Visible = True
Sheet11.OLEObjects("ListBox16").Visible = True
Sheet11.OLEObjects("ListBox17").Visible = True
Sheet11.OLEObjects("ListBox18").Visible = True
Else
'This area contains the things you want to happen
'when the toggle button is depressed
Range("101:183").EntireRow.Hidden = True
Sheet1.Range("94:144").EntireRow.Hidden = True
Sheet11.OLEObjects("ListBox1").Visible = False
Sheet11.OLEObjects("ListBox2").Visible = False
Sheet11.OLEObjects("ListBox3").Visible = False
Sheet11.OLEObjects("ListBox4").Visible = False
Sheet11.OLEObjects("ListBox5").Visible = False
Sheet11.OLEObjects("ListBox6").Visible = False
Sheet11.OLEObjects("ListBox7").Visible = False
Sheet11.OLEObjects("ListBox8").Visible = False
Sheet11.OLEObjects("ListBox9").Visible = False
Sheet11.OLEObjects("ListBox10").Visible = False
Sheet11.OLEObjects("ListBox11").Visible = False
Sheet11.OLEObjects("ListBox12").Visible = False
Sheet11.OLEObjects("ListBox13").Visible = False
Sheet11.OLEObjects("ListBox14").Visible = False
Sheet11.OLEObjects("ListBox15").Visible = False
Sheet11.OLEObjects("ListBox16").Visible = False
Sheet11.OLEObjects("ListBox17").Visible = False
Sheet11.OLEObjects("ListBox18").Visible = False
End If
End Sub
I know this isn't the answer to your question (I haven't even looked at it yet), but i just felt like giving you this code, this is the exact code you provided and will function the same way, just looks a tiny bit clearer (actually as it also removes the if statement it prolly even performs at like 1/1000000 of a millisecond faster also =D)
Private Sub ToggleButton1_Click()
Dim boolToggleValue As Boolean
Dim i As Integer
boolToggleValue = ToggleButton1.Value
'This area contains the things you want to happen
'when the toggle button is not depressed
Range("101:183").EntireRow.Hidden = Not boolToggleValue
Sheet1.Range("94:144").EntireRow.Hidden = Not boolToggleValue
'This hides the listboxes since they can not move and
'size with cells
With Sheet11
For i = 1 To 18
.OLEObjects("ListBox" & i).Visible = boolToggleValue
Next i
End With
End Sub