Consulting

Results 1 to 11 of 11

Thread: Code for changing word from portrait to landscape

  1. #1

    Question Code for changing word from portrait to landscape

    Hi All

    I wonder if you can help please, I have some code built in Excel which opens word and copies and pastes some data into a word document. The thing is I need to change the layout in word from portrait to landscape but do not seem to be able to do this, I have used ActiveDocument.PageSetup and the orientation but this seems to come back as an error - any ideas?

    Many Thanks

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Welcome to the board

    Do you create a new word document?

    Have you considered using a template?

    Could you post the code you are using and running from excel?

    When posting code, select it and hit the vba button to format it for the forum.

    You can also attach your excel file by hitting go advanced and scroll down till you see the manage attachments button.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3

    Code for changing word from portrait to landscape Reply to Thread

    Hi, Thanks for your help with this.

    Yes it is a new Word Document, not a template.

    [VBA]Private Sub CmdRepWord_Click()

    'Message Box pops up to advise about re-sizing if needed once in Word
    MsgBox "The Picture of your selection will now appear in MS Word." & vbCrLf & "This is set for the width of the whole Risk Report (A:W)," & vbCrLf & "however you can resize accordingly once within Word, by clicking on the Picture and altering its size."
    Application.ScreenUpdating = False
    Dim ObjWord As Object, WrdDoc As Object
    Set ObjWord = CreateObject("Word.Application")

    ObjWord.Visible = True
    Set WrdDoc = ObjWord.Documents.Add
    Set WrdDoc = ActiveDocument.PageSetup

    'Get the address, or reference, from the RefEdit control.
    Addr = RefEdit1.Value
    If RefEdit1.Value = "" Then MsgBox "Need to Select Data"
    On Error Resume Next

    'Set the SelRange Range object to the range specified in the
    'RefEdit control.
    Set SelRange = Range(Addr)

    'Copy the range as a piicture
    SelRange.CopyPicture , xlPicture

    'Paste
    ObjWord.Selection.PasteAndFormat (wdPasteDefault)

    'Clean Up
    Set WrdDoc = Nothing
    Set ObjWord = Nothing

    'Unload user Form
    Unload Me

    Application.ScreenUpdating = True
    End Sub[/VBA]

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    could you possibly attach your excel file with fake info so I don't have to try to figure out and re-create a userform and controls that you already have in your posession?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    Its quite a large doc . I am thinking however that a template maybe a good option as later I need to have the standard format headers and footers etc that our company uses. Do you therefore know what I would use to ref to a specific template please?

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Here's one way:

    [VBA]dim path as string
    Dim wb As Excel.Workbook
    dim docword as object
    Set wb = ActiveWorkbook
    Path = wb.Path & "\pushmerge.dot"

    Set docWord = pappWord.Documents.Add(Path)
    docWord.Bookmarks("bookmark").Range.Text = your range here[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Here's a KB entry that might help. There are probably more if you search.

    http://www.vbaexpress.com/kb/getarticle.php?kb_id=381
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    OK, Thanks for your help on this - its still not working - it returns the error 424 Object Required on the line

    [VBA]
    Dim Path As String
    Dim ObjWord As Object, WrdDoc As Object
    Set ObjWord = CreateObject("Word.Application")
    Dim pappword As Object
    Path = wb.Path & "\Word Report R&I.doc"

    ObjWord.Visible = True
    [/VBA]

    [VBA]
    Path = wb.Path & "Word Report R&I.doc"
    [/VBA]
    Any suggestions this is sending me CRAZY !!!

  9. #9
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    It doesn't know what wb is.

    you need to add this before the line with the error:

    [vba]
    Dim wb As Excel.Workbook
    Set wb = ActiveWorkbook
    [/vba]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  10. #10
    Thank you so very much - Its all working now

  11. #11
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    If you have your solution, please mark your thread solved using the thread tools at the top of the page.

    That keeps others from reading the entrire thread just to find it's been solved.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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