Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2007

Tip: Looking for answers? Try searching our database.

Add the document property of Subject to a table in a footer

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Legal Learning - 19 Nov 2007 20:21 GMT
The table is already in the footer and the built in document property of
Subject needs to be inserted into this 1 row 3 column table in every footer  
regardless of how many sections there are in the document.  The field needs
to be in the 1st column.

I have the code working to delete anything in that row/column but can not
get it to add without errors.
Signature

CLG

Jean-Guy Marcil - 19 Nov 2007 20:33 GMT
Legal Learning was telling us:
Legal Learning nous racontait que :

> The table is already in the footer and the built in document property
> of Subject needs to be inserted into this 1 row 3 column table in
[quoted text clipped - 3 lines]
> I have the code working to delete anything in that row/column but can
> not get it to add without errors.

What errors do you get?

Post the relevant code.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Legal Learning - 19 Nov 2007 20:44 GMT
Here is the code.  Thanks for your help!

Sub AddFileName()

Dim sSubject As String
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
       pRange.Tables(1).Cell(1, 1).Range.Fields.Add Range:=Selection.Range,
Type:=wdFieldSubject
       On Error GoTo 0
     Case Else
       'Do nothing
   End Select
   Set pRange = pRange.NextStoryRange
 Loop
Next

End Sub

Signature

CLG

> Legal Learning was telling us:
> Legal Learning nous racontait que :
[quoted text clipped - 10 lines]
>
> Post the relevant code.
Jean-Guy Marcil - 19 Nov 2007 22:07 GMT
Legal Learning was telling us:
Legal Learning nous racontait que :

> Here is the code.  Thanks for your help!
>
[quoted text clipped - 35 lines]
>>
>> Post the relevant code.

You still did not mention the error you got... But from looking at your
code, I guess it has to do with the following line:

   pRange.Tables(1).Cell(1, 1).Range.Fields.Add Range:=Selection.Range,
Type:=wdFieldSubject

The code will add the field at the range represented by the current
selection, not in the first cell of the table as desired.
See the following code for a correction.

'_______________________________________
Sub AddFileName()

Dim sSubject As String
Dim pRange As Word.Range
Dim rgeCell As Range

For Each pRange In ActiveDocument.StoryRanges
 Do Until (pRange Is Nothing)
   Select Case pRange.StoryType
     Case wdFirstPageFooterStory, _
          wdPrimaryFooterStory, wdEvenPagesFooterStory
       On Error Resume Next
       Set rgeCell = pRange.Tables(1).Cell(1, 1).Range
       rgeCell.Collapse wdCollapseStart
       rgeCell.Fields.Add Range:=rgeCell, Type:=wdFieldSubject
       On Error GoTo 0
     Case Else
       'Do nothing
   End Select
   Set pRange = pRange.NextStoryRange
 Loop
Next

End Sub
'_______________________________________

Why the
   On Error Resume Next
line?

It is always better to code for predictable errors than to have a Band-Aid
that will let everything go.
I guess it has to do with the fact that some footers may not have a table.
If this is the case, try this instead:

'_______________________________________
Sub AddFileName()

Dim sSubject As String
Dim pRange As Word.Range
Dim rgeCell As Range

For Each pRange In ActiveDocument.StoryRanges
 Do Until (pRange Is Nothing)
   Select Case pRange.StoryType
     Case wdFirstPageFooterStory, _
          wdPrimaryFooterStory, wdEvenPagesFooterStory
       If pRange.Tables.Count > 0 Then
           Set rgeCell = pRange.Tables(1).Cell(1, 1).Range
           rgeCell.Collapse wdCollapseStart
           rgeCell.Fields.Add Range:=rgeCell, Type:=wdFieldSubject
       End If
     Case Else
       'Do nothing
   End Select
   Set pRange = pRange.NextStoryRange
 Loop
Next

End Sub
'_______________________________________

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Legal Learning - 19 Nov 2007 22:23 GMT
Perfect!  You rock.  Thanks so much.  
Signature

CLG

> Legal Learning was telling us:
> Legal Learning nous racontait que :
[quoted text clipped - 111 lines]
> End Sub
> '_______________________________________

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.