PDA

View Full Version : Modify a table in the header with VBA in Word



frio27
03-28-2019, 06:06 AM
Hi all.

I'm trying to programme a macro in word to change the header. In the header of the document I have a table and I need to change some things there (the background color, change the text of a cell...).

For example I need to apply this comand to the header:

Cell(1, 2).Shading.BackgroundPatternColor = 11382784


My problem its that every macro I programme dont affect the header, just the document.

I will need to change the header of the first page and the header of the rest of the pages.


Thanks!

gmayor
03-28-2019, 06:22 AM
You need to address the headers directly e.g. the following will address the headers and any tables they contain. It treats each section the same, but you can add code to address each section differently as required. I have only added code for the primary header. You can make your own choices for the other headers.


Sub Macro1()
Dim oSection As Section
Dim oHeader As HeaderFooter
Dim oTable As Table
For Each oSection In ActiveDocument.Sections
For Each oHeader In oSection.Headers
If oHeader.Exists Then
Select Case oHeader.Index
Case Is = wdHeaderFooterPrimary
If oHeader.Range.Tables.Count > 0 Then
Set oTable = oHeader.Range.Tables(1)
oTable.Cell(1, 2).Shading.BackgroundPatternColor = 11382784
End If
Case Is = wdHeaderFooterFirstPage
If oHeader.Range.Tables.Count > 0 Then
Set oTable = oHeader.Range.Tables(1)
'do stuff with otable
End If
Case Is = wdHeaderFooterEvenPages
If oHeader.Range.Tables.Count > 0 Then
Set oTable = oHeader.Range.Tables(1)
'do stuff with otable
End If
End Select
End If
Next oHeader
Next oSection
lbl_Exit:
Set oSection = Nothing
Set oHeader = Nothing
Set oTable = Nothing
Exit Sub
End Sub

frio27
03-28-2019, 07:16 AM
You need to address the headers directly e.g. the following will address the headers and any tables they contain. It treats each section the same, but you can add code to address each section differently as required. I have only added code for the primary header. You can make your own choices for the other headers.


Sub Macro1()
Dim oSection As Section
Dim oHeader As HeaderFooter
Dim oTable As Table
For Each oSection In ActiveDocument.Sections
For Each oHeader In oSection.Headers
If oHeader.Exists Then
Select Case oHeader.Index
Case Is = wdHeaderFooterPrimary
If oHeader.Range.Tables.Count > 0 Then
Set oTable = oHeader.Range.Tables(1)
oTable.Cell(1, 2).Shading.BackgroundPatternColor = 11382784
End If
Case Is = wdHeaderFooterFirstPage
If oHeader.Range.Tables.Count > 0 Then
Set oTable = oHeader.Range.Tables(1)
'do stuff with otable
End If
Case Is = wdHeaderFooterEvenPages
If oHeader.Range.Tables.Count > 0 Then
Set oTable = oHeader.Range.Tables(1)
'do stuff with otable
End If
End Select
End If
Next oHeader
Next oSection
lbl_Exit:
Set oSection = Nothing
Set oHeader = Nothing
Set oTable = Nothing
Exit Sub
End Sub


Thank you for your answere

I have try your programme and it gives me the error 5941 in this line:
oTable.Cell(1, 2).Shading.BackgroundPatternColor = 11382784

I cant find where the problem is.

gmayor
03-29-2019, 09:32 PM
I can't immediately spot where the error might be. The posted code works fine here in the PC version of Office 2007 through to 2016 with a table that has two or more cells in row 1 and the tables are not nested.
You could try instead

oTable.Range.Cells(2).Shading.BackgroundPatternColor = 11382784
but I don't see why it should be necessary.
If the table is not a simple table, post a sample of a document with the header in question.

frio27
04-01-2019, 12:00 AM
Thank you for the answere.
It still doesnt work.

I attach the example word. What I want is the macro color de coloured cell.


23971

gmayor
04-01-2019, 12:29 AM
OK, you cannot use that method when the table has merged cells. The following however will work with that table whichever header it is in (It is in the first page header in your sample)
If you want to select which header, then you will need to replace the line


oTable.Cell(1, 2).Shading.BackgroundPatternColor = 11382784
with

oTable.Range.Cells(3).Shading.BackgroundPatternColor = 11382784


Sub Macro1()
Dim oSection As Section
Dim oHeader As HeaderFooter
Dim oTable As Table
For Each oSection In ActiveDocument.Sections
For Each oHeader In oSection.Headers
If oHeader.Exists Then
If oHeader.Range.Tables.Count > 0 Then
Set oTable = oHeader.Range.Tables(1)
If oHeader.Range.Tables.Count > 0 Then
Set oTable = oHeader.Range.Tables(1)
oTable.Range.Cells(3).Shading.BackgroundPatternColor = 11382784
End If
End If
End If
Next oHeader
Next oSection
lbl_Exit:
Set oSection = Nothing
Set oHeader = Nothing
Set oTable = Nothing
Exit Sub
End Sub

frio27
04-01-2019, 07:11 AM
Thank you my friend.

With your help I finally have finnish my macro and it works perfectly.

If you ever come to Pamplona i will invite you to have dinner.


Thanks!!

frio27
04-01-2019, 07:11 AM
OK, you cannot use that method when the table has merged cells. The following however will work with that table whichever header it is in (It is in the first page header in your sample)
If you want to select which header, then you will need to replace the line


oTable.Cell(1, 2).Shading.BackgroundPatternColor = 11382784
with

oTable.Range.Cells(3).Shading.BackgroundPatternColor = 11382784


Sub Macro1()
Dim oSection As Section
Dim oHeader As HeaderFooter
Dim oTable As Table
For Each oSection In ActiveDocument.Sections
For Each oHeader In oSection.Headers
If oHeader.Exists Then
If oHeader.Range.Tables.Count > 0 Then
Set oTable = oHeader.Range.Tables(1)
If oHeader.Range.Tables.Count > 0 Then
Set oTable = oHeader.Range.Tables(1)
oTable.Range.Cells(3).Shading.BackgroundPatternColor = 11382784
End If
End If
End If
Next oHeader
Next oSection
lbl_Exit:
Set oSection = Nothing
Set oHeader = Nothing
Set oTable = Nothing
Exit Sub
End Sub

Thank you my friend.

With your help I finally have finnish my macro and it works perfectly.

If you ever come to Pamplona i will invite you to have dinner.


Thanks!!