PDA

View Full Version : Header alignment pls help



Monk3
12-19-2010, 03:46 PM
Hi I needed to do a batch image change in the header for 648 docx files. I got this code from this site and it worked great! Trouble is that it is aligning the header image centered when I need it right aligned. Can any one help with this... should be easy but after a lot of looking I can't find it and my coding days are long long gone. Here is the code:

Sub ReplaceEntireHdr()
Dim wrd As Word.Application
Set wrd = CreateObject("word.application")
wrd.Visible = True
AppActivate wrd.Name
'Change the directory to YOUR folder's path
FName = Dir("h:\Temp\*.docx")
Do While (FName <> "")
With wrd
'Change the directory to YOUR folder's path
.Documents.Open ("H:\Temp\" & FName)
If .ActiveWindow.View.SplitSpecial = wdPaneNone Then
.ActiveWindow.ActivePane.View.Type = wdPrintView
Else
.ActiveWindow.View.Type = wdPrintView
End If
.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
.Selection.WholeStory
.Selection.Paste
.ActiveDocument.Save
.ActiveDocument.Close
End With
FName = Dir
Loop
Set wrd = Nothing
End Sub

Thanks for any help!

macropod
12-19-2010, 09:40 PM
Hi Monk3,

There's nothing specific in that code to manage the pasted data's alignment. Indeed, there's nothing in it to say how the data being pasted have been copied. So, how are you copying the data and have you checked the :
. alignment; and
. Style definition,
of the copied range against the corresponding attributes of the destination range?

Also, from where are you running this code? There's a lot of overhead in it that is unnecessary if you're running it from within Word.

Finally, when posting code, please embed it in VBA tags (per the VBA icon on the message panel).

fumei
12-20-2010, 10:59 AM
Agreed. We need a bit more information.

What is the purpose of:AppActivate wrd.Name

It is quite likely that you do not need to use ActivePane.View. You can do anything you want with the Range of the header. You also do not state WHICH header.

As for alignment, either have a right-aligned style in place, or apply, or use in the following example, an AutoText with everything already in place.

Sub ReplaceEntireHdr()
Dim wrd As Word.Application
Dim wrdDoc As Word.Document
Dim path As String
Dim file

Set wrd = CreateObject("Word.Application")
wrd.Visible = True
AppActivate wrd.Name
path = "h:\Temp\"
file = Dir(path & "*.docx")
Do While file <> ""
Set wrdDoc = wrd.Documents.Open(path & file)
With wrdDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range
.Delete
.Text = "blah"
.InsertAutoText
End With
With wrdDoc
.Save
.Close
End With
Set wrdDoc = Nothing
file = Dir()
Loop
Set wrd = Nothing
End Sub
The AutoText is named "blah". It has the image, and all formatting including alignment already in place. All Primary headers are entirely replaced for all files.