1
votes

I have this ridiculous problem on ms-word 2007. I have most of my macro working as intended but the orientation can't seem to stay in place. I set it to landscape using VBA but it will always go back to portrait. If I step into the code right after this line the document IS in landscape but as soon as I click even once only in the document it goes back to portrait.

Do you guys have an idea why this happens ? I can't seem to find anyone having this bug on Google.

Option Explicit

Sub créer_rapport()
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document

    Set wdApp = New Word.Application
    wdApp.Visible = True

    Set wdDoc = wdApp.Documents.Open(Range("path_fichier").Value)

    wdApp.Selection.WholeStory
    wdApp.Selection.Font.Name = "Courier New"
    wdApp.Selection.Font.Size = 7


    wdDoc.PageSetup.Orientation = wdOrientLandscape
    wdDoc.PageSetup.PaperSize = wdPaperLegal
    wdDoc.SaveAs ActiveWorkbook.Path & "\test2", wdFormatXMLDocument

    Do While wdApp.Selection.Find.Execute("Merge")
        wdApp.Selection.MoveUp wdLine, 1
        wdApp.Selection.InsertBreak wdPageBreak
        wdApp.Selection.MoveDown wdLine, 2
    Loop

    With wdDoc
        .SaveAs (ActiveWorkbook.Path & "\test")
        .Close (True)
    End With

    wdApp.Quit False
End Sub

This is all there is to my macro (for now).

(Oh and you can highlight bad style, this is the first time I do VBA macros for Word (I do them all the time in Excel))

Thanks !

1

1 Answers

3
votes

Instead of:

wdDoc.PageSetup.Orientation = wdOrientLandscape

Try this:

Selection.PageSetup.Orientation = wdOrientLandscape