Consulting

Results 1 to 7 of 7

Thread: pmFooterInsertCurrentDocument()

  1. #1
    VBAX Tutor TheAntiGates's Avatar
    Joined
    Feb 2005
    Location
    Tejas
    Posts
    263
    Location

    pmFooterInsertCurrentDocument()

    I acknowledge great lameness in the [normal.dot] code below and would appreciate y'all giving it a try - if only to tell me that it's totally demented and a more sane solution is available.

    Sorry for the width. I truly worked hard to squeeze it for this post. Also note that the HTM VBA parser inserted 2 or 3 blank spaces in the middle of several keywords in the code[VBA] Sub pmFooterInsertCurrentDocument()
    'Inserts a footer in each section in active document, including the complete path.
    Dim iMsgboxResponse As Integer, iBeenPromptedCount As Integer
    Dim bAlwaysOverwrite As Boolean,fFoo As Single,i As Integer,sstr As String
    'This is no rant. Removing the Gates vars. causes this to fail. Help?
    Dim rngKILLGates As Range,GatesSUCKS1 As Field,GatesSUCKS2 As Field,GatesSUCKS3 As Field
    iBeenPromptedCount = 0: bAlwaysOverwrite = False
    CARR_RET = Chr(10) ' & Chr(13) unnecessary
    With ActiveDocument.PageSetup
    'with "points" (1/72 inch?), footer always takes .2" margin, so consider its excess;
    'Bottom margin should leave .4" to fit our 2-line-footer, for typical font anyway
    fFoo = .FooterDistance - Application.InchesToPoints(0.2)
    If .BottomMargin - IIf(fFoo > 0, fFoo, 0) < Application.InchesToPoints(0.4) Then
    MsgBox "Note-tight squeeze for footer at bottom of page...continuing anyway:" _
    , , "Check bottom of page clearance on tab '" & ActiveDocument.Name & "'"
    End If
    iMsgboxResponse = vbYes ' Deem agreement if there is no msgbox to be presented !!
    For i = ActiveDocument.Sections.Count To 1 Step -1
    sstr = ActiveDocument.Sections(i).Headers(wdHeaderFooterPrimary).Range.Text _
    & ActiveDocument.Sections(i).Footers(wdHeaderFooterPrimary).Range.Text
    If (sstr <> "") And (sstr <> (vbCr & vbCr)) Then
    If iBeenPromptedCount = 1 Then
    iMsgboxResponse=MsgBox("Yet another header or footer already exists." _
    & CARR_RET & CARR_RET _
    & "Do you want to OVERWRITE..ALL..existing footers,without being asked again?", _
    vbYesNo + vbDefaultButton2)
    If iMsgboxResponse = vbYes Then bAlwaysOverwrite = True
    End If
    iBeenPromptedCount = iBeenPromptedCount + 1
    If Not bAlwaysOverwrite Then
    iMsgboxResponse = _
    MsgBox("Header(s) and/or footer(s) already exist(s). Please pick one:" _
    & CARR_RET & CARR_RET _
    & "Yes to OVERWRITE this section's footer;" & CARR_RET _
    & "No or Enter key to skip THIS section and continue," & CARR_RET _
    & "Cancel or Escape key to abort all now?", _
    vbYesNoCancel + vbDefaultButton2, _
    "Already has header or footer in tab '" & ActiveDocument.Name & "'")
    End If
    If iMsgboxResponse = vbCancel Then _
    Application.StatusBar = "Footers cancelled": Exit Sub
    End If
    If iMsgboxResponse = vbYes Then
    'This is no rant. Removing the Gates vars. causes this to fail. Help?
    Set rngKILLGates = Selection.Range
    Set GatesSUCKS1=ActiveDocument.Fields.Add(Range:=Selection.Range,Type:=wdFieldP age)
    Set GatesSUCKS2=ActiveDocument.Fields.Add(Range:=Selection.Range,Type:=wdFieldD ate)
    Set GatesSUCKS3=ActiveDocument.Fields.Add(Range:=Selection.Range,Type:=wdFieldT ime)
    ActiveDocument.Sections(i).Footers(wdHeaderFooterPrimary).Range.Text = _
    "Page " & GatesSUCKS1.Result.Text & " of " & _
    ActiveDocument.BuiltInDocumentProperties(wdPropertyPages) & _
    "; printed: " & " DATE " & " " & GatesSUCKS3.Result.Text _
    & CARR_RET & "(by " & ActiveDocument.BuiltInDocumentProperties _
    (wdPropertyLastAuthor) & " in " & ActiveDocument.FullName _
    & " on " & Date & ")"
    'ActiveDocument.Sections(i).Footers(wdHeaderFooterPrimary).Range.Text= _
    "NO! Not updated"
    On Error Resume Next
    GatesSUCKS1.Delete: GatesSUCKS2.Delete: GatesSUCKS3.Delete
    On Error GoTo 0
    'This adds a right-aligned page number to the primary footer _
    in the section "i" in the active document.
    'ActiveDocument.Sections(i).Footers(wdHeaderFooterPrimary).PageNumbers.Add _
    ' PageNumberAlignment:=wdAlignPageNumberLeft
    End If
    Next i
    End With
    End Sub
    [/VBA]
    I just found a cool semi-advanced VBA page - dictionary, queue, etc. http://analystcave.com/excel-vba-dic...ta-structures/

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Only one doubt.

    If you hate Gates that much why are you still using Office? Why not Open Office and its macro language (basic language)?
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Only idiots even believe in an AntiChrist. Your code demonstrates an incredible misunderstanding of programming, and a amazing lack of understanding of the object model. It looks like some other people's code; those who like to make things look complicated. Why is that?

    I am no fan of Microsoft products, nor their corporate philosophy.

    Your ability to pass on deluded concepts of something like an antichrist into deluded code is, I guess appropriate.

    I suggest your take a look at some design books or sites. I strongly suggest you look at the Word object model. Try writing out what you want to do, step by step. Even more importantly, try to think of WHY you need any code, and WHO is going to be using it. If it is just you, playing around, then making inane messages to yourself is just, shall we say, playing with yourself.

    If it for real users in a real environment, you still need to do good design. Is this going to be for new documents? If so, there should be no existing footers. In which case, you do not need to use Range.Text. You may (or may not) notice that the code below has no reference to the Selection object, including anything like for Gates ranges. Again, I suggest you actual study and think about the object model.

    Other than some format (your right alignment etc.) the following does put the filename and path into every footer, in every section. Here it is in logical chunks.

    For every section
    for every footer in that section
    1. insert filename/path field into Primary
    2. check if FirstPage exists, if so, insert field
    3. check if EvenPages exists, if so, insert field

    There, that so not hard is it? Yes, you can definitely expand this (and you are a decent programmer you will....) to do serious error trapping:
    - is there existing text?
    - is there existing graphics (eithe InLine or otherwise)
    - are their margins issues (is section landscape?)

    RE: your spacing. Ever heard of using the underscore to make new lines? Try it. It works.

    "This is no rant. Removing the Gates vars. causes this to fail. Help?" Get a grip on yourself. You have a choice of variable names, YOU made that choice, and clearly indicates a rant. They are NOT "Gates" variables, they are YOUR variables.

    "We have met the enemy, and it is us."

    But here you go.
    [vba]Sub FilePathFooterEachSection()
    Dim var
    Dim i As Integer
    ' do you really need a bunch of variables?
    '.....gosh......no
    i = 1
    For var = 1 To ActiveDocument.Sections.Count
    With ActiveDocument.Sections(i).Footers
    ' this is Primary and is required
    .Item(1).Range.Fields.Add _
    Range:=.Item(1).Range, _
    Type:=wdFieldEmpty, _
    Text:="FILENAME \p ", _
    PreserveFormatting:=True
    If .Item(wdHeaderFooterFirstPage).Exists Then
    .Item(2).Range.Fields.Add _
    Range:=.Item(2).Range, _
    Type:=wdFieldEmpty, _
    Text:="FILENAME \p ", _
    PreserveFormatting:=True
    End If
    If .Item(wdHeaderFooterEvenPages).Exists Then
    .Item(3).Range.Fields.Add _
    Range:=.Item(3).Range, _
    Type:=wdFieldEmpty, _
    Text:="FILENAME \p ", _
    PreserveFormatting:=True
    End If
    End With
    i = i + 1
    Next var
    End Sub[/vba]

  4. #4
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Thankyou for that Gerry.

    I must admit I took one look at the original posted code and quickly pressed the back button on my browser - yeeugghh!
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  5. #5
    VBAX Tutor TheAntiGates's Avatar
    Joined
    Feb 2005
    Location
    Tejas
    Posts
    263
    Location
    Thank you, Mr. fumei, for your fine effort, and I especially thank the useful and productive contribution by the comebacker. I am short on time but later I will try to embrace the improvements accompanying the "deversitalization" achieved by your code slicing.

    Indeed I have very poor knowledge of the Word object model and posted this to aid my understanding and figuratively cry out for help (and share the routine, or at least the framework, and fix mine up). Perhaps that help's coming at some point. The "Gates" variables demonstrated just how illogically Word VBA behaved and we may never know why it did. Believe me, it was frustrating and I wish they weren't required. That's okay; I knew it was demented and accept that there may be no explanation. Nonetheless, I am very impressed at how you were not intimidated to actually explain the vagaries of the object model in the code; nay, you demonstrated that one can ignore those fallacies by stripping down the code and functionality! Good one! Then buttressed by expression that O.P. is deluded and blames it on Gates! And that he didn't plan and has no structure! Then explaining logical structural chunks like I actually USED - duh. And telling me that I didn't and needed to use underscores! Wow, you scored big.

    I thought that I expressed contrition in the original post and asked for help. I can see that there is value in your post and I'm sure that we'll all benefit. In particular, you actually made good points on how a decent programmer can expand the code. No doubt, there's someone out there (that doesn't suffer from not being laid) that will jump on that idea and expand the code as such. When I have time, I'll take a crack as well. And uhhhh, I believe the message boxes, if statements and indenting make the desired functionality excruciatingly obvious, but maybe my structured programming training was deluded. If anyone else doesn't get it, I'll be happy to explain the "design."

    Oh, and on the deluded structure/design subject, I know you're dying to show off what great textbooks you've already conquered, and you might even know a Zen that I haven't read. Care to share your favorite recommendation (links work too)? We'll all benefit. The webmistress may benefit as well if it's not too overwhelming (She can fight, but she's gonna code, albeit perhaps kicking and...)

    In the spirit of the site I'm through with sarcasm on this now and I'd like to both receive a good routine and share it. Bring on the intelligent and constructive advice.
    I just found a cool semi-advanced VBA page - dictionary, queue, etc. http://analystcave.com/excel-vba-dic...ta-structures/

  6. #6
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    TheAntiGates,

    Quote Originally Posted by TheAntiGates
    ... and I especially thank the useful and productive contribution by the comebacker.
    I have no problem with your handle, but I do have a problem with your apparent attitude. You can write your code whatever way you like at home but posting code full of variables like GatesSUCKS1 demonstrates a remarkable lack of maturity that I consider inappropriate here, as on most other boards on the web. I (and most other board regulars) tend to fly straight on by when faced with a post like yours. Your code looks a mess - OK, many beginners write messy code. Your suggestion that the VBA tags on the site are somehow responsible is a classic case of a bad workman blaming his tools.

    There are some aspects of the Word object model which are difficult and there are some aspects of it which don't work properly; there might even be some aspects of it which are illogical but they are few and if there is something you don't understand, then ask. Far more often than not you will find that it is your understanding that is lacking rather than a problem or illogicality in Word, and if you ask reasonable questions here you should get reasonabe answers. There are many people here who are happy to help but most, like me, do it for nothing other than the reward of seeing others learn and have better things to do than wade through what looks like a teenage rant.

    Quote Originally Posted by TheAntiGates
    In the spirit of the site I'm through with sarcasm on this now and I'd like to both receive a good routine and share it. Bring on the intelligent and constructive advice.
    Now, that's the right attitude! I look forward to your future posts in that vein.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "No doubt, there's someone out there (that doesn't suffer from not being laid) that "

    1. The personal information above is neither amusing, relevant, or accurate. Others may have the same "problem" but it does not affect their posts.

    2. "Contrition?" Hardly. I suggest you look in a good dictionary.

    If you wish to actually write useful VBA, then try and be one of those rare ones who: pause, think, design, think again, weeding out the fluff, design again - all before writing a single line of code. In my mind, the actual writing of code is the least significant part of the process.

    Oh, and on the deluded structure/design subject, I know you're dying to show off what great textbooks you've already conquered, and you might even know a Zen that I haven't read. Care to share your favorite recommendation (links work too)? We'll all benefit. The webmistress may benefit as well if it's not too overwhelming (She can fight, but she's gonna code, albeit perhaps kicking and...)
    1. Zen? Thank you for at least making a smile for me. Does the Word object model have Bhuddha nature?

    2. " I know you are dying to show off what great textooks youve already conquered". You know nothing. In particular, you know nothing about Zen, or manners, or even acting in your own interest. Phrased differently, I would have passed on some recommendations, and would have remained willing to help as much as possible. As it is, ...keep your poor design, sloppy coding. I am going to ignore anything you post, as of now. Mostly because of:

    3. Your comment regarding the webmistress (a terrible term, but I think probably not yours). This is so petty and juvenile that, well, congratulations...I have never come across someone that I was not willing to listen to, to try and assist, and often learn something myself.

    I have now. Post anything you like in response. I do not care, as I have no intention of ever reading anything from you. I hope other will be willing to read and help as much as possible. I shall not be one of them.

Posting Permissions

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