Consulting

Results 1 to 10 of 10

Thread: Changing orientation of word doc from excel

  1. #1
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location

    Changing orientation of word doc from excel

    The awsome DrJ ( Jake) gave me with the code below, what i need to do is open the word doc in landscape instead of portraite

    Any ideas?

    Dim Cel                 As Range
         Dim WS              As Worksheet
         Dim FirstAddress    As String
         Dim AppWord         As Word.Application
         Dim Word            As String
         Dim Prompt As String
         Dim Search As String
    Prompt = "What do you want to search for?"
         Title = "Search Criteria"
         Search = InputBox(Prompt, Title)
         If Search = "" Then
         MsgBox "Nothing Selected"
         End If
    Set AppWord = CreateObject("Word.Application")
         AppWord.Documents.Add
         Set WS = ThisWorkbook.Sheets("Exhibits")
         With WS.Cells
             Set Cel = .Find(What:=Search, LookIn:=xlValues, _
             LookAt:=xlPart, MatchCase:=False)
             If Not Cel Is Nothing Then
                 FirstAddress = Cel.Address
                 Do
                     WS.Range("A" & Cel.Row & ":D" & Cel.Row).Copy
                     AppWord.Selection.Paste
                     Set Cel = .FindNext(Cel)
                 Loop While Not Cel Is Nothing And Cel.Address <> FirstAddress
             End If
         End With
    AppWord.Selection.Find.ClearFormatting
         AppWord.Selection.Find.Replacement.ClearFormatting
    With AppWord.Selection.Find
             .Text = Search
             .Replacement.Font.name = "Arial Black"
             .Replacement.Font.Color = wdColorRed
             .Replacement.Text = ""
             .Forward = True
             .Wrap = wdFindContinue
             .Format = True
             .MatchCase = False
             .MatchWholeWord = False
             .MatchWildcards = False
             .MatchSoundsLike = False
             .MatchAllWordForms = False
         End With
         AppWord.Selection.Find.Execute Replace:=wdReplaceAll
         AppWord.Visible = True

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

    try this:


    With AppWord.PageSetup
    .LineNumbering.Active = False
    .Orientation = wdOrientLandscape   '<- here is what you want. You may delete the rest
    .TopMargin = CentimetersToPoints(3)
    .BottomMargin = CentimetersToPoints(3)
    .LeftMargin = CentimetersToPoints(2.5)
    .RightMargin = CentimetersToPoints(2.5)
    .Gutter = CentimetersToPoints(0)
    .HeaderDistance = CentimetersToPoints(1.25)
    .FooterDistance = CentimetersToPoints(1.25)
    .PageWidth = CentimetersToPoints(29.7)
    .PageHeight = CentimetersToPoints(21)
    .FirstPageTray = wdPrinterDefaultBin
    .OtherPagesTray = wdPrinterDefaultBin
    .SectionStart = wdSectionNewPage
    .OddAndEvenPagesHeaderFooter = False
    .DifferentFirstPageHeaderFooter = False
    .VerticalAlignment = wdAlignVerticalTop
    .SuppressEndnotes = False
    .MirrorMargins = False
    .TwoPagesOnOne = False
    .BookFoldPrinting = False
    .BookFoldRevPrinting = False
    .BookFoldPrintingSheets = 1
    .GutterPos = wdGutterPosLeft
    End With
    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 Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    Tried that i get method or data member not found ???

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Add this:

    Dim Doc             As Document
    Replace this:

    AppWord.Documents.Add
    With this:

    Set Doc = AppWrd.Documents.Add
    Add this:

    Doc.PageSetup.Orientation = wdOrientLandscape

  5. #5
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    Sorry Jake Get an error object required


    Dim Cel             As Range
     Dim WS              As Worksheet
     Dim FirstAddress    As String
     Dim AppWord         As Word.Application
     Dim Word            As String
     Dim Prompt          As String
     Dim Search          As String
     Dim Doc             As Document
    Prompt = "What do you want to search for?"
     Title = "Search Criteria"
     Search = InputBox(Prompt, Title)
     If Search = "" Then
     MsgBox "Nothing Selected"
     End If
    Set AppWord = CreateObject("Word.Application")
     Set Doc = AppWrd.Documents.Add '<<<< Error on this line object required
     Doc.PageSetup.Orientation = wdOrientLandscape
     Set WS = ThisWorkbook.Sheets("Disposal")
     With WS.Cells
     Set Cel = .Find(What:=Search, LookIn:=xlValues, _
     LookAt:=xlPart, MatchCase:=False)
     If Not Cel Is Nothing Then
     FirstAddress = Cel.Address
     Do
     WS.Range("A" & Cel.Row & "" & Cel.Row).Copy
     AppWord.Selection.Paste
     Set Cel = .FindNext(Cel)
     Loop While Not Cel Is Nothing And Cel.Address <> FirstAddress
     End If
     End With
    AppWord.Selection.Find.ClearFormatting
     AppWord.Selection.Find.Replacement.ClearFormatting
    With AppWord.Selection.Find
     .Text = Search
     .Replacement.Font.Name = "Arial Black"
     .Replacement.Font.Color = wdColorRed
     .Replacement.Text = ""
     .Forward = True
     .Wrap = wdFindContinue
     .Format = True
     .MatchCase = False
     .MatchWholeWord = False
     .MatchWildcards = False
     .MatchSoundsLike = False
     .MatchAllWordForms = False
     End With
     AppWord.Selection.Find.Execute Replace:=wdReplaceAll
     AppWord.Visible = True

  6. #6
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Check the variables. One is AppWrd and one is AppWord. Just change them all to match and you should be fine.

  7. #7
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    Removes the error jake but then doesnt find the word

  8. #8
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Why is Cel.Row typed twice here?

    WS.Range("A" & Cel.Row & "" & Cel.Row).Copy
    Shouldnt it be?

    WS.Range("A" & Cel.Row).Copy
    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

  9. #9
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    I think Paleo has it. The line should either be:

    'One Cell
    Range("A" & Cel.Row)
    Or

    'Several Cells
    Range("A" & Cel.Row & ":C" & Cel.Row)

  10. #10
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    That did it cheers to you both

Posting Permissions

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