Results 1 to 22 of 22

Thread: FOoter Looping Program

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    19

    FOoter Looping Program

    I have the following Code: and I am getting a Select Case Compilation error that I can't figure out.. I tried adding an "End Select" for my second case.... etc.

    Sub MacroUpdateFooter()
    '
    ' MacroUpdateFooter Macro
    ' Macro recorded 7/28/2006 by Blue2man
    '

    Dim tmp As String
    Dim ext As String
    Dim XLS As Variant
    Dim sPath As String

    sPath = "c:\baseline\"

    tmp = Dir(sPath & "*.*")
    Do While tmp > ""
    ext = LCase(Right(tmp, 3))
    Select Case ext

    Case "xls"
    Set XLS = New Application
    Set oBook = XLS.Workbooks.Open(sPath & tmp)
    For Each oSheet In oBook.Worksheets
    With oSheet.PageSetup
    .PrintArea = ""
    .LeftFooter = "&D"
    .CenterFooter = "&Z&F"
    .RightFooter = "&P"
    End With
    Next oSheet
    oBook.Save
    Set oBook = Nothing
    XLS.Quit
    Set XLS = Nothing

    Select Case WRD
    Case "doc"
    Set DOC = New Application
    WRD.Documents.Open sPath & tmp
    With Documents.PageSetup
    .Print Area = ""
    .LeftFooter = "&Z&F"
    'WRD.ActiveDocument.Save
    'RD.Quit
    'Set WRD = Nothing
    'Case "ppt"

    ' Case "???" ' visio

    End Select
    tmp = Dir
    Loop
    MsgBox "Job Complete"

    End Sub

    Now When compiling.. VB highlights ENd Select (4th to last line)
    and says its an End Select without a Select Case...

    Thanks.

  2. #2
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: FOoter Looping Program

    You have an extra Select Case in there. It should look kind of like this:
    VB Code:
    1. Sub MacroUpdateFooter()
    2.    
    3.     Dim tmp As String
    4.     Dim ext As String
    5.     Dim XLS As Variant
    6.     Dim sPath As String
    7.    
    8.     sPath = "c:\baseline\"
    9.    
    10.     tmp = Dir(sPath & "*.*")
    11.     Do While tmp > ""
    12.         ext = LCase(Right(tmp, 3))
    13.         Select Case ext
    14.             Case "xls"
    15.                 Set XLS = New Application
    16.                 Set oBook = XLS.Workbooks.Open(sPath & tmp)
    17.                 For Each oSheet In oBook.Worksheets
    18.                     With oSheet.PageSetup
    19.                         .PrintArea = ""
    20.                         .LeftFooter = "&D"
    21.                         .CenterFooter = "&Z&F"
    22.                         .RightFooter = "&P"
    23.                     End With
    24.                 Next oSheet
    25.                 oBook.Save
    26.                 Set oBook = Nothing
    27.                 XLS.Quit
    28.                 Set XLS = Nothing
    29.             Case "doc"
    30.                 Set DOC = New Application
    31.                 WRD.Documents.Open sPath & tmp
    32.                 With Documents.PageSetup
    33.                 .Print Area = ""
    34.                 .LeftFooter = "&Z&F"
    35.                 'WRD.ActiveDocument.Save
    36.                 'RD.Quit
    37.                 'Set WRD = Nothing
    38.             Case "ppt"
    39.                
    40.             Case "???" ' visio
    41.            
    42.         End Select
    43.         tmp = Dir
    44.     Loop
    45.     MsgBox "Job Complete"
    46.  
    47. End Sub
    BTW, please indent your code and use the VBCode tags. It's a lot easier to read that way, and in this case, would have found your problem for you.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    19

    Re: FOoter Looping Program

    I tried compiling without the 2nd select case... and I get the same error:
    "End Select without Select Case"

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: FOoter Looping Program

    There is no "End With" to match up to the "With Documents.PageSetup".

    (VB gets confused in situations like this by End If/End With/End Select/...)

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: FOoter Looping Program

    if you indent your code as the examples it will make it much easier to find coding errors

    pete

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    19

    Re: FOoter Looping Program

    Case "doc"
    Case "doc"
    Set WRD = New Application
    WRD.Documents.Open sPath & tmp
    With Documents.PageSetup
    .Print Area = ""
    .LeftFooter = "&Z&F"
    End With

    It's giving me a runtime error 438

    Object doesn't support this property or method.. and it highlights
    WRD.Documents.Open sPath & tmp

    Also, I am not sure whether I am supposed to
    Put Set WRD = New Word. Application
    Regardless it gives me a compilation error.

    Thanks for your help.

  7. #7
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: FOoter Looping Program

    This should be right:
    VB Code:
    1. Set WRD = New Word.Application
    What compile error does that give? BTW, make sure you have a reference to the Word object library and have dimensioned WRD as a Word.Application. You also need to make sure that there is a "." between the path and the extension.

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: FOoter Looping Program

    VB Code:
    1. Dim WRD As Word.Application
    2. Set WRD = New Word.Application
    3. spath = "C:\My Documents\"
    4. tmp = "test.doc"
    5. WRD.Documents.Open (spath & tmp)
    plus you must also set a reference to microsoft word

    pete

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    19

    Re: FOoter Looping Program

    The problem is that VB isn't recognizing the object word... It isn't specifically defined in the code... so how do i go about referencing to Microsoft Word as you say...
    thanks.

  10. #10
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: FOoter Looping Program

    If I remember correctly, you're working in Excel VBA. Go to Tools--->References, and check Microsoft Word in the list. You'll also have to do this for PowerPoint, Visio, etc.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    19

    Re: FOoter Looping Program

    I referenced Microsoft word and even started a new Macro with the following case for word files...
    Private Sub Workbook_Open()

    Dim WRD As Word.Application

    Set WRD = New Word.Application
    WRD.Documents.Open ("c:\Baseline\EC - JIT TRAINING - July 25 2005 - v1.0.doc")

    WRD.ActiveDocument.PageSetup
    With WRD.ActiveDocument.PageSetup
    .LeftFooter = "&Z&F"
    End With
    WRD.ActiveDocument.Save
    RD.Quit
    Set WRD = Nothing
    End Sub


    And I get an invalid property use message for the ActiveDocument.PageSetup code...
    So Essentiallly Excel is still having trouble opening the Word Document..
    Any suggestions?

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: FOoter Looping Program

    Remove the following line (as it isnt valid):
    VB Code:
    1. WRD.ActiveDocument.PageSetup

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    19

    Re: FOoter Looping Program

    K thanks,
    Is there a better way to setup footers in Word other then identifying exactly where tehy go.. perhaps something like
    .footer = " " Where in the quotes i would have a string with &Z&F and etc with the necessar spaces and indentations between the components of the footer,.

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: FOoter Looping Program

    I can't remember how to set the footer in Word (LeftFooter and similar do not work), but the text you need (as with doing it manually) is a tab character, eg:
    VB Code:
    1. ??? = "Left" & vbTab & "Center" & vbTab & "Right"

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    19

    Re: FOoter Looping Program

    Does anyone know how to set the footer in Word using Macros?
    Thanks.

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    19

    Re: FOoter Looping Program

    Btw, for this program to work on a whole drive i would need to implement recursion. I am sure this has been talked about before this forum.. can anyone link me there.

  17. #17
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: FOoter Looping Program

    VB Code:
    1. Sub foot()
    2.  
    3. Dim f As HeaderFooter, s As Section
    4. For Each s In Documents(1).Sections
    5.     For Each f In s.Footers
    6.         f.Range = "well well it worked"
    7.     Next f
    8. Next s
    9.  
    10. End Sub
    there are 3 footers in the footers collection of each section of each document, so you will have to work out which one you want, one (footers(1) or f.index = 1) is the normal footer, one the first page footer if it is different and the third is for even pages
    Last edited by westconn1; Aug 3rd, 2006 at 07:53 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  18. #18
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: FOoter Looping Program

    Btw, for this program to work on a whole drive i would need to implement recursion. I am sure this has been talked about before this forum.. can anyone link me there
    have a look at this thread for code by vbasicgirl to find all files in folder and subfolder, using api call, change *.* to *.Doc or whatever and implement you code in place of List1.AddItem spath & sdir
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    19

    Re: FOoter Looping Program

    What is the f and s in your explanations. Are those new variables.. I wasn't able to dim it.

  20. #20
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: FOoter Looping Program

    you will have to use the full object name to dim them

    maybe dim s as WRD.section, f as WRD.headerfooter
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    19

    Re: FOoter Looping Program

    Dim footer As WRD.headerfooter ... is not a valid statement.. this has to be a variant or a string or whatever the Macros alows it to be.
    Thanks.

  22. #22
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: FOoter Looping Program

    Change WRD.?? in westconn1's example to Word.??

Posting Permissions

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



Click Here to Expand Forum to Full Width