Word- VBA- How To Automatically Run Code Before Printing? - vba

Document has shading wdColorGray25 by default. I want to be able to remove all shading before printing (or saving as a PDF) but this doesn't seem to work as simple as it would in Excel (the code works fine by itself in a standard module).
Private Sub appWord_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
With ActiveDocument.Styles("example").Font
With .Shading
.BackgroundPatternColor = wdColorAutomatic
End With
End With
End Sub
In other words, if I hit Ctrl+P, the document will remove the shading before printing and then apply it back after printing. Would also like the change to be reflected in the print preview as well.
If I select the FilePrint command and hit Create it generates the following code (not Application.PrintOut):
Sub FilePrint()
'
' FilePrint Macro
' Prints the active document
'
Dialogs(wdDialogFilePrint).Show
End Sub
If I modify the code to:
Sub PrintOut()
' PrintOut runs when you use Ctrl + P
With ActiveDocument.Styles("example").Font
With .Shading
.BackgroundPatternColor = wdColorAutomatic
End With
End With
Application.PrintOut
With ActiveDocument.Styles("example").Font
With .Shading
.BackgroundPatternColor = wdColorGray25
End With
End With
End Sub
And then hit Ctrl+P, you can see from the below screenshot below that the text shading still appears.
However, if I change the name of the sub like below, it will print without the shading but the print preview pane does not appear (it just prints directly without any prompts in between).
Sub PrintPreviewandPrint()
' PrintOut runs when you use Ctrl + P
With ActiveDocument.Styles("example").Font
With .Shading
.BackgroundPatternColor = wdColorAutomatic
End With
End With
Application.PrintOut
With ActiveDocument.Styles("example").Font
With .Shading
.BackgroundPatternColor = wdColorGray25
End With
End With
End Sub

In Word, you can capture the built-in commands to modify them. To see how this works, try these steps:
In Word, choose Developer>Macros.
Change the Macros in dropdown to Word commands.
Find and select FilePrint.
Change the Macros in dropdown back to Normal.dotm.
Click on the Create button. Word creates a new macro titled with the command name.
Sub PrintOut()
' PrintOut runs when you use Ctrl + P
With ActiveDocument.Styles("example").Font
With .Shading
.BackgroundPatternColor = wdColorAutomatic
End With
End With
Application.PrintOut
With ActiveDocument.Styles("example").Font
With .Shading
.BackgroundPatternColor = wdColorGray25
End With
End With
End Sub
Sub FilePrint()
'
' FilePrint Macro
' Prints the active document
'
With ActiveDocument.Styles("example").Font
With .Shading
.BackgroundPatternColor = wdColorAutomatic
End With
End With
Dialogs(wdDialogFilePrint).Show
With ActiveDocument.Styles("example").Font
With .Shading
.BackgroundPatternColor = wdColorGray25
End With
End With
End Sub
Now add your code to modify the style before and after the Dialogs line, removing the shading before and adding it after.

Related

In Word 2010 use VBA to make first document to fit page

When Word 2010 starts I would to automatically execute the VBA code
ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitFullPage
to make the document fit the page.
Many thanks for a tip
You can put your command(s) in the AutoExec macro.
Sub AutoExec()
ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitFullPage
End Sub
You could add a macro like the following to the 'ThisDocument' code module of a global template (e.g. Normal.dotm):
Private Sub Document_Open()
With ActiveWindow
'Reduce flickering while changing settings
.Visible = False
'Switch to a single view pane
.View.SplitSpecial = wdPaneNone
.View.Type = wdPrintView
.ActivePane.View.Zoom.PageFit = wdPageFitFullPage
'Restore the window now that we're finished
.Visible = True
End With
End Sub

Word Macro to Deselect a content control after macro

My current project is making a list for a coworker and since I like to over-complicate everything, I'm running macros for him to check off as he completes work. The description of work will change from a header style to normal when he's completed it, then update the ToC. All of this is working, but I have trouble with the content control staying selected sometimes. I can usually check and uncheck it once or twice with no problem, but eventually the cursor doesn't move out of the check box for some reason so subsequent clicks don't fire the OnEnter.
Private Sub Document_ContentControlOnEnter(ByVal CCtrl As ContentControl)
With CCtrl
If .Type = wdContentControlCheckBox Then
If .Checked = True Then
.Range.Paragraphs.First.Style = wdStyleNormal
ActiveDocument.TablesOfContents(1).Update
Selection.Collapse direction:=wdCollapseEnd
Else
.Range.Paragraphs.First.Style = wdStyleHeading2
ActiveDocument.TablesOfContents(1).Update
Selection.MoveRight 1
End If
End If
End With
End Sub
Is there a way to force word to deselect the content control and move the cursor somewhere on the same line?
I tried Selection.MoveDown 1, Selection.Collapse direction:=wdCollapseEnd, and also Selection.MoveEnd but none work.
You can leverage the fact that, through the Content Control's Range the objects which contain it can be accessed. For example, you can "drill up" to the paragraph in which the content control is located:
CCtrl.Range.Paragraphs(1).Range.Characters.Last.Select
This could also be any character in the paragraph. The following (in my test) puts the selection immediately after the content control:
CCtrl.Range.Paragraphs(1).Range.Characters(4).Select
Incorporated into your code:
Private Sub Document_ContentControlOnEnter(ByVal CCtrl As ContentControl)
With CCtrl
If .Type = wdContentControlCheckBox Then
If .Checked = True Then
.Range.Paragraphs.First.Style = wdStyleNormal
ActiveDocument.TablesOfContents(1).Update
Selection.Collapse direction:=wdCollapseEnd
Else
.Range.Paragraphs.First.Style = wdStyleHeading2
ActiveDocument.TablesOfContents(1).Update
Selection.MoveRight 1
End If
'Select the last character in the paragraph (the paragraph mark)
CCtrl.Range.Paragraphs(1).Range.Characters.Last.Select
'Remove the selection, so the cursor blinks at the end of the paragraph
Selection.Collapse
End If
End With
End Sub

