Copy just the page field into the clipboard from a header

A

andreas

Dear Experts:

For some specific reason I need to copy a page field ( {Page
\*Arabic} ) located in one of my headers into the clipboard using
VBA.

How can I set a range in the header to include just the page field
( {Page \*Arabic} )


The header has the following make-up:

{StyleRef Heading 1\n} {StyleRef Heading 1} Tab Tab {Page
\*Arabic}


I guess the code should start like this but then I am not able to set
the range to include just the page field and copy it into the
clipboard.


Dim rng As Range
Dim sect As Section

Set sect = ActiveDocument.Sections(1)

Set rng = sect.Headers(wdHeaderFooterFirstPage).Range

rng.MoveUntil ???
rng.Copy ????

Help is much appreciated. Thank you very much in advance.Regards,
Andreas
 
H

Helmut Weber

Hi Andreas,

like that:

Sub Test456aa()
Dim rng As Range
Dim sect As Section
Dim Fld As Field
Set sect = ActiveDocument.Sections(1)
Set rng = sect.Headers(wdHeaderFooterFirstPage).Range
For Each Fld In rng.Fields
If Fld.Type = wdFieldPage Then
Fld.Copy
End If
Next
End Sub

Note that the content of the clipboard varies
wether show field codes is true or false.
--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
G

Graham Mayor

You could use something like

Dim oHeader As HeaderFooter
Dim oField As Field
Set oHeader =
ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage)
If oHeader.Exists Then
For Each oField In oHeader.Range.Fields
If oField.Type = wdFieldPage Then
oField.Copy
Exit For
End If
Next oField
Else
Set oHeader =
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
For Each oField In oHeader.Range.Fields
If oField.Type = wdFieldPage Then
oField.Copy
Exit For
End If
Next oField
End If
MsgBox oField


Note that your Styleref field syntax won't work because it has a space in
the style name and no quotes

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

For some reason my first reply seems to have disappeared from the server and
Helmut now has it covered, however my own effort had a measure of error
trapping, so let's try again:

You could use something like

Dim oHeader As HeaderFooter
Dim oField As Field
Set oHeader =
ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage)
If oHeader.Exists Then
For Each oField In oHeader.Range.Fields
If oField.Type = wdFieldPage Then
oField.Copy
Exit For
End If
Next oField
Else
Set oHeader =
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
For Each oField In oHeader.Range.Fields
If oField.Type = wdFieldPage Then
oField.Copy
Exit For
End If
Next oField
End If
MsgBox oField


Note that your Styleref field syntax won't work because it has a space in
the style name and no quotes

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
A

andreas

For some reason my first reply seems to have disappeared from the server and
Helmut now has it covered, however my own effort had a measure of error
trapping, so let's try again:

You could use something like

Dim oHeader As HeaderFooter
Dim oField As Field
    Set oHeader =
ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage)
    If oHeader.Exists Then
        For Each oField In oHeader.Range.Fields
                If oField.Type = wdFieldPage Then
                    oField.Copy
                    Exit For
                End If
        Next oField
    Else
        Set oHeader =
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
        For Each oField In oHeader.Range.Fields
                If oField.Type = wdFieldPage Then
                    oField.Copy
                    Exit For
                End If
        Next oField
    End If
    MsgBox oField

Note that your Styleref field syntax won't work because it has a space in
the style name and no quotes

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>














- Zitierten Text anzeigen -

Graham, ok, thank you it works just fine like Helmut one's . Thank you
very much. Regards, Andreas
 
A

andreas

Hi Andreas,

like that:

Sub Test456aa()
Dim rng As Range
Dim sect As Section
Dim Fld As Field
Set sect = ActiveDocument.Sections(1)
Set rng = sect.Headers(wdHeaderFooterFirstPage).Range
For Each Fld In rng.Fields
   If Fld.Type = wdFieldPage Then
      Fld.Copy
   End If
Next
End Sub

Note that the content of the clipboard varies
wether show field codes is true or false.
--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP

Helmut, thank you very much for your valuable help. It is working just
fine. Thank you. Regards, Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top