Results 1 to 7 of 7
  1. #1
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: header and footers in word (vba/word)

    Hello Sheila,

    Welcome to Woody's Lounge. What is your question? And which version of Word is this about (you mention Windows 95, but that is the operating system)?

  2. #2
    Lounger
    Join Date
    May 2003
    Location
    cork, Ireland, Republic of
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    header and footers in word (vba/word)

    Oops I managed to delete my question when i edited it.......... <img src=/S/confused.gif border=0 alt=confused width=15 height=20>
    This was the question:

    I'm new to VB and have inherited some code to change. All I need to do is to add a Date_String on the first page only footer of a word doc. I''ve managed to get the Date_String (which has to be generated in different part of the code) printing on every page footer but unfortunarely this won't do. I've tried to control the footer code with a boolean and with a page number but this didn't work. Its a bit brute force anyway. The docs can be of various lengths. I've seen range and wdstory but don't know how to incorporate it into the code. The header has to print on every page. I tried using DifferentFirstPageHeaderFooter but this caused the header not to print which won't do.

    This is the code Im using:

    Public Sub Print_ticket(lot_num As String, Ticket As String, Date_String As String)

    Active_Word.Documents.Open (Ticket & ".doc")
    Active_Word.Visible = True
    Active_Word.Activate
    If Active_Word.ActiveWindow.View.SplitSpecial <> wdPaneNone Then
    Active_Word.ActiveWindow.Panes(2).Close
    End If
    If Active_Word.ActiveWindow.ActivePane.View.Type = wdNormalView Or _
    Active_Word.ActiveWindow.ActivePane.View.Type = wdOutlineView Or _
    Active_Word.ActiveWindow.ActivePane.View.Type = wdMasterView Then
    Active_Word.ActiveWindow.ActivePane.View.Type = wdPageView
    End If


    Active_Word.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Active_Word.ActiveWindow.View.SeekView = wdSeekCurrentPageHeader
    Active_Word.ActiveWindow.Selection.Find.Text = "LOT NUMBER:"
    Active_Word.ActiveWindow.Selection.Find.Replacemen t.Text = "LOT NUMBER:" & Chr(9) & lot_num
    Active_Word.ActiveWindow.Selection.Find.Execute "LOT NUMBER:", False, , , , , , , , "LOT NUMBER:" & Chr(9) & lot_num, True '


    '************************************************* ************************************************** ********************
    'A & B print datestring on footer of every page********************************************** ***********************
    'a) Active_Word.ActiveDocument.Sections(1).Footers(wdH eaderFooterPrimary).Range.Text = "Date & Time Printed: " & Date_String
    'B)'Active_Word.ActiveWindow.ActivePane.View.SeekV iew = wdSeekCurrentPageFooter
    'Active_Word.ActiveWindow.View.SeekView = wdSeekCurrentPageFooter
    'Active_Word.ActiveWindow.ActivePane.Selection.Tex t = "Date & Time Printed: " & Date_String
    '************************************************* ************************************************** ********************
    'prints date string on footer of page 1 only but causes the header code not to run
    'With Active_Word.ActiveDocument
    ' .PageSetup.DifferentFirstPageHeaderFooter = True
    ' .Sections(1).Footers(wdHeaderFooterFirstPage) _
    ' .Range.InsertBefore _
    ' "Date & Time Printed: " & Date_String
    'End With
    '************************************************* ************************************************** ********************
    Active_Word.ActiveWindow.View.SeekView = wdSeekMainDocument

    BackGroundOption = Active_Word.Options.PrintBackground
    Active_Word.Options.PrintBackground = True
    Active_Word.Application.PrintOut
    Do While Active_Word.BackgroundPrintingStatus = 0
    DoEvents
    Loop
    Do While Active_Word.BackgroundPrintingStatus = 1
    DoEvents
    Loop
    Active_Word.Options.PrintBackground = BackGroundOption
    Active_Word.Documents.Close wdDoNotSaveChanges

    'Active_Word.Application.Quit wdDoNotSaveChanges
    Me.SetFocus
    End Sub


    I'd really appreciate any help
    Thanks in advance
    Sheila (I hope I was clear in what I need help with!!)

  3. #3
    Lounger
    Join Date
    May 2003
    Location
    cork, Ireland, Republic of
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: header and footers in word (vba/word)

    Thanks for replying so quickly hans. Sorry for my non clarity - I think VB has killed the last few brain cells that I had !!!!!! I hope my edit explained things a bit better..........

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: header and footers in word (vba/word)

    Hello Sheila,

    Try code like this - it is not the complete macro, only the bit that sets the headers and footers, and you will probably have to tweak it to get it right, but it does set the headers for all pages and the footer for the first page only in a test I did. The code sets header and footer text for the first section in the document.

    <img src=/w3timages/blueline.gif width=33% height=2>

    Dim oDoc As Word.Document
    Dim oSec As Word.Section

    Set oDoc = Active_Word.Documents.Open(Ticket & ".doc")
    Set oSec = oDoc.Sections(1)

    oSec.PageSetup.DifferentFirstPageHeaderFooter = True

    ' Set header for first page and for subsequent pages
    oSec.Headers(wdHeaderFooterFirstPage).Range.Text = vbTab & "LOT NUMBER " & lot_num
    oSec.Headers(wdHeaderFooterPrimary).Range.Text = vbTab & "LOT NUMBER " & lot_num

    ' Set footer for first page, clear footer for subsequent pages
    oSec.Footers(wdHeaderFooterFirstPage).Range.Text = vbTab & "Date & Time Printed: " & Date_String
    oSec.Footers(wdHeaderFooterPrimary).Range.Delete

    <img src=/w3timages/blueline.gif width=33% height=2>

    As you see, this code doesn't actually switch to the header or footer; instead, it uses Ranges to manipulate the text. This is usually more efficient that setting the Selection to enter or modify text. I used the symbolic constant vbTab instead of Chr$(9).

    See if you can adapt this to your needs. Post back if you need more assistance.

  5. #5
    Lounger
    Join Date
    May 2003
    Location
    cork, Ireland, Republic of
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: header and footers in word (vba/word)

    Hi Hans
    Thanks very much for your help. Your code worked very well but there is still a problem - my fault; I didn't mention this - in each document header there is static information such as document master date, a QC no , no of pages etc which all has to be retained. This means that adding the lot number to the header has to be done by find & replace as Lot Number is part of the header & this needs to be replaced with Lot Number &lot_num. As there are hundreds of Word docs that this code will be accessing, it would be a mammoth task to change all docs so the code has to make the changes.

    Any further help would be greatly appreciated and thanks a million for the code you've given me so far.

    Sheila

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: header and footers in word (vba/word)

    Find is a method of a range object as well as of Selection, so it should be possible to find and replace text in the header or footer by applying your original code to a header or footer range.

  7. #7
    Lounger
    Join Date
    May 2003
    Location
    cork, Ireland, Republic of
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: header and footers in word (vba/word)

    Hiya Hans
    Im still tweaking code but fortunately its time to go home so I can look forward to being driven crazy tomorrow as well!!!! You've been brilliant (& very patient) Thanks very much - I owe you a couple of beers (or whatever your tipple is 8- )

    regards
    Sheila

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •