Toggle the "Different Odd & Even Pages" checkbox in Word - vba

I'm setting up a set of "boilerplate" Word documents.
All the documents have different Footers for odd and even pages.
The Footers are left- and right-hand biased so that double sided printing will always show important information at the thumb edge of the page.
The document printing default is double sided, so the Header/Footer "Different Odd & Even Pages" box is checked.
However there will be times when a user will want to print single sided.
The box will need to be unchecked.
Each document contains a UserForm to manage features (import external values for Document Variables etc.)
I would like to control the "Different Footer Odds & Even Pages" feature from the UserForm via a pair of Option Buttons.
I've searched multiple site to no avail.
The closest that I gotten is the sample code from the OddAndEvenPagesHeaderFooter Help topic:
Set myDoc = Documents("Document1")
myDoc.PageSetup.OddAndEvenPagesHeaderFooter = True
With myDoc.Sections(1)
.Headers(wdHeaderFooterPrimary).Range _
.InsertAfter "Odd Header"
.Headers(wdHeaderFooterEvenPages).Range _
.InsertAfter "Even Header"
End With
However I don't want to create footers. I just want to toggle the PageSetup.OddAndEvenPagesHeaderFooter between True and False
I have tried this "stripped down version:
'Set OddFootersOnly
With ActiveDocument.PageSetup
.OddAndEvenPagesHeaderFooter = False
End With
I get the following Error Message
Run-time error: '4608': Value out of range

Related

I'm trying to create a header that begins on the second page, inserts an em dash, the page number and another em dash, centered in vba

document.Application.ActiveDocument.Sections(1).Headers(WdHeaderFooterIndex.wdHeaderFooterPrimary) _
.PageNumbers.StartingNumber = 2
For Each section As Word.Section In document.Application.ActiveDocument.Sections
Dim headerRange As Word.Range = section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
headerRange.Text = " — "
headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage)
headerRange.Text = headerRange.Text & " — "
Next
The issue that I am having is I cannot get the dashes on either side of the page number. It will always place both of them before or after the page number.
I have tried concatenating, I have tried various placements of the dashes. I have tried the headerRange.Collapse with no success.
'document.Application.ActiveDocument.Sections(1).Headers(WdHeaderFooterIndex.wdHeaderFooterPrimary) _
' .PageNumbers.StartingNumber = 2
'For Each section As Word.Section In document.Application.ActiveDocument.Sections
' Dim headerRange As Word.Range = section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
' headerRange.Text = "—"
' headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
' headerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
' headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage)
' headerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
' headerRange.Text = "-"
'Next
To a man with a hammer, everything looks like a nail.
You are doing this the hard way, in my opinion.
I look at using a macro for this as reinventing the wheel - Instead, use Templates or Building Blocks
Perhaps I am lazy, but setting things up in vba and formatting it is hard, at least to me.
You could create a template with your header and use that as the basis for new documents. Here is my page on Templates in Microsoft Word.
You could save this as a Building Block if you want to use it in existing documents. Remember that using a page number building Block for top of page replaces any existing header, for bottom of page replaces any existing footer.
Here is a temporary link to a template containing three page number building blocks with those for top and bottom of the page being centered. All have M-dashes on either side. The font and size will be determined by the Header or Footer styles in the document. The template contains instructions for using it.
If you want to use vba to insert these building blocks, there are examples shown on my web page with specific instructions on placement of the macros and building blocks. Here is my answer here about using vba to insert a Table of Contents a Building Block.
You could also have a macro to create a new document based on your template.
No matter how you decide to do this, keep in mind that each Word section has three headers, whether or not they are seen. In multi-section documents, these may, or may not, be linked to like headers in previous sections.
Here is my recap of Header and Footer Settings.

Need a VBA code for autotext from dropdown menu

