Test to see if date and time field exists in footer

L

Legal Learning

and if it does, update it and if it doesn't add one. There is a document id
placed in the footer by the document management system first and then a date
and time word field needs to be placed to the right of that document id.

Some documents will have the date and time field and some won't so I need to
test for it before adding it. All of the documents would have the doc id
however.


Using Word 2000 (unfortunately)

Thanks for your help in advance.

CG
 
H

Helmut Weber

Hi,

something along these lines:

Sub Macro7()
Dim rFtr As Range
Dim oFld As Field
Dim bDat As Boolean
Set rFtr = ActiveDocument.StoryRanges(wdPrimaryFooterStory)
For Each oFld In rFtr.Fields
If oFld.Type = 31 Then
bDat = True
Exit For
End If
Next
MsgBox bDat
End Sub

This may get a bit more complicated,
if there are different footers,
not to speak of different sections.

Also, as it seems, there is only one date field type.
Whether time is displayed in addition to date,
could be answered from the field code or the field result.

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
L

Legal Learning

There will be multiple sections, first page footer etc. Can I put that in
the set ftr line?
 
L

Legal Learning

Ok, now I am feeling really stupid. I have battled with this for 4 hours
now. This should not be that hard??? There has to be a solution to this
one. Any suggestions?
 
L

Legal Learning

Ok, here is what is working with the code below: It updates a field that
exists but does not add on when it does not exist. I am sure I messed
something up here. I appreciate any help with this at all.


Sub AddSelectedFields()
Dim pRange As Word.Range

For Each pRange In ActiveDocument.StoryRanges
Do Until (pRange Is Nothing)
Select Case pRange.StoryType
Case wdFirstPageFooterStory, wdPrimaryFooterStory,
wdEvenPagesFooterStory

On Error Resume Next
If pRange.Fields Then
pRange.Fields.Update
Else
With Selection
.Collapse Direction:=wdCollapseStart
.Fields.Add Range:=Selection.Range, Type:=wdFieldDate
End With
End If

End Select
Set pRange = pRange.NextStoryRange
Loop
Next

End Sub
 
B

Bear

Legallearning:

I'm going to express my personal opinion here, and if I'm steering you
wrong, I hope somebody who knows better will inform us both.

I've learned that for any application like this (if it's there, then... if
it's not, then) it's almost always easier to make the condition than to test
for the condition.

In other words, I'd approach it by deleting everything (now I KNOW there's
no date field there) and adding it all back the way I want it.

This is just a strategy thing, I have no actual code to show you.

More strategy: If you're storing your official corporate header and footer
as autotext in the attached template it gets even easier. You just verify
that the correct template is attached, delete any header and footer content
you encounter, replacing it with the material in the autotext entry.

Bear
 
B

Bear

Wait! I DO have some code. This should work you through all the headers and
footers in the document. You'll still need the code to delete the old content
and insert the new content.

For Each objSection In ActiveDocument.Sections
For Each objHeaderFooter In objSection.Headers
' Your code here, probably working on objHeaderFooter.Range
Next objHeaderFooter
For Each objHeaderFooter In objSection.Footers
' Your code here, probably working on objHeaderFooter.Range
Next objHeaderFooter
Next objSection

Bear
 

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