PDA

View Full Version : Pasting in a footer?



garethtaylor
12-29-2009, 04:09 AM
Ok cannot get this to work any help will be appreciated.
I am copying some text from a text box opening a document then pasting the footer in.
I have tried recording it, however, when I run it the pasted text pastes onto the page and not into the footer and I don’t know why?
As the text box information changes I cannot write the footer info in the macro this is why I need to paste it.
This is what I have
Private Sub CommandButton42_Click()
ActiveDocument.Shapes("Text Box 19").Select
Selection.WholeStory
Selection.Copy
ChangeFileOpenDirectory "O:\In development - not to be used yet\Master Documents\Master Letter Templates\"
Documents.Open FileName:="C1.dot", ConfirmConversions:=False, ReadOnly:= _
False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _
"", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
Format:=wdOpenFormatAuto
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.MoveDown Unit:=wdScreen, Count:=1
Selection.Paste
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

fumei
12-29-2009, 10:11 AM
1. WHY are you opening a .DOT file?? This is not normal.

2. You can action into a footer directly by using an object.
Dim oHF As HeaderFooter
Set oHF = ActiveDocument.Sections(1) _
.Footers(wdHeaderFooterPrimary)

' make your copy and then....
oHF.Range.PasteAndFormat(wdFormatOriginalFormatting)

This pastes (with original formatting) the contents of the Copy into the Primary footer of Section 1.

fumei
12-29-2009, 11:36 AM
Using Selection is not efficient. Most of the time it is not needed.

BTW: why Selection.MoveDown Unit:=wdScreen

fumei
12-29-2009, 12:00 PM
Could you confirm something? When I try:
ActiveDocument.Shapes(1).Select ' using index number
Selection.WholeStory
Selection.Copy
it copies everything, not just the textbox contents. That is WholeStory. Are you saying it only copies the textbox contents for you?

BTW: if I execute your whole code - but changing the Shapes to an index number - like this:
ActiveDocument.Shapes(1).Select
Selection.WholeStory
Selection.Copy

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.MoveDown Unit:=wdScreen, Count:=1
Selection.Paste
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
it does copy the textbox into the footer, NOT the page. Although, as stated it copies everything, not just the textbox content. The textbox itself is copied.

BTW: it does the same with (but only the textbox) with:ActiveDocument.Shapes(1).Select
Selection.Copy
Dim oHF As HeaderFooter
Set oHF = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
oHF.Range.PasteAndFormat (wdFormatOriginalFormatting)
Note that it copies the textbox, NOT the text content of the textbox.

You can copy only the text by using:
Dim oHF As HeaderFooter
Set oHF = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
oHF.Range.Text = ActiveDocument.Shapes("Text Box 19") _
.TextFrame.TextRange.Text
this copies the text content, and only the text content.

If you already have content in the footer - and you are trying to ADD the textbox text to it, then use:
Dim oHF As HeaderFooter
Set oHF = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
oHF.Range.InsertAfter ActiveDocument.Shapes("Text Box 19") _
.TextFrame.TextRange.Text
which inserts the text content of Text Box 19 at the end of the range of the Primary footer of Section 1.

No Selection.
No use of View.

TonyJollans
01-05-2010, 12:24 PM
Oh, what fun!!!

1. Selection.MoveDown Unit:=wdScreen will go from the Header that the View opens to the Footer --- in some circumstances. Best to replace ..
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.MoveDown Unit:=wdScreen, Count:=1
with
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter

2. Gerry .. what .WholeStory selects depends on the Story you are in, which depeds on whether the Text Box is InLine with Text or Wrapped.

3. I don't know why, but the line:
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Only selects the Header View if the code is running in the ActiveDocument - hence it works for Gerry's test, but not for Gareth's.

4. Gareth .. you'll need to Paste explicitly as Gerry demonstrates, except that I prefer to avoid messing with the User's Clipboard if possible, something like this:

Private Sub CommandButton42_Click()

Dim SourceDoc As Word.Document
Dim TargetDoc As Word.Document

Set SourceDoc = ActiveDocument
Set TargetDoc = Documents.Open(FileName:="C1.dot")

TargetDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text _
= SourceDoc.Shapes("Text Box 19").TextFrame.TextRange.Text

End Sub

fumei
01-05-2010, 01:46 PM
"4. Gareth .. you'll need to Paste explicitly as Gerry demonstrates, except that I prefer to avoid messing with the User's Clipboard if possible, something like this:"


In my last example, I do not use any Paste.
Dim oHF As HeaderFooter
Set oHF = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
oHF.Range.Text = ActiveDocument.Shapes("Text Box 19") _
.TextFrame.TextRange.Text

Although, I admit, I did use the word "copies". Technically of course it is NOT a copy. It is an equals to.


I would most certainly agree that it is best to use explicit document objects lik eSourceDoc and TargetDoc.

I still want to know the .DOT file itself is being opened.

TonyJollans
01-06-2010, 01:08 AM
Mea culpa. You know I don't read posts properly, Gerry :)