A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Word » Page Layout
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Macro to place Insert information into a footer



 
 
Thread Tools Display Modes
  #1  
Old September 23rd, 2009, 11:54 PM posted to microsoft.public.word.pagelayout
Liz130
external usenet poster
 
Posts: 2
Default Macro to place Insert information into a footer

In Word 2003 I had a lovely macro that I used to place the filename, author,
creation date, revision date, and page number into a footer that repeated on
every page. I would open a blank file, save it to the filename I wanted it
to have, run my macro and poof the footer was exactly as I wanted. I cannot
make Word 2007 do that for me. The macro blows up with Run-time error '5941':
The requested member of the collection does not exist.

I created the macro using the record device in macro. Debug highlights the
two lines " ActiveDocument.AttachedTemplate.BuildingBlockEntri es("Plain
Number 2"). _
Insert Whe=Selection.Range, RichText:=True"

Here is the full code:


Sub Foot()
'
' Foot Macro
' Create Footer for Manuscripts
'
WordBasic.ViewFooterOnly
ActiveDocument.AttachedTemplate.BuildingBlockEntri es("Plain Number 2"). _
Insert Whe=Selection.Range, RichText:=True
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME ", PreserveFormatting:=True
Selection.TypeText Text:=" "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"AUTHOR ", PreserveFormatting:=True
Selection.TypeText Text:=" "
Selection.InsertDateTime DateTimeFormat:="MMMM d, yyyy", InsertAsField:= _
False, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
InsertAsFullWidth:=False
Selection.TypeText Text:=" "
Selection.InsertDateTime DateTimeFormat:="M/d/yyyy h:mm am/pm", _
InsertAsField:=True, DateLanguage:=wdEnglishUS, CalendarType:= _
wdCalendarWestern, InsertAsFullWidth:=False
Selection.TypeText Text:=" "
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

  #2  
Old September 24th, 2009, 01:09 AM posted to microsoft.public.word.pagelayout
Jay Freedman
external usenet poster
 
Posts: 9,488
Default Macro to place Insert information into a footer

There's a lesson he Don't always assume that the macro recorder
does the right thing. In fact, there's an article about its foibles:
http://www.word.mvps.org/FAQs/Macros...ordedMacro.htm

In this case, the recorder is completely wrong when it assumes that
the building block you clicked in the gallery came from the template
attached to the active document. In fact, it comes from the Building
Block.dotx template. There is no way to record the code that's
necessary to find that building block:

Sub Foot()
Dim oTmp As Template, BBTmp As Template
For Each oTmp In Templates
If LCase(oTmp.Name) = "building blocks.dotx" Then
Set BBTmp = oTmp
Exit For
End If
Next

If Not BBTmp Is Nothing Then
WordBasic.ViewFooterOnly
BBTmp.BuildingBlockEntries("Plain Number 2"). _
Insert Whe=Selection.Range, RichText:=True

' rest of code here
End If
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.


On Wed, 23 Sep 2009 15:54:01 -0700, Liz130
wrote:

In Word 2003 I had a lovely macro that I used to place the filename, author,
creation date, revision date, and page number into a footer that repeated on
every page. I would open a blank file, save it to the filename I wanted it
to have, run my macro and poof the footer was exactly as I wanted. I cannot
make Word 2007 do that for me. The macro blows up with Run-time error '5941':
The requested member of the collection does not exist.

I created the macro using the record device in macro. Debug highlights the
two lines " ActiveDocument.AttachedTemplate.BuildingBlockEntri es("Plain
Number 2"). _
Insert Whe=Selection.Range, RichText:=True"

Here is the full code:


Sub Foot()
'
' Foot Macro
' Create Footer for Manuscripts
'
WordBasic.ViewFooterOnly
ActiveDocument.AttachedTemplate.BuildingBlockEntri es("Plain Number 2"). _
Insert Whe=Selection.Range, RichText:=True
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME ", PreserveFormatting:=True
Selection.TypeText Text:=" "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"AUTHOR ", PreserveFormatting:=True
Selection.TypeText Text:=" "
Selection.InsertDateTime DateTimeFormat:="MMMM d, yyyy", InsertAsField:= _
False, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
InsertAsFullWidth:=False
Selection.TypeText Text:=" "
Selection.InsertDateTime DateTimeFormat:="M/d/yyyy h:mm am/pm", _
InsertAsField:=True, DateLanguage:=wdEnglishUS, CalendarType:= _
wdCalendarWestern, InsertAsFullWidth:=False
Selection.TypeText Text:=" "
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

  #3  
Old September 24th, 2009, 02:42 AM posted to microsoft.public.word.pagelayout
Liz130
external usenet poster
 
Posts: 2
Default Macro to place Insert information into a footer

Okay and I thank you. I cut and pasted as instructed, removing the offending
code in the process. It no longer bombs and it does create a footer and
position me in the footer but it does not automatically fill in the fields.
Waqs that capability sacrificed in 2007?

"Jay Freedman" wrote:

There's a lesson he Don't always assume that the macro recorder
does the right thing. In fact, there's an article about its foibles:
http://www.word.mvps.org/FAQs/Macros...ordedMacro.htm

In this case, the recorder is completely wrong when it assumes that
the building block you clicked in the gallery came from the template
attached to the active document. In fact, it comes from the Building
Block.dotx template. There is no way to record the code that's
necessary to find that building block:

Sub Foot()
Dim oTmp As Template, BBTmp As Template
For Each oTmp In Templates
If LCase(oTmp.Name) = "building blocks.dotx" Then
Set BBTmp = oTmp
Exit For
End If
Next

If Not BBTmp Is Nothing Then
WordBasic.ViewFooterOnly
BBTmp.BuildingBlockEntries("Plain Number 2"). _
Insert Whe=Selection.Range, RichText:=True

' rest of code here
End If
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.


On Wed, 23 Sep 2009 15:54:01 -0700, Liz130
wrote:

In Word 2003 I had a lovely macro that I used to place the filename, author,
creation date, revision date, and page number into a footer that repeated on
every page. I would open a blank file, save it to the filename I wanted it
to have, run my macro and poof the footer was exactly as I wanted. I cannot
make Word 2007 do that for me. The macro blows up with Run-time error '5941':
The requested member of the collection does not exist.

I created the macro using the record device in macro. Debug highlights the
two lines " ActiveDocument.AttachedTemplate.BuildingBlockEntri es("Plain
Number 2"). _
Insert Whe=Selection.Range, RichText:=True"

Here is the full code:


Sub Foot()
'
' Foot Macro
' Create Footer for Manuscripts
'
WordBasic.ViewFooterOnly
ActiveDocument.AttachedTemplate.BuildingBlockEntri es("Plain Number 2"). _
Insert Whe=Selection.Range, RichText:=True
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME ", PreserveFormatting:=True
Selection.TypeText Text:=" "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"AUTHOR ", PreserveFormatting:=True
Selection.TypeText Text:=" "
Selection.InsertDateTime DateTimeFormat:="MMMM d, yyyy", InsertAsField:= _
False, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
InsertAsFullWidth:=False
Selection.TypeText Text:=" "
Selection.InsertDateTime DateTimeFormat:="M/d/yyyy h:mm am/pm", _
InsertAsField:=True, DateLanguage:=wdEnglishUS, CalendarType:= _
wdCalendarWestern, InsertAsFullWidth:=False
Selection.TypeText Text:=" "
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub


  #4  
Old September 24th, 2009, 03:15 AM posted to microsoft.public.word.pagelayout
Jay Freedman
external usenet poster
 
Posts: 9,488
Default Macro to place Insert information into a footer

