简体   繁体   中英

I want to set the page orientation to landscape on word using vba from excel

I think this would be an easy soultion, but I can't find the way.....

So the problem is this, I have an excel document with different sheets, but in just one sheet I want to create a table, after that (I can do it), that table will paste on a new word document.

In this new word document I want to set the orientation to landscape, but obviously I don't want to make it manual, so when I use the.PageSetUp or.Orientation syntaxis, they give me an error.

I tried using With.WordApp.PageSetUp.Orientation = xlLandscape or wdLandscape but I can't.

So there is a way that I can open a new word document, and automatically set the orientation to Landscape and after that paste the selection from excel to that page. And In this new document I tried to set up the Margins, and same case. .LeftMargin = CentimetersToPoints(1), but the error appears.

Option Explicit
Sub Imprimir()
    
Dim WordApp As Object
Dim f, ff As Date
Dim s, qty As Integer
Dim NoEncontrado As Boolean
Dim doc As Documents

Sheets("Entrega").Select
f = Sheets("Entrega").Range("D1").Value
qty = 1
s = 35
NoEncontrado = True
    Do While NoEncontrado = True
    
        Do While qty < 30
            Sheets("Concentrado").Select
            Cells(s, 7).Select
            ff = Sheets("Concentrado").Cells(s, 7).Value
                
                If f = ff Then
                Sheets("Concentrado").Select
                Sheets("Concentrado").Cells(s, 3).EntireRow.Select
                selection.Copy
                Sheets("Entrega").Select
                ActiveSheet.Range("A4").Select
                selection.PasteSpecial Paste:=xlPasteValues
                qty = qty + 1
                s = s - 1
                Rows("4:4").Select
                selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
                                
                Else
                qty = qty + 1
                s = s - 1
                
                End If
        Loop
        
        NoEncontrado = False
    Loop

Set WordApp = CreateObject("Word.Application")
Sheets("Entrega").Select
ActiveSheet.Range("G1").Select
Range("D3:S35").Select
selection.Copy
MsgBox (" Entrega de guardia del día " & f & " lista para imprimir"), vbInformation
With WordApp 'Con este codigo se abrira Word y se creara un documento nuevo
.Visible = True
.Activate
.Documents.Add
End With

WordApp.selection.PasteSpecial link:=True 'Se pegara en el documento lo seleccionado en la hoja de calculo
selection.pagesetup.Orientation = wdOrientLandscape
Set WordApp = Nothing
Sheets("Entrega").Select
Range("A4:CA34").Select
selection.ClearContents
End Sub

You can solve the problem yourself if you get your fingers off the keyboard, lean back and try to understand what's going on. Think about the Selection object.

Something you select is a part of what you see on the screen. On the screen you see a Window. Even if you see several windows on your screen, only one of them can be active. And only the active one can have a selection. Remember this connection: Window -> Application -> Document -> Selection. Since you can't have more than one application in any window you also can't have simultaneous selections in Word and Excel. Create one and you lose the other.

Selecting things is something the user needs but VBA doesn't. Workbooks("MyWorkbook").Sheet1.Range("A1:B3") enables VBA to find the exact range you are talking about. You can read the Value of any of its cells or change it, assign a Font or Colour - do anything you want with it, including Copy to the Clipboard. Observe then: What's the Selection object to the user is the Range object to VBA. Never select anything unless you want to show it to the user.

It's always good practise to assign objects to named variables. When you work with more than one application it's a must . Bear in mind that a Range in Word is a different animal from a Range in Excel. You need to declare them as Excel.Range and Word.Range respectively. VBA won't demand that distinction from you and will use your variables correctly most of the time. If you rely on it your code will work exactly like that - most of the time. And, in passing, Dim f, ff As Date declares f As Variant (by default since nothing is mentioned) and only ff As Date . I recommend only one declaration per line.

What is Orientation a property of? It's the property of a Word.Document (not Documents - plural - as you code has it). More precisely, it's a property of the PageSetup object of the document. Hence Doc.PageSetup.Orientation . Think about it: How could the PageSetup object be a property of any Selection ? The Selection is a visualised Range . It can't have an orientation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM