Consulting

Page 1 of 3 1 2 3 LastLast
Results 1 to 20 of 55

Thread: Help with VBA

  1. #1

    Help with VBA

    [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]

    What it does basically is that it creates a new section in the document and adds zen.doc to the first page of the 2'nd section.

    Now the trick is, how to implement something in this code in order to change the font, alignment, etc of the 2'nd section before the printing occurs, but without affecting the 1'st section and the content of it?

    For example: default font and size is Arial 10 on both the main document and zen.doc. I need this code to run and change font to Arial 20 and only for the zen. doc, before the print. How to make changes that will affect everything on the section 2, but Nothing on the section 1 and to implement it within this code?

    Thank you.

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Learn to use Styles properly. If you have a style - say Section2Style

    [vba]ActiveDocument.Sections2).Range.Style="Section2Style"[/vba]

    Done.

    If you only have two Sections, why are you using For Each????

    Why use a For Each (even if you have 1,000 Sections) at all???

  3. #3
    I understand.
    Thank you fumei.

    How to implement changing font of the section2 Before the printing.

    Where would I implement this code of yours in my code above to achieve that.

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    [vba]With Selection
    .EndKey Unit:=wdStory
    .InsertBreak Type:=wdSectionBreakNextPage
    .InsertFile FileName:="C:\Documents and Settings\lorien\My Documents\zen.doc", Range:=""

    End With
    ActiveDocument.Sections2).Range.Style="Section2Style" [/vba]I am not sure what you are trying to do with the Print code.

  5. #5
    Quote Originally Posted by fumei
    [vba]With Selection
    .EndKey Unit:=wdStory
    .InsertBreak Type:=wdSectionBreakNextPage
    .InsertFile FileName:="C:\Documents and Settings\lorien\My Documents\zen.doc", Range:=""

    End With
    ActiveDocument.Sections2).Range.Style="Section2Style" [/vba]
    That's it. It will basically print section 2 free of section's 1 format.

    Thx

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Only if it HAS a different format. Using a SAtyle will do that.

  7. #7
    Quote Originally Posted by fumei
    [vba]With Selection
    .EndKey Unit:=wdStory
    .InsertBreak Type:=wdSectionBreakNextPage
    .InsertFile FileName:="C:\Documents and Settings\lorien\My Documents\zen.doc", Range:=""

    End With
    ActiveDocument.Sections2).Range.Style="Section2Style" [/vba]
    Doesn't work / Compile error, Syntax error

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

    ActiveDocument.Sections2).Range.Style="Section2Style"

    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]

    Compile error, syntax error

  9. #9
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    ActiveDocument.Sections(2)
    Will refer to section 2.
    _______________________________________________
    Please don't cross-post without providing links to your cross-posts. We answer questions for free. Please don't waste the time of the people helping you.
    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

    - Frosty

  10. #10
    Quote Originally Posted by Frosty
    ActiveDocument.Sections(2)
    Will refer to section 2.

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

    ActiveDocument.Sections(2).Range.Style="Section2Style"

    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]

    Run-time error 5834

  11. #11
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Do you actually HAVE a style Section2Style?

  12. #12
    Quote Originally Posted by fumei
    Do you actually HAVE a style Section2Style?
    Section 2 style is the style of the document zen.doc, meaning that it has different font size, alignment etc. compared to section 1. Thus it has to keep it rather than just assuming the Section's 1 font, alignment etc.

  13. #13
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Think.

    Does it in fact keep that style? Clearly it does not, otherwise you would not be having this problem.

    So..........

  14. #14
    Quote Originally Posted by fumei
    Think.

    Does it in fact keep that style? Clearly it does not, otherwise you would not be having this problem.

    So..........
    It keeps the style from the section 1, for some reason..
    I need section 2 clear of that

  15. #15
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    1. It keeps the style from the section 1, for some reason..

    Word always does that. Pasting takes on format of the document it is pasted into.

    2. I need section 2 clear of that

    Then do as I have suggested. Make it that style.

  16. #16
    Quote Originally Posted by fumei
    1. It keeps the style from the section 1, for some reason..

    Word always does that. Pasting takes on format of the document it is pasted into.

    2. I need section 2 clear of that

    Then do as I have suggested. Make it that style.
    I tried your code, but it doesn't work..

    Basic idea was to copy zen.doc into the original doc without it assuming original docs style.

  17. #17
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Instead of using Insertfile, have you considered copy & paste? Check out the different paste options - one of them retains the source formatting.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  18. #18
    Quote Originally Posted by macropod
    Instead of using Insertfile, have you considered copy & paste? Check out the different paste options - one of them retains the source formatting.
    Unfortunately I need it to work with the code I've posted.

    If it's impossible it's ok.

  19. #19
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    What? But the code you've posted does *not* work. So that will be impossible.

    Do you mean that you are obligated to the .InsertFile method for some reason, despite an alternative method being easier to implement your specifications?

  20. #20
    Quote Originally Posted by Frosty
    What? But the code you've posted does *not* work. So that will be impossible.

    Do you mean that you are obligated to the .InsertFile method for some reason, despite an alternative method being easier to implement your specifications?
    I am obligated to .InsertFile method for the reasons which I am not allowed to talk about.

    Fumei said:

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

    End With
    ActiveDocument.Sections(2).Range.Style="Section2Style" [/VBA]

    So .InsertFile method was calculated within it. Unfortunately it does not work.

    Is there a way to enable this code?

Posting Permissions

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