The Author and Date fields should update automatically, but the
Filename field never will (http://support.microsoft.com/kb/832897).
This is not new behavior in Word 2007.

To work around this, add a line just before the End Sub:

Selection.Paragraphs(1).Range.Fields.Update

Also create the AutoOpen macro described by the KB article.

On Wed, 23 Sep 2009 18:42:01 -0700, Liz130
wrote:

Okay and I thank you. I cut and pasted as instructed, removing the offending
code in the process. It no longer bombs and it does create a footer and
position me in the footer but it does not automatically fill in the fields.
Waqs that capability sacrificed in 2007?

"Jay Freedman" wrote:

There's a lesson he Don't always assume that the macro recorder
does the right thing. In fact, there's an article about its foibles:
http://www.word.mvps.org/FAQs/Macros...ordedMacro.htm

In this case, the recorder is completely wrong when it assumes that
the building block you clicked in the gallery came from the template
attached to the active document. In fact, it comes from the Building
Block.dotx template. There is no way to record the code that's
necessary to find that building block:

Sub Foot()
Dim oTmp As Template, BBTmp As Template
For Each oTmp In Templates
If LCase(oTmp.Name) = "building blocks.dotx" Then
Set BBTmp = oTmp
Exit For
End If
Next

If Not BBTmp Is Nothing Then
WordBasic.ViewFooterOnly
BBTmp.BuildingBlockEntries("Plain Number 2"). _
Insert Whe=Selection.Range, RichText:=True

' rest of code here
End If
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.


On Wed, 23 Sep 2009 15:54:01 -0700, Liz130
wrote:

In Word 2003 I had a lovely macro that I used to place the filename, author,
creation date, revision date, and page number into a footer that repeated on
every page. I would open a blank file, save it to the filename I wanted it
to have, run my macro and poof the footer was exactly as I wanted. I cannot
make Word 2007 do that for me. The macro blows up with Run-time error '5941':
The requested member of the collection does not exist.

I created the macro using the record device in macro. Debug highlights the
two lines " ActiveDocument.AttachedTemplate.BuildingBlockEntri es("Plain
Number 2"). _
Insert Whe=Selection.Range, RichText:=True"

Here is the full code:


Sub Foot()
'
' Foot Macro
' Create Footer for Manuscripts
'
WordBasic.ViewFooterOnly
ActiveDocument.AttachedTemplate.BuildingBlockEntri es("Plain Number 2"). _
Insert Whe=Selection.Range, RichText:=True
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME ", PreserveFormatting:=True
Selection.TypeText Text:=" "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"AUTHOR ", PreserveFormatting:=True
Selection.TypeText Text:=" "
Selection.InsertDateTime DateTimeFormat:="MMMM d, yyyy", InsertAsField:= _
False, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
InsertAsFullWidth:=False
Selection.TypeText Text:=" "
Selection.InsertDateTime DateTimeFormat:="M/d/yyyy h:mm am/pm", _
InsertAsField:=True, DateLanguage:=wdEnglishUS, CalendarType:= _
wdCalendarWestern, InsertAsFullWidth:=False
Selection.TypeText Text:=" "
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub


  #5  
Old September 24th, 2009, 06:51 AM posted to microsoft.public.word.pagelayout
Graham Mayor
external usenet poster
 
Posts: 18,297
Default Macro to place Insert information into a footer

It would be as well to also add a line to save the document before updating
the filename field, so that there is a filename to display, rather than the
default Document(n)?

--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Jay Freedman wrote:
The Author and Date fields should update automatically, but the
Filename field never will (http://support.microsoft.com/kb/832897).
This is not new behavior in Word 2007.

To work around this, add a line just before the End Sub:

Selection.Paragraphs(1).Range.Fields.Update

Also create the AutoOpen macro described by the KB article.

On Wed, 23 Sep 2009 18:42:01 -0700, Liz130
wrote:

Okay and I thank you. I cut and pasted as instructed, removing the
offending code in the process. It no longer bombs and it does
create a footer and position me in the footer but it does not
automatically fill in the fields. Waqs that capability sacrificed in
2007?

"Jay Freedman" wrote:

There's a lesson he Don't always assume that the macro recorder
does the right thing. In fact, there's an article about its foibles:
http://www.word.mvps.org/FAQs/Macros...ordedMacro.htm

In this case, the recorder is completely wrong when it assumes that
the building block you clicked in the gallery came from the template
attached to the active document. In fact, it comes from the Building
Block.dotx template. There is no way to record the code that's
necessary to find that building block:

Sub Foot()
Dim oTmp As Template, BBTmp As Template
For Each oTmp In Templates
If LCase(oTmp.Name) = "building blocks.dotx" Then
Set BBTmp = oTmp
Exit For
End If
Next

If Not BBTmp Is Nothing Then
WordBasic.ViewFooterOnly
BBTmp.BuildingBlockEntries("Plain Number 2"). _
Insert Whe=Selection.Range, RichText:=True

' rest of code here
End If
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.


On Wed, 23 Sep 2009 15:54:01 -0700, Liz130
wrote:

In Word 2003 I had a lovely macro that I used to place the
filename, author, creation date, revision date, and page number
into a footer that repeated on every page. I would open a blank
file, save it to the filename I wanted it to have, run my macro
and poof the footer was exactly as I wanted. I cannot make Word
2007 do that for me. The macro blows up with Run-time error
'5941': The requested member of the collection does not exist.

I created the macro using the record device in macro. Debug
highlights the two lines "
ActiveDocument.AttachedTemplate.BuildingBlockEntri es("Plain Number
2"). _ Insert Whe=Selection.Range, RichText:=True"

Here is the full code:


Sub Foot()
'
' Foot Macro
' Create Footer for Manuscripts
'
WordBasic.ViewFooterOnly
ActiveDocument.AttachedTemplate.BuildingBlockEntri es("Plain
Number 2"). _ Insert Whe=Selection.Range, RichText:=True
Selection.Fields.Add Range:=Selection.Range,
Type:=wdFieldEmpty, Text:= _ "FILENAME ",
PreserveFormatting:=True Selection.TypeText Text:=" "
Selection.Fields.Add Range:=Selection.Range,
Type:=wdFieldEmpty, Text:= _ "AUTHOR ",
PreserveFormatting:=True Selection.TypeText Text:=" "
Selection.InsertDateTime DateTimeFormat:="MMMM d, yyyy",
InsertAsField:= _ False, DateLanguage:=wdEnglishUS,
CalendarType:=wdCalendarWestern, _ InsertAsFullWidth:=False
Selection.TypeText Text:=" "
Selection.InsertDateTime DateTimeFormat:="M/d/yyyy h:mm am/pm",
_ InsertAsField:=True, DateLanguage:=wdEnglishUS,
CalendarType:= _ wdCalendarWestern, InsertAsFullWidth:=False
Selection.TypeText Text:=" "
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub



 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 04:36 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.