Set background color of headers and footers - vba

I have been able to change the background color of Word tables in the body of the document with: oSourceTable.Shading.BackgroundPatternColor = wdRed. However, the same code does not work for header or footer Word tables. I have also tried setting the Section back color as well, but to no avail. The code runs, but the Headers and Footers are always displayed with white backgrounds.
I have done mostly Excel VBA, and only a little bit of Word VBA so maybe I am missing something obvious here. Thanks in advance for any ideas and/or suggestions.
As requested, here is the code I am using. vTableBackColors is just an array of colors. This code is for the body tables and is working perfectly.
For Each oWordTable In oWordDoc.Tables
lIndex = lIndex + 1
oWordTable.Shading.BackgroundPatternColor = vTableBackColors(lIndex)
Next
I tried to do the same thing for the header and footer tables, but it does not work. I tried using the Header/Footer tables as below.
For Each oWordSection In oWordDoc.Sections
For Each oWordTable In oWordSection.Headers.Item(wdHeaderFooterPrimary).Range.Tables
' I selected this one to see if it would make a difference.
oWordTable.Select
oWordTable.Shading.BackgroundPatternColor = m_HeaderBackColor
Next
For Each oWordTable In oWordSection.Footers.Item(wdHeaderFooterPrimary).Range.Tables
oWordTable.Shading.BackgroundPatternColor = m_FooterBackColor
Next
Next
I also tried using the Section Headers / Footers directly. Before and after checking shows that the BackgroundPatternColor has changed as desired, but it is not displayed.
oWordSection.Headers.Item(wdHeaderFooterPrimary).Range.Shading.BackgroundPatternColor = m_HeaderBackColor
Here is a screenshot showing the colorized body tables and the unchanged header.
Is it possible that, unlike the Body table colors, the Header / Footer colors are never displayed as such by the Word designer, but are only true at runtime?
Thanks for any additional information.

«I have been able to change the background color of Word tables in the body of the document with: oSourceTable.Shading.BackgroundPatternColor = red.» That would not work unless you have defined 'red' as an RGB value.
As for the header/footer issue, this works for me:
For Each oWordSection In oWordDoc.Sections
For Each oWordTable In oWordSection.Headers.Item(wdHeaderFooterPrimary).Range.Tables
oWordTable.Shading.BackgroundPatternColorIndex = wdRed
Next
For Each oWordTable In oWordSection.Footers.Item(wdHeaderFooterPrimary).Range.Tables
oWordTable.Shading.BackgroundPatternColorIndex = wdRed
Next
Next
Perhaps your underlying problem is in the value you have assigned to 'm_HeaderBackColor' and 'm_FooterBackColor' - which your posted code doesn't show. It's also possible your code is addressing the wrong headers/footers (i.e. maybe it's not the primary ones you need to address).

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.

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.

How to choose a different header image in MS Word 2013 using a macro/button

I'd like to create a Word stationary template with ability to cycle through different colored logos in its header. My company uses a logo in five different colors and I would like to create a single template with a button that would allow me to cycle through the different colored logos every time I create a new document from this template. Can this be done, perhaps with a little VBA?
Edit:
After working with an answer from Olle Sjögren I've come up with the following working script:
Option Explicit
Public imgCounter As Integer
Sub cycle_logos()
Dim I As Variant
Dim logoColors(4) As String
logoColors(0) = "logo_magenta.png"
logoColors(1) = "logo_teal.png"
logoColors(2) = "logo_orange.png"
logoColors(3) = "logo_red.png"
logoColors(4) = "logo_grayscale.png"
For Each I In logoColors
ActiveDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange.Item(I).Visible = msoFalse
Next I
imgCounter = imgCounter + 1
If imgCounter = 5 Then imgCounter = 0
ActiveDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange.Item(logoColors(imgCounter)).Visible = msoTrue
End Sub
It is worth mentioning how I came up with the image names, since I didn't find a way to do this from inside Word. I renamed the template extension to zip and unzipped it. In the extracted files I opened word\header2.xml (this may vary, depending on the position in the document) and edited the nodes containing the names of pictures, i.e. <wp:docPr/>, e.g.:
<wp:docPr name="Picture 1" id="1"/>
became:
<wp:docPr name="logo_magenta.png" id="1"/>
etc. I then replaced the XML file in the ZIP with my edited version and changed the extension back to dotm.
As mentioned, there are several ways to do this. I would suggest storing the images outside of the template, otherwise all documents based on the templates would include all logo images, even if they are not shown, making the documents bigger than they need to be. That approach makes installing the template a bit harder, since you would have to copy the images to the clients as well as the template file.
To answer your question regarding addressing the images in the header - you can address then through the correct story range object. In my example I assume that they are in the primary header. To tell the different images apart, you can use the Name property or the index in the Item collection:
ThisDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange.Item("Picture 1").Visible = msoTrue
ThisDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange.Item(1).Visible = msoFalse

Incomplete border around range in excel

I'm using Vb.Net to automate an excel spreadsheet generation. I'm drawing a border around a range, but for some reason the text in the first columns hides the border. Is there some way of sending this text to "background", like a z-index in web, so that the border displays correctly?
This is the relevant line of code:
range = sheet.Range("B" + (sectionStart).ToString, "M" + (currentLine).ToString)
range.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin)
I would say your best bet is to wrap the text in column A, we get this problem a lot here at work and it usually solves the issue.
Try adding:
sheet.Range(“A:A″).IsWrapText = True
To your code, hope this helps!

Word 2003 vba cannot change bookmark font

The following code does not work
objDoc.Bookmarks("SomeBookMark").Range.Font.Bold = True
objDoc.Bookmarks("SomeBookMark").Range.Text = GetSomeText()
objDoc.Bookmarks("SomeBookMark").Range.Font.Bold = True
When I run the code the text that is retrieved by GetSomeText() still has the default font.
Why don't you just style the bookmarks?
I searched for this in the past and also didn't find a proper solution back then.
However, since a bookmark represents a fixed field in a text template, usually the format shouldn't change so easily.
I'm sorry to put this in the reply field, but I can't find the "comment" button :-s