How can I paste the table on my first page/section? - vba

i have this code
word_fichier_template.Tables(1).Range.Copy
'Pasting the table in every section of my document
With word_fichier.Sections(1).Footers(wdHeaderFooterPrimary).Range
.Collapse wdCollapseStart
.Paste
End With
'Working fine, pasting the table in the second section
word_fichier_template.Tables(2).Range.Copy
With word_fichier.Sections(2).Headers(wdHeaderFooterPrimary).Range
.Collapse wdCollapseStart
.Paste
End With
I want to copy/paste a table in headers and footers in a .docx, i dont understand the thing i put in comment in my code. I also tried Headers(wdHeaderFooterFirstPage).Range to put the table just on the first page/section but doesnt work.
Here is a sample of my word.

Your footers are linked - see the tag at the top right of the footer - "Identique au précédente". Turn that setting off and the footers in Section 2 will not be the same as Section 1.
To get a different footer on the first page of a section you need to change the setting in Page Layout.

Related

Set background color of headers and footers

I have been able to change the background color of Word tables in the body of the document with: oSourceTable.Shading.BackgroundPatternColor = wdRed. However, the same code does not work for header or footer Word tables. I have also tried setting the Section back color as well, but to no avail. The code runs, but the Headers and Footers are always displayed with white backgrounds.
I have done mostly Excel VBA, and only a little bit of Word VBA so maybe I am missing something obvious here. Thanks in advance for any ideas and/or suggestions.
As requested, here is the code I am using. vTableBackColors is just an array of colors. This code is for the body tables and is working perfectly.
For Each oWordTable In oWordDoc.Tables
lIndex = lIndex + 1
oWordTable.Shading.BackgroundPatternColor = vTableBackColors(lIndex)
Next
I tried to do the same thing for the header and footer tables, but it does not work. I tried using the Header/Footer tables as below.
For Each oWordSection In oWordDoc.Sections
For Each oWordTable In oWordSection.Headers.Item(wdHeaderFooterPrimary).Range.Tables
' I selected this one to see if it would make a difference.
oWordTable.Select
oWordTable.Shading.BackgroundPatternColor = m_HeaderBackColor
Next
For Each oWordTable In oWordSection.Footers.Item(wdHeaderFooterPrimary).Range.Tables
oWordTable.Shading.BackgroundPatternColor = m_FooterBackColor
Next
Next
I also tried using the Section Headers / Footers directly. Before and after checking shows that the BackgroundPatternColor has changed as desired, but it is not displayed.
oWordSection.Headers.Item(wdHeaderFooterPrimary).Range.Shading.BackgroundPatternColor = m_HeaderBackColor
Here is a screenshot showing the colorized body tables and the unchanged header.
Is it possible that, unlike the Body table colors, the Header / Footer colors are never displayed as such by the Word designer, but are only true at runtime?
Thanks for any additional information.
«I have been able to change the background color of Word tables in the body of the document with: oSourceTable.Shading.BackgroundPatternColor = red.» That would not work unless you have defined 'red' as an RGB value.
As for the header/footer issue, this works for me:
For Each oWordSection In oWordDoc.Sections
For Each oWordTable In oWordSection.Headers.Item(wdHeaderFooterPrimary).Range.Tables
oWordTable.Shading.BackgroundPatternColorIndex = wdRed
Next
For Each oWordTable In oWordSection.Footers.Item(wdHeaderFooterPrimary).Range.Tables
oWordTable.Shading.BackgroundPatternColorIndex = wdRed
Next
Next
Perhaps your underlying problem is in the value you have assigned to 'm_HeaderBackColor' and 'm_FooterBackColor' - which your posted code doesn't show. It's also possible your code is addressing the wrong headers/footers (i.e. maybe it's not the primary ones you need to address).

Need Word VBA code to move cursor to end of first column in two-column page layout

I have a Word VB macro that automatically inserts a Visio drawing in a two-column layout. The code works well, but I would like to further automate the process. To do so, I am looking specifically for code that will move the cursor to the end of the first column of the selected page. This column typically does not already have a column break, so my solution can't involve searching for column breaks. And I believe I already have code that selects the current page, just not the end of the first column on that page.
I am able to move the cursor to the end of the page programmatically, and I am able to move the cursor to the top of the page programmatically. I just can't get the cursor to move to the end of the first column of text. All of the online examples I've found so far focus on moving to the end of a column in a table, but not to the end of a column of text in a two-column layout.
10/15/2019: Here's an example page as requested by #cindym
[SamplePage][1]
The document may be several pages long. My current macro works well and meets company requirements for this kind of document, but it requires the user to place the cursor where they want the two-column graphic inserted. I want to make the process more automated (check "top" or "bottom" in an existing userform and have the macro place the two-column figure accordingly on the current page).
Again, I am able programmatically move the cursor to the top or bottom of the page just fine, I just can't get the cursor to the end of the first column (the left-hand column on the two-column page). Our code inserts continuous section breaks and works well if it can insert the first of those breaks at the end of the left-hand column of text.
Based in part on CindyM's suggestion, I tried the following for a two-column Visio diagram at the bottom of the current page, but things go wrong after the third line of code. Other than this initial VBA-driven cursor placement problem, the rest of my code works fine (selects the Visio, inserts continuous breaks, inserts a caption for the figure, etc.). Hope this helps clarify. Thanks again!
`ActiveDocument.Bookmarks("\Page") `Select selects current page`
Selection.GoToNext (wdGoToPage) `cursor at top of page 2`
Selection.MoveEnd wdCharacter, -1 `cursor now at end of target page`
Selection.MoveRight Unit:=wdItem `cursor now in headline, top of page :(`
Selection.MoveRight Unit:=wdItem
[1]: https://i.stack.imgur.com/OR7BF.png
As no code was included in the question as a starting point, the following uses GoTo to move to a specific page. The assumption is that this position is not a newspaper column. Then the equivalent of pressing Alt+Down Arrow twice in the UI to move to the next TextColumn (newspaper column) is performed, which would be the beginning of the next column after the first. The selection is then moved back one character, to put it at the end of the column.
If the top of the page is a newspaper column and the code should move to the end of that column, then remove one of the Selection.MoveRight Unit:=wdItem lines.
Sub MoveToEndOfNextNewspaperColumn()
Dim goToPageNr As String
goToPageNr = "1"
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=goToPageNr
Selection.MoveRight Unit:=wdItem
Selection.MoveRight Unit:=wdItem
Selection.MoveEnd Unit:=wdCharacter, Count:=-1
End Sub

In MS-Word how do you VBA split a table inserting a Section Break AND a paragraph mark

wdSectionBreakNextPage does not come with a trailing paragraph mark
I have a massive table i copy from excel to word sprinkled throughout with homemade fields PAGEX and SECTIONX. My macro will change these to page and section breaks, splitting the table for each (what I want). But page breaks come with a trailing paragraph mark before the table and section breaks do not. My feeble attempts to insert a paragraph mark after a section break always ends up with the paragraph mark inside the table.
My goal is to get the same gap between my header and the beginning of the table after both section breaks and page breaks. I think I simply want a macro to insert a paragraph mark after my section breaks similar to the way wdPageBreak works (i.e., between the break and the table). But I'm open to suggestions.
As perhaps further clarification, doing this manually involves creating a new row after a section break, then converting that row to text (quite impractical for the counts I'm looking at).
Do While .Execute
Selection.Delete
Selection.InsertBreak Type:=wdSectionBreakNextPage
Loop
Try:
ActiveDocument.Compatibility(wdSplitPgBreakAndParaMark) = False
thanks free and macro. i probably should have noted that i'm a vba novice (cut-and-paste mostly).
i decided it was easiest to just insert a page break after each section break, and then do a replace of ^m^p by ^p.
this works in my case, but won't work for others reading this if they have existing page breaks they don't want to lose.
GJ

Word Macro Find Last Line of current page

In MS Word using a macro, I would like to find the last line of the current page and add a "page break". Although this should be easy I can't find the script for the last line of the current page.
Sub LastLineInsert()
Selection-end 'is supposed to work to find last line
Selection.InsertBreak Type:=wdPageBreak
EndSub
The easiest way is probably to use Selection.GoToNext(wdGoToPage) and then back up one character. So:
Selection.GoToNext(wdGoToPage)
Selection.MoveEnd wdCharacter, -1
Selection.InsertBreak
Note that doing this where Word has already made a page break can result in a blank page being added. Be careful about this if you're trying to loop.

Delete page in Word

I need VBA code to delete all sections(pages) except first in Word document
For this I use below code.
For Each oSec In ActiveDocument.Sections
If oSec.Index <> 1 Then
oSec.Range.Delete
End If
Next oSec
This works but does not delete second section only removes its content.
If I removes if condition in code it removes content of first page.
I want to preserve content of first page.
Please tell me where I am making mistake.
When deleting you need to include section break marks. Try to change this line:
oSec.Range.Delete
into this one:
ActiveDocument.Range(oSec.Range.Start - 1, oSec.Range.End).Delete
BTW, you should not think that page=section, they are different type of document units.