PDA

View Full Version : Select range of cells in Footer/Header



Josie67
02-03-2016, 02:42 PM
Dear all,

I'm trying to programm a template for a word document.
I would like to select a range of cell in a HEADER table.
My code is working if I try this on a table in the core of the document but not in the header (see below).
In the core, I use the following code : ActiveDocument.Range(HdrTable.Cell(1, 1).Range.Start, HdrTable.Cell(1, 2).Range.End).Select

Public Sub Test()
Dim HdrRange As Range
Dim HdrTable As Table
Set HdrRange = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
Set HdrTable = ActiveDocument.Tables.Add(HdrRange, 3, 3)
With HdrTable
.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
End With
ActiveDocument.sections(1).headers(1).Range(Tables(1).Cell(1, 1).Range.Start, Tables(1).Cell(1, 2).Range.End).Select
End Sub

Please give me your help!
Thanks in forward for your return.

Regards,

gmayor
02-03-2016, 10:13 PM
You have already set the table as a variable so you can address that variable. The following should do what you require:

Option Explicit
Public Sub Test()
Dim HdrRange As Range
Dim HdrTable As Table
Dim oCells As Range
Set HdrRange = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
Set HdrTable = ActiveDocument.Tables.Add(HdrRange, 3, 3)
With HdrTable
.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
End With
Set oCells = HdrTable.Range
oCells.End = HdrTable.Rows(1).Cells(2).Range.End
oCells.Select
lbl_Exit:
Set HdrRange = Nothing
Set HdrTable = Nothing
Set oCells = Nothing
Exit Sub
End SubHowever, there is no need to select the cells (and that screws up the display) in order to process them. You haven't said what you want to do with the selected cells, but if you want to write to them, process them individually e.g.
Option Explicit
Public Sub Test2()
Dim HdrRange As Range
Dim HdrTable As Table
Dim oCell As Range
Set HdrRange = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
Set HdrTable = ActiveDocument.Tables.Add(HdrRange, 3, 3)
With HdrTable
.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
Set oCell = .Cell(1, 1).Range
oCell.End = oCell.End - 1 ' Remove the cell end character from the range
oCell.Text = "This is cell one"
Set oCell = .Cell(1, 2).Range
oCell.End = oCell.End - 1 ' Remove the cell end character from the range
oCell.Text = "This is cell two"
End With
lbl_Exit:
Set HdrRange = Nothing
Set HdrTable = Nothing
Set oCell = Nothing
Exit Sub
End Sub

gmaxey
02-04-2016, 04:29 AM
If you typically use PrintView like I do, Graham's macro may present a awkward display. Consider:


Public Sub Test()
Dim HdrRange As Range
Dim HdrTable As Table
Dim oCells As Range
Set HdrRange = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
Set HdrTable = ActiveDocument.Tables.Add(HdrRange, 3, 3)
With HdrTable
.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
End With
Set oCells = HdrTable.Range
oCells.End = HdrTable.Rows(1).Cells(2).Range.End
oCells.Select
With ActiveDocument.ActiveWindow.View
.Type = wdPrintView
.SeekView = wdSeekCurrentPageHeader
End With
lbl_Exit:
Set HdrRange = Nothing
Set HdrTable = Nothing
Set oCells = Nothing
Exit Sub
End Sub

gmayor
02-04-2016, 05:23 AM
If you typically use PrintView like I do, Graham's macro may present a awkward display. It will only present 'an awkward display' if the range is selected as per the OPs request. There is no reason to select the range in order to process it, which was the point of my second macro.

gmaxey
02-04-2016, 03:11 PM
Graham.

No criticism of your code intended.