PDA

View Full Version : Help with old Header change code batch of files



DeanosBeano
12-16-2013, 01:30 PM
Hi all
I have been using this piece of code for a whil but come across a problem now that i want to use it on files that have more than 1 page

Cant post links yet uinfortunately :(

Code

Sub ReplaceJustLogo()
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("C:\temp\*.doc")
Do While (fName <> "")
With wrd
'Change the directory to YOUR folder's path
.Documents.Open ("C:\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
If .Selection.InlineShapes.Count <> 0 Then .Selection.InlineShapes(1).Delete
.Selection.Paste
.ActiveDocument.Save
.ActiveDocument.Close
End With
fName = Dir
Loop
Set wd = Nothing
End Sub

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("C:\temp\*.doc")
Do While (fName <> "")
With wrd
'Change the directory to YOUR folder's path
.Documents.Open ("C:\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


I have tried to contact the author but he doesnt seem to be about much anymore or recently .

Basicly what i am looking for is an enhncement of the code that will change the header foreach page in the file that it has open and then continue the loop foreach file in the folder .
In addition i would also like to add another Macro for the footer , would this be simply a case of changing the
wdSeekCurrentPageHeader to Footer ?

Many thanks in advance

SamT
12-16-2013, 05:45 PM
On the face of it, those codes won't work because
.Documents.Open ("C:\Temp\" & fName) Evaluates to
.Documents.Open ("C:\Temp\C:\temp\*.doc") I stopped looking at that point.

Who is the original author, anyway?

Can you show us the actual code that you have been using for a while?

fumei
12-16-2013, 09:11 PM
There are bunch of things that seem in error.

1. you have wrd as the Word application, but you destroy it with wd (not wrd)
2. you have .Selection.Paste, but there is nothing in Selection in the code
3. as Sam posted the string fname will not work with the DIR function as it is here. You need to separate the path part of the filename from the filename itself.
4. to get all headers in all files, use headerfooter objects within section objects. Like this:


Sub ReplaceJustLogo()
Dim wrd As Word.Application
Dim path As String
Dim fName
Dim oHF As HeaderFooter
Dim oSection As Section
Set wrd = CreateObject("word.application")
path = "C:\temp\"
fName = Dir(path & "*.doc")
Do While fName <> ""
With wrd
.Documents.Open FileName:=path & fName
For Each oSection In ActiveDocument.Sections
For Each oHF In oSection.Headers
If oHF.Range.InlineShapes.Count <> 0 Then
oHF.InlineShapes(1).Delete
' nothing to indicate anything has been copied
Selection.Paste
End If
Next
Next
ActiveDocument.Save
ActiveDocument.Close
End With
fName = Dir()
Loop
Set wrd = Nothing
End Sub

fumei
12-16-2013, 09:13 PM
BTW, I take it you are running this from another Office app (Excel maybe). You do need to have a reference to the Word library, for this to work as early binding.

DeanosBeano
12-17-2013, 01:55 AM
Hi all, thanks for the replies , I have edited this response because things Develop as always , basicly the original code works flawlessly on *.docm ,what i hadnt relayed was that its necessary to first copy the Header from the source file with the code attached or the Logo and then run the code to the trusted folder with the docs to be changed.So what i would like help on is , the files i will change headers on are multi page and unforutnately they have section breaks , so the code changes the header of the first page and any headers Linked , however it does not go on to change the pages in the document thereafter which are section breaks (terminology may be off here ). Fumei , i tried your code thanks , obviously i now have cleared up my requirements for help this may change your ideas. Many thanks in advance

fumei
12-17-2013, 06:33 PM
My code will delete InlineShapes from every header in every section in every document. No matter how many pages. Mind you it only delete the first InlineShape, so if there is more than one in any given header they will not be deleted.

cupidsrose
12-24-2013, 12:40 AM
hello everyone

fumei
12-24-2013, 02:44 PM
hello