I'm writing a code to select a specific autotext from a dropdown list. I've not written code since the late 1970s the old Basic A...I've forgotten most of it.
The dropdown list is called HVAC
the text to insert is in an autotext file called "Split Systems" or whichever text associated with the dropdown items
The error message is "5941 the requested member of the collection does not exist"
I've tried using the value of 1,2,3,or 4 in the if statement line instead of the "Split...", it doesn't work either
Where is issue or how should I code it.
MY code follows:
Sub one()
If ActiveDocument.Formfields(hvac).DropDown.Value = "Split Systems" Then GoTo 10 Else GoTo 20
10
ActiveDocument.Content.Select
Selection.Collapse Direction:=wdCollapseEnd
ActiveDocument.AttachedTemplate.AutoTextEntries("Split Systems").Insert _
Where:=Selection.Range, RichText:=True
GoTo 100
20
If ActiveDocument.Formfields(hvac).DropDown.Value = "Packaged Systems" Then GoTo 25 Else GoTo 30
25
ActiveDocument.Content.Select
Selection.Collapse Direction:=wdCollapseEnd
ActiveDocument.AttachedTemplate.AutoTextEntries("Packaged System").Insert _
Where:=Selection.Range, RichText:=True
GOto 100
30
If ActiveDocument.Formfields(hvac).DropDown.Value = Central Heating System" Then GoTo 35 Else GoTo 40
35
ActiveDocument.Content.Select
Selection.Collapse Direction:=wdCollapseEnd
ActiveDocument.AttachedTemplate.AutoTextEntries("Central Heating System").Insert _
Where:=Selection.Range, RichText:=True
GoTo 100
40
If ActiveDocument.Formfields(hvac).DropDown.Value = "PTACs" Then GoTo 45
45
ActiveDocument.Content.Select
Selection.Collapse Direction:=wdCollapseEnd
ActiveDocument.AttachedTemplate.AutoTextEntries("Central Heating System").Insert _
Where:=Selection.Range, RichText:=True
100
End Sub
Tried everything I know
Word now has Building Block Gallery Content Controls that eliminate the need to add code to dropdowns. Choose the Developer tab, then choose Building Block Gallery Content Control in the Controls group. Use the Properties button in the Controls group to point the control at AutoText. The only significant drawback is that this control doesn't currently work in Word for Mac.
Why are you trying to reinvent the wheel?
Look at AutoTextList and Building Blocks Gallery Content Control
These two features built into Word create drop downs of AutoText or other Building Blocks. What are you trying to do that cannot be accomplished using one of them?
Here is my writing on the Building Blocks Gallery Content Control.
Here is a link to a sample template with both.
The Content Control gives a very nice drop-down gallery.
Here is a link to vba to insert AutoText/Building Blocks. (It varies depending on the location of the AutoText.)
Note: all links are to my writing.
Use Field Coding rather than vba for this
Since you want to select something in your list and have the AutoText appear in a different location in the document, this could be done with either an AutoText field or with an IF Field.
The AutoText Field would only be useful if what is in your dropdown is the exact name of an AutoText entry's name. This is unlikely.
However, the AutoText Field could be combined in an IF Field that tests the choice in the dropdown.
Here is an example where the choice in the dropdown is "Choice 1," the bookmark name of the Dropdown is "Location" and the corresponding AutoText entry is named "TextPart123."
The properties in your dropdown must include Calculate on Exit checked.
{ IF { Location } = "Choice 1" "{ AutoText TextPart123 }" }
For the dropdown to work, that part of the document must be protected for filling in forms.
Here is a temporary link to a sample document with a dropdown and fields like the one above. https://www.dropbox.com/s/wbfnxoji0uvoikd/deleteme%20dropdown%20for%20conditional%20autotext.docx?dl=0
The first two choices trigger different building blocks. The third choice does not trigger anything.
To trigger the change, you need to exit from the dropdown. Typically this is done using the Tab key.
Note that because there are currently no AutoText entries built into Word, the AutoText field in the sample calls a couple of Watermarks that are built in. The field works with any Building Block, including AutoText.
If you absolutely need to use vba to insert AutoText rather than use the AutoText field, see my page on Boilerplate in Word, specifically on using vba to insert building blocks. As you've discovered, with the entry of Building Blocks in 2007 the code to insert became finicky.

Is there a feature "keep with next" for header titles in RST?

