Consulting

Page 3 of 3 FirstFirst 1 2 3
Results 41 to 55 of 55

Thread: Help with VBA

  1. #41
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    I have no idea what happened to my last post - there was a whole lot more there after I posted it. Trying again.

    Replace:
    [VBA]'Call ReplicateLayout(wdDoc.Sections(i), ActiveDocument.Sections(i + 1))
    For Each HdFt In .Sections(i).Headers
    With ActiveDocument.Sections(i + 1)
    If .Headers(HdFt.Index).Exists Then
    .Headers(HdFt.Index).Range.Copy
    HdFt.Range.PasteAndFormat Type:=wdFormatOriginalFormatting
    .Headers(HdFt.Index).Range.Characters.Last.Delete
    End If
    End With
    Next
    For Each HdFt In .Sections(i).Footers
    With ActiveDocument.Sections(i + 1)
    If .Footers(HdFt.Index).Exists Then
    .Headers(HdFt.Index).Range.Copy
    HdFt.Range.PasteAndFormat Type:=wdFormatOriginalFormatting
    .Footers(HdFt.Index).Range.Characters.Last.Delete
    End If
    End With
    Next [/VBA]
    with:
    [VBA]'Call ReplicateLayout(.Sections(i), Rng.Sections(i))
    ' Replicate the last Section's headers & footers.
    For Each HdFt In .Sections(i).Headers
    With Rng.Sections(i).Headers(HdFt.Index)
    If .Exists Then
    HdFt.Range.Copy
    .Range.PasteAndFormat Type:=wdFormatOriginalFormatting
    .Range.Characters.Last.Delete
    End If
    End With
    Next
    For Each HdFt In .Sections(i).Footers
    With Rng.Sections(i).Footers(HdFt.Index)
    If .Exists Then
    HdFt.Range.Copy
    .Range.PasteAndFormat Type:=wdFormatOriginalFormatting
    .Range.Characters.Last.Delete
    End If
    End With
    Next[/VBA]
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  2. #42
    Doesn't work. I tried it several times. It deletes all headers from both section 1 and section 2.

    Here is the code I used.

    [VBA] Application.ScreenUpdating = False
    Dim wdDoc As Document, strFile As String, Rng As Range, HdFt As HeaderFooter, i As Long
    'strFile = "C:\Users\Macropod\Documents\Attachments\Faces.docx"
    strFile = "C:\Documents and Settings\jingo\My Documents\lopsy.docx"
    With ActiveDocument
    'If there is more than one Section, unlink the headers & footers from the 1st Section
    ' so that the new Section's content won't impact subsequent Sections
    If .Sections.Count > 1 Then
    With .Sections(2)
    For Each HdFt In .Headers
    HdFt.LinkToPrevious = False
    Next
    For Each HdFt In .Footers
    HdFt.LinkToPrevious = False
    Next
    End With
    End If
    Set Rng = .Sections(1).Range
    With Rng
    'If there's already a Section break, move back one character
    If Asc(.Characters.Last) = 12 Then
    .End = .End - 1
    End If
    'Ensure there's an empty paragraph for the new Section.
    While Asc(.Characters.Last.Previous) <> 13
    .InsertAfter vbCr
    Wend
    .MoveEnd wdCharacter, -1
    .Collapse wdCollapseEnd
    End With
    'Add the new Section
    .Sections.Add Range:=Rng, Start:=wdSectionNewPage
    ' Unlink the headers & footers from the 1st Section
    ' so that the new Section's content won't impact first Section
    With .Sections(2)
    'Insert the new content, retaining its formatting
    Set wdDoc = Documents.Open(FileName:=strFile, AddToRecentFiles:=False, Visible:=False)
    For Each HdFt In .Headers
    HdFt.LinkToPrevious = False
    Next
    For Each HdFt In .Footers
    HdFt.LinkToPrevious = False
    Next
    Set Rng = .Range
    Rng.Collapse wdCollapseStart
    With wdDoc
    .Range.Copy
    Rng.PasteAndFormat Type:=wdFormatOriginalFormatting
    i = .Sections.Count
    'Call ReplicateLayout(wdDoc.Sections(i), ActiveDocument.Sections(i + 1))
    For Each HdFt In .Sections(i).Headers
    With Rng.Sections(i).Headers(HdFt.Index)
    If .Exists Then
    HdFt.Range.Copy
    .Range.PasteAndFormat Type:=wdFormatOriginalFormatting
    .Range.Characters.Last.Delete
    End If
    End With
    Next
    For Each HdFt In .Sections(i).Footers
    With Rng.Sections(i).Footers(HdFt.Index)
    If .Exists Then
    HdFt.Range.Copy
    .Range.PasteAndFormat Type:=wdFormatOriginalFormatting
    .Range.Characters.Last.Delete
    End If
    End With
    Next
    .Close SaveChanges:=False
    End With
    End With
    End With
    Set wdDoc = Nothing: Set Rng = Nothing
    Application.ScreenUpdating = True
    End Sub[/VBA]

  3. #43
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Have fun Paul.

  4. #44
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Zack
    Doesn't work. I tried it several times. It deletes all headers from both section 1 and section 2.
    Not when I use it ...

    When I use it, even on a document that already has two or more Sections, the newly-inserted Section 2's content is completely independent of the Sections before and after. All of the preceding and following content is preserved.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #45
    Quote Originally Posted by macropod
    Not when I use it ...

    When I use it, even on a document that already has two or more Sections, the newly-inserted Section 2's content is completely independent of the Sections before and after. All of the preceding and following content is preserved.
    I tried it again. It deletes header from all pages, both sections.
    I am using exactly what I posted here.

  6. #46
    Basically, this code deletes the header from the section 2, but keeps the style from the section 1.

    [VBA]With Selection
    .EndKey Unit:=wdStory
    .InsertBreak Type:=wdSectionBreakNextPage
    .InsertFile FileName:="C:\Documents and Settings\lorien\My Documents\zen.doc", Range:=""

    End With

    Dim oSec As Section
    Dim sPrintCode As String


    For Each oSec In ActiveDocument.Sections
    sPrintCode = sPrintCode & oSec.Range.Information(wdActiveEndPageNumber) & ","
    Next


    sPrintCode = Left(sPrintCode, Len(sPrintCode) - 1)


    ActivePrinter = "Jingo2"
    Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, Item:= _
    wdPrintDocumentContent, Copies:=1, Pages:=sPrintCode, PageType:= _
    wdPrintAllPages, ManualDuplexPrint:=False, Collate:=True, Background:= _
    True, PrintToFile:=False, PrintZoomColumn:=0, PrintZoomRow:=0, _
    PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0



    End Sub [/VBA]

    Since this code does the job well, is there a way to incorporate saving style of section 2 (inserted document) in it?

  7. #47
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    This is just the same thing you asked at the start, over and over again.

    And you have been answered, over and over again.

    You are abusing this forum. Smarten up or I will formally ask that you be removed.

  8. #48
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Zack
    I tried it again. It deletes header from all pages, both sections.
    I am using exactly what I posted here.
    I ran the code from your post with my data and the headers were preserved.

    The problem is your's. As I said:
    When I use it, even on a document that already has two or more Sections, the newly-inserted Section 2's content is completely independent of the Sections before and after. All of the preceding and following content is preserved.
    As far as I am concerned, you're a waste of time. Aside from anything else, we've at least proved that your previous post about:
    obligated to .InsertFile method for the reasons which I am not allowed to talk about
    was indeed crap. The code works for everything you have specified - and more. I'm done with this.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #49
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    No, not just smarten up. You need to apologize. In fact, if you do not apologize FIRST - no other postings about how something is not working - then I will ask for you to be removed.
    Last edited by fumei; 09-21-2012 at 05:18 PM.

  10. #50
    Quote Originally Posted by fumei
    No, not just smarten up. You need to apologize. In fact, if you do not apologize FIRST - no other postings about how something is not working - then I will ask for you to be removed.
    Apologize in your stead for not bringing solution to this issue? I am not that benevolent..However, it seems that your fragile ego has been hurt, thus I understand your desire of "removing" a user from this forum. Natural response of a lesser man..

    Best regards.

  11. #51
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Apologize in your stead for not bringing solution to this issue?
    Except for a simple fact...we DID bring a solution. You were warned.

  12. #52
    Quote Originally Posted by fumei
    Except for a simple fact...we DID bring a solution. You were warned.
    No you didn't. The code does not work. I have no reason to lie.

    Warned about being honest? Wonderful..

  13. #53
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Warned about being honest? No, I did no such thing. I warned you about your behaviour. The fact that you make that comment just demonstrates once again you do not get it.

    Everyone else: I have formally asked that Zack be removed from the forum. I think that any other postings to these threads are pointless.

  14. #54
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    I was actually going to chime in and say it's not worth your time, Fumei. New user names can be created. No need to get heated about it... the person posting under the username Zack is either a) a troll, b) feels entitled to free help for some reason c) has some kind of language barrier thing going on and it's all a big misunderstanding.

    Personality types A and B aren't worth engaging any more. Personality type C can simply try again in a new thread. But the question of this thread has been asked and answered as well as it could have been, much like the other thread.

    Can lead a horse to water and all that

  15. #55
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Sorry, but I do not think a, b, OR c applies.

    a) troll. Perhaps, but I an inclined to think not. I think Zack truly wants an answer. He is simply not willing to work with the forum, or work with himself to see the answer.

    b) No, well maybe yes, in that he does not want to pay anything. The point is that he is getting free help. He is simply not willing to work with the forum, or work with himself to see the answer.

    c) language. I doubt this very much - as I have covered in another thread "Detecting last page VBA". Zack uses phrases like "Nah, mate". That thread ALSO caused much frustration (and not just with me).

    Time? It took 10 seconds to PM the admin to look at this. Far less than the time we have wasted trying to help.

    Heated? No, not really. This site has helped and assisted many people, and I have been here from the very beginning. I have a fondness for it. I am simply trying to keep it clean.

    Can someone get a new name and try again? Yes I suppose. But I seriously doubt someone can hide their character - and yes, I do think it is a reflection of character. Unless they altered their behaviour significantly (in which case they are very welcome), they will be recognized.

    Look, just about all of the major contributors to the forum have expressed frustration and annoyance with how this is going, not just me. "I am done here" has been stated over again, and not just me.

    I am (was?) not looking for an abject apology. I just want a recognition, from the OP, that they are the primary cause of the problem. That when we ask for a response to a simple question - did you try this? - ignoring that IS a primary cause of the problem. Repeatedly parroting back the same thing over and over again does not lead to a solution. That stating this does not work (with no explanation or information) does not lead to a solution.

    "Horse to water and all that". Yes, precisely. And yes, true, the solution to a horse refusing to drink when it is in front of water, is to walk away and let the darn horse either drink, or not. BUT... if that horse is blocking others from the water, or that horse is sucking up resources (I.e. the people leading it to water over and over again), then it needs to be dealt with.

    Obscure reference: title of early Jane Fonda movie...

Posting Permissions

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