![]() |
|
|||||||
|
|
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
Hello,
I have the following code (an InsertFooterPage procedure) that allows me to insert a footer at the end of my document. It is a series of QuickPart fields + a Buildingblock. However, after inserting the footer, my toggle buttons no longer refresh. If I delete the footer (using a DeleteFooterPage procedure), the toggle buttons refresh again. The problem occurs after executing Code:
For Each oSection In ActiveDocument.Sections - The functions are activated by a checkbox. The checkbox itself does not cause any problems. - Writing the formula (fields and buildingblock) directly in the footers also creates the same problem. Has anyone found a solution? Best regards, David Code:
Sub InsertFooterPage ()
Dim oSection As section
Dim oFooter As HeaderFooter
Dim oRng As Range
Application.ScreenUpdating = False
With ActiveDocument.ActiveWindow.View
.SeekView = wdSeekCurrentPageFooter
End With
ActiveWindow.ActivePane.View.ShowFieldCodes = True
For Each oFooter In ActiveDocument.Sections(1).Footers
If oFooter.Exists Then
oFooter.Range.Delete
oFooter.Range.Font.Size = 7
End If
Next oFooter
For Each oSection In ActiveDocument.Sections
For Each oFooter In oSection.Footers
If oFooter.Exists Then
Set oRng = oFooter.Range
With oRng
.Collapse 0
.Fields.Add Range:=oRng, Type:=wdFieldIf, Text:="{PAGE} = {NUMPAGES} {AUTOTEXT}", PreserveFormatting:=False
.Collapse 1
.MoveEndUntil "}"
.End = .End + 1
.MoveStartUntil "{"
.Text = ""
.Fields.Add Range:=oRng, Type:=wdFieldPage, PreserveFormatting:=False
.Collapse 0
.MoveEndUntil "}"
.End = .End + 1
.MoveStartUntil "{"
.Text = ""
.Fields.Add Range:=oRng, Type:=wdFieldNumPages, PreserveFormatting:=False
.Collapse 0
.MoveEndUntil "}"
.End = .End + 1
.MoveStartUntil "{"
.Text = ""
.Fields.Add Range:=oRng, Type:=wdFieldAutoText, Text:="MyBuildingBlock3", PreserveFormatting:=False
.Fields.Update
End With
End If
Next oFooter
Next oSection
ActiveWindow.View.ShowFieldCodes = False
With ActiveWindow.View
.Type = wdPrintView
.SeekView = wdSeekMainDocument
End With
Application.ScreenUpdating = True
If Not objRibbon Is Nothing Then
objRibbon.Invalidate
End If
GoTo Sortir
Sortir:
On Error Resume Next
If Not oSection Is Nothing Then Set oSection = Nothing
If Not oFooter Is Nothing Then Set oFooter = Nothing
If Not oRng Is Nothing Then Set oRng = Nothing
Exit Sub
End Sub
|
|
#2
|
||||
|
||||
|
You're going about this in a very convoluted way. There is no need to loop through all Sections or to activate the Header/Footer pane.
For example: Code:
Sub InsertFooterPage()
Application.ScreenUpdating = False
Dim HdFt As HeaderFooter, Rng As Range
For Each HdFt In ActiveDocument.Sections.Last.Footers
If HdFt.Exists Then
Set Rng = ActiveDocument.Fields.Add(Range:=HdFt.Range, Type:=wdFieldIf, Text:="{} = {} {}", PreserveFormatting:=False).Code
With Rng
.Collapse wdCollapseStart
.MoveStartUntil "{"
.End = .End + 2
.Text = ""
.Fields.Add Range:=Rng, Type:=wdFieldPage, PreserveFormatting:=False
.Collapse wdCollapseEnd
.MoveStartUntil "{"
.End = .End + 2
.Text = ""
.Fields.Add Range:=Rng, Type:=wdFieldNumPages, PreserveFormatting:=False
.Collapse wdCollapseEnd
.MoveStartUntil "{"
.End = .End + 2
.Text = ""
.Fields.Add Range:=Rng, Type:=wdFieldAutoText, Text:="MyBuildingBlock3", PreserveFormatting:=False
End With
End If
Next
Application.ScreenUpdating = True
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
Why not create a single building block with the necessary field codes. E.g.,
Select your existing footer construction. Save it as MyFooterLastPageBB Then simply use: Code:
Sub InsertFooterPageII()
Dim HdFt As HeaderFooter, Rng As Range
Application.ScreenUpdating = False
For Each HdFt In ActiveDocument.Sections.Last.Footers
If HdFt.Exists Then
Set Rng = HdFt.Range
'Replace NormalTemplate as necessary to reflect where your BB is stored.
NormalTemplate.BuildingBlockEntries("MyFooterLastPageBB").Insert Rng, True
End If
Next HdFt
Application.ScreenUpdating = True
lbl_Exit:
ExitSub
End Sub
|
|
#4
|
|||
|
|||
|
Thanks Macropod! Yes, your code is much simpler and cleaner (I have to admit that after trying a bunch of things to solve the problem, I ended up writing and leaving useless lines of code).
I tested your code, it works very well, but the problem is still there. My toggleButtons are no longer refreshed when the footer is inserted. Thanks gmaxey for your code! It's well thought out, works very well, and reduces the number of lines even further. I used ActiveDocument.AttachedTemplate... However, I'll use Macropod's version instead, simply because I have more visibility of the whole thing, the construction of the fields, the building block call... Otherwise, unfortunately, the problem persists too as indicated above. In the meantime, I've done several tests, and it seems that the problem comes from the content of the building block. It's a simple table with a single cell within text. - The table is displayed correctly, but when it is present, the cursor partially disappears and no longer flashes in the document, even though I can still write text. I have the impression that part of the process remains in the footer, as if the insertion process were not complete and the cursor had not been fully rendered. - Now, if I simply put text in my building block (without a table), it inserts correctly and the problem disappears. The toggle buttons work normally. Everything suggests that the problem does indeed originate from the table in the building block. If you have any ideas or solutions to resolve this issue, I would be very grateful if you could share them with me. For my part, I will continue to look into this issue with tables in BB. If I find out anything more, I will let you know. Best regards, David |
|
#5
|
||||
|
||||
|
I have no idea what toggle buttons you're referring to, so I can't help you with that. Additionally, if all you're doing is inserting a table, you could use code like:
Code:
Sub InsertFooterPage()
Application.ScreenUpdating = False
Dim HdFt As HeaderFooter, Rng As Range, Tbl As Table
For Each HdFt In ActiveDocument.Sections.Last.Footers
If HdFt.Exists Then
Set Rng = ActiveDocument.Fields.Add(Range:=HdFt.Range, Type:=wdFieldEmpty, Text:="IF= ", PreserveFormatting:=False).Code
With Rng
.Collapse wdCollapseEnd
Set Tbl = .Tables.Add(.Duplicate, 1, 1)
Tbl.Cell(1, 1).Range.Text = "Hello World"
Tbl.Range.Characters.Last.Next.InsertBefore Chr(34)
Tbl.Range.Characters.First.Previous.InsertBefore Chr(34)
.End = Tbl.Range.Start - 3
.Fields.Add Range:=Rng, Type:=wdFieldNumPages, PreserveFormatting:=False
.End = .End - 2
.Fields.Add Range:=Rng, Type:=wdFieldPage, PreserveFormatting:=False
End With
End If
Next
Application.ScreenUpdating = True
End Sub
Code:
Sub InsertFooterPage()
Application.ScreenUpdating = False
Dim HdFt As HeaderFooter, Rng As Range, Tbl As Table
For Each HdFt In ActiveDocument.Sections.Last.Footers
If HdFt.Exists Then
Set Rng = ActiveDocument.Fields.Add(Range:=HdFt.Range, Type:=wdFieldEmpty, Text:="IF= """"", PreserveFormatting:=False).Code
With Rng
.Collapse wdCollapseEnd
.End = .End - 2
Set Tbl = .Tables.Add(.Duplicate, 1, 1)
Tbl.Cell(1, 1).Range.Text = "Hello World"
.End = Tbl.Range.Start - 3
.Fields.Add Range:=Rng, Type:=wdFieldNumPages, PreserveFormatting:=False
.End = .End - 2
.Fields.Add Range:=Rng, Type:=wdFieldPage, PreserveFormatting:=False
End With
End If
Next
Application.ScreenUpdating = True
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#6
|
|||
|
|||
|
Thank you very much, macropod, for your help!
To give you an idea of how the toggle buttons I use work, they are essentially the same as the buttons for aligning a paragraph (right, center, left, or justified). Only one of these buttons can be selected at a time. The solution to this problem is not to save a table in a Building Block, but rather to use and adapt a simple text paragraph and surround it with borders... (see Home tab > Paragraph group). In this case, only one toggle button is selected at a time. Thank you all! Best regards, David |
|
#7
|
||||
|
||||
|
Presumably you have some code attached to the buttons that works with a selection. Obviously enough, if nothing is selected, such buttons won't do anything. Neither your code nor any of Greg's or mine selects anything.
Since you haven't posted a document containing said buttons & code, or a substantive explanation of what they're supposed to do in this context, we can't really help.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#8
|
|||
|
|||
|
Thanks for your feedback, macropod!
Actually, the problem isn't with the toggle buttons, but with the building blocks that don't handle tables well, and as a result don't fully release control to the ribbon. |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Sorting Charts - series custom color problem | Rhene | Excel | 0 | 08-02-2017 11:20 AM |
Run-time error '-2147467259 (80004005)': Method 'Insert' of object 'BuildingBlock' failed
|
Katherine1995 | Word VBA | 6 | 11-17-2016 12:04 PM |
| INSERT building blocks from Quickpart in word | jasserin | Word VBA | 0 | 06-05-2013 12:55 PM |
| Need help with quickparts(fields) in footer | Nighthawk | Word | 2 | 08-22-2012 05:13 AM |
Sorting or an Excel formula to insert a blank row after ending a series.
|
PRADEEPB270 | Excel | 1 | 09-07-2011 02:33 AM |