While generating pdf from a set of rst files, I sometimes get an awkward situation that the title of a section and the first paragraph of that section text are on different pages (the title is on the bottom of a page, and the text continues on the next page).
Is there any way to specify "keep with next paragraph" for the section title, like it can be done in, for example, Word?

Different PPT templates reacting different on the same macros

I'm new here, so I might have not seen a possibility to upload my problem files, which would make it easier to describe the problem.
Edit: The files a here: https://drive.google.com/file/d/0B--IbmtX58h8TnVrdlRyUXZ5a2dEOVJBQkplVjFuVEVMVXhJ/view?usp=docslist_api
and: https://drive.google.com/file/d/0B--IbmtX58h8TFR6d3FkWlZpSGFVUGF5bHVhRTR5ZTlnbXAw/view?usp=docslist_api
(Thank you for the idea, Steve)
What it is about:
I have two documents with different master templates reacting completely different on the same set of macros and I have no idea how and why this can happen and how to repair or avoid it.
Two of the macros just create objects - one is a single textbox, the other one a group of a rectangle and a textbox . The first mentioned appears on the position defined in the code in one of the templates, but a bit below it in the other one. Even more strange is the behavior of the group. The rectangle appears on the correct position in both of the templates, the textbox only in one of it.
Next is a macro for increasing the paragraphing between text lines by 3 pt. It works fine in one template, but in the other template it increases the spacing by 43.2 pt!
Macro number four is made to set back the paragraphing space after back to 0. This one works fine in both templates.
Funny enough, the mistakes appear in opposite to each other. The single textbox and the group produce their error in the template, where the spacing tool works fine, and the spacing tool does strange things in the template where the single textbox and the group work well.
Any idea will be appreciated!
Thanks,
RG
I work with PowerPoint 2010.
Your footnote is getting misplaced because the default text settings in one presentation are different from those in the other; in this case the auto fit setting.
' in this section of your FOOTNOTE routine:
With .TextFrame
' Add this next line and it will work as expected
.AutoSize = ppAutoSizeNone
.TextRange.Text = "Note: " & vbCrLf & "Source: "
.VerticalAnchor = msoAnchorBottom
Likewise, in your SectionMarker subroutine:
With .TextFrame
' add this
.AutoSize = ppAutoSizeNone
' then the rest of your code
Then it all works as you'd expect. Or at least, it works the same with both templates.

Word VBA: .Hide function doesn't hide?

I took an old MS Word document to adapt it with a new layout.
I finished last week and everything was working fine, the main macro has to hide or display some text.
For this, a zone of text is "bookmarked", and then we get this bookmark and set its font to hidden:
ActiveDocument.Bookmarks("MyBookMarkname").Range.Font.Hidden = True 'Or False
It's how it was done on the old document, and I had only to do the same on the new document(recreate those bookmarks).
But today, when trying again to make this action, the text isn't hidding anymore! When it is supposed to be hidden, the text is like underlined by a small blue line(the same line you have when an word is not spellt correctly, but in blue).
I searched online, I found several things, but none of them worked:
Private Sub HideHiddenText()
For Each myWindow In Windows
myWindow.View.ShowHiddenText = False
Next myWindow
End Sub
I've no "revision mode" enabled either.
What could be wrong?
I believe the wavy blue line that Word is displaying is being triggered by the hidden text because Word uses the blue line to mark formatting inconsistencies. To get rid of the line in Office 2007/2010 go to
Office Orb Menu (2007) or File Menu (2010)|Options|Advanced
and uncheck Mark formatting inconsistencies
The wavy blue line, however, has nothing to do with your hidden text being displayed. I believe this is happening because the "Show/hide formatting marks" function is turned on. To make sure your hidden text is kept hidden by vba, you will need the following:
With ActiveDocument
.ActiveWindow.View.ShowAll = False 'Hide all formatting marks
.ActiveWindow.View.ShowHiddenText = False 'Do not display hidden text
.Application.Options.PrintHiddenText = False 'Do not print hidden text
End With
It is worth noting that an experienced Word user can always choose to display hidden text via Word's user interface and that if this is to be avoided, a great deal of additional work would need to be invested to disable the native Word functions that can be used to display hidden text (if that is even possible).