Word VBA macro button only works once

I am trying to create a button that copies the contents of an entire page, and pastes it to a new page within the same document. When I first formatted this as a MacroButton (CTRL + F9) it would work multiple times. Now I have tried it using a command button under Legacy forms (Developer tab) as a click event, and it will only work once. I am not sure why this is occuring. Here is my code:
Private Sub AddPage3_Click()
Const wdPageBreak = 7
ActiveDocument.Bookmarks("\page").Range.Copy
Selection.InsertBreak (wdPageBreak)
Selection.Paste
Selection.TypeBackspace
Selection.TypeBackspace
End Sub
When I press the button, it will create another page in the document with the contents of the active page (the page the button is located on), but it will not work a second time. If you have any suggestions please let me know :)!
After copy/paste, you can rename your commandbutton as follows and the commandbutton keeps running each time:
Private Sub AddPage3_Click()
Dim shp As InlineShape
Const wdPageBreak = 7
ActiveDocument.Bookmarks("\page").Range.Copy
Selection.InsertBreak (wdPageBreak)
Selection.Paste
Selection.TypeBackspace
Selection.TypeBackspace
For Each shp In ActiveDocument.InlineShapes
On Error Resume Next
If shp.OLEFormat.ClassType = "Forms.CommandButton.1" Then
If shp.OLEFormat.Object.Name = "AddPage31" Then
shp.OLEFormat.Object.Name = "AddPage3"
End If
End If
Next
End Sub

Managing Find and Replace dialog box in MS Word 2010 using Macro

The below code display Find and Replace dialog box. But the problem is that the Find tab and the Go To tabs are disabled.
How to keep all the three tabs enabled?
Is it possible to set the Find and Replace combobox Editable property to False?
Public Sub EditReplace()
On Error Resume Next
With Dialogs(wdDialogEditReplace)
Selection.HomeKey Unit:=wdStory
.Find = "[ ^13^t]{1,};"
.Replace = ";"
.Show
End With
End Sub
Try (with the assumption that "Ctrl H" is still a shortcut for "Replace" in the menu):
Public Sub AnotherName()
On Error Resume Next
Selection.HomeKey Unit:=wdStory
Selection.Find.Text = "[ ^13^t]{1,};"
Selection.Find.Replacement.Text = ";"
SendKeys "+^%H", True
End Sub
The name should rather not be "EditReplace", or you will direct here all replace operations in the future.

VBA fails on first try error 5479

It seems like this guy has had the same problem as me, but the vbModeless doesn't seem to do the trick.
I'm creating a simple add-in, and I want VBA to open a word file, copy the entire content and paste it in the original document.
It works perfectly when executed from the VBA editor or even after the first run where it debugs, but opening a Word instance for the first time and trying to execute the code from the add-in seems to be a problem.
The code is as follows:
Sub insertFigureFrame(control As IRibbonControl)
StandardFrames.StartUpPosition = 0
StandardFrames.Top = Application.Top + (Application.Height - StandardFrames.Height) * 0.5
StandardFrames.Left = Application.Left + (Application.Width - StandardFrames.Width) * 0.5
StandardFrames.Show
End Sub
Sub Standard()
Dim OriginalDocument As String
Dim SaveChanges As Boolean
Dim doc As Document
On Error GoTo err:
Application.DisplayAlerts = wdAlertsNone
Application.ScreenUpdating = False
OriginalDocument = ActiveDocument.Path
Documents.Open MyTemplate
Selection.WholeStory
Selection.Copy
Documents("MyTemplate").Close (SaveChanges = False)
Documents.Open (OriginalDocument)
Selection.PasteAndFormat wdPasteDefault
Exit Sub
err:
Call errHandling
End Sub
The StandardFrames Userform calls the sub Standard.
Any idea what might cause the problem?
EDIT:
I'm using Word 2007 and Windows XP.
Try hidding your userform before you open a document:
Sub Standard()
StandardFrames.Hide
'The rest of your code
End Sub