Orientación horizontal en un informe de Access mediante VBA
Estoy intentando cambiar la orientación de vertical a horizontal en un informe de Access mediante VBA, modificando, posteriormente, los márgenes a mi gusto. El motivo es que tengo dos informes idénticos, pero uno en horizontal, y otro en vertical (apaisado), y estoy viendo la forma de juntaros en uno.
He probado con
Me.Printer.Orientation = acPRORLandscape Me.Printer.Orientation = acPRORPortrait
pero no funciona. Buscando por internet no he encontrado más información.
1 respuesta
buscando un poco por internet encontré esto: https://support.office.com/es-es/article/propiedad-prtdevmode-f87eebdc-a13e-484a-83ed-2e2beeb9d699
No lo he probado, pero creo que te servirá.
Hola. Gracias.
A ver, sí me vale, pero me da un error que no sé cómo solucionarlo. Esto es lo que he puesto:
Private Sub acc_AfterUpdate()
Dim MiArgumento As String
Const DM_PORTRAIT = 1
Const DM_LANDSCAPE = 2
Dim DevString As str_DEVMODE
Dim DM As type_DEVMODE
Dim strDevModeExtra As String
Dim rpt As Report
If Informe = 1 Then
MiArgumento = "1" & Me.acc
DoCmd.OpenReport "04-H Gastos anuales mensuales", acViewDesign
Set rpt = Reports("04-H Gastos anuales mensuales")
If Not IsNull(rpt.PrtDevMode) Then
strDevModeExtra = rpt.PrtDevMode
DevString.RGB = strDevModeExtra
LSet DM = DevString
DM.lngFields = DM.lngFields Or DM.intOrientation
' Initialize fields.
If DM.intOrientation = DM_PORTRAIT Then
DM.intOrientation = DM_LANDSCAPE
Else
DM.intOrientation = DM_PORTRAIT
End If
' Update property.
LSet DevString = DM
Mid(strDevModeExtra, 1, 94) = DevString.RGB
rpt.PrtDevMode = strDevModeExtra
End If
Set rpt = Nothing
DoCmd.OpenReport "04-H Gastos anuales mensuales", acViewPreview, , "[Año]='" & Me.acc & "'", , MiArgumento 'Si es texto
End IfAhora bien, me dice esto:

¿Sabes por qué puede ser? ¿No será alguna referencia que me falte?
Saludos.
No es ninguna referencia, lo que pasa es que solo has copiado el segundo código, pero te falta copiar también los dos Private Type que están en el primer código.
Sí. Ya me he dado cuenta de eso. Ese código (los dos Private Type, y el Public Sub SwitchOrient(ByVal strName As String)), ¿dónde debo colocarlos? ¿En el formulario o en el informe?
Supongo que luego tendré que llamar a esa función SwitchOrient de alguna manera, ¿o no? Mi problema es que no sé cómo funciona.
¡Gracias!
Los dos Type los tienes que declarar en el mismo sitio que has pegado el código, para que los tenga accesibles, o bien los puedes poner en un módulo independiente, pero cambiando su ámbito de Private a Public.
A la función no la tienes que llamar de ninguna manera, porque veo que no la has creado, sino que has puesto el código dentro de un evento del formulario. Tan solo pega los type en el mismo módulo del formulario.
Hola. A ver si consigo hacerlo funcionar :). Este es todo el código que tengo en el formulario que abre el informe. He utilizado el primer código, que debería mostrar un mensaje, pero no hace nada. Desconozco qué hago mal.
Option Compare Database
Public cmbAno As New FindAsYouTypeCombo
Private Type str_DEVMODE
RGB As String * 94
End Type
Private Type type_DEVMODE
strDeviceName As String * 32
intSpecVersion As Integer
intDriverVersion As Integer
intSize As Integer
intDriverExtra As Integer
lngFields As Long
intOrientation As Integer
intPaperSize As Integer
intPaperLength As Integer
intPaperWidth As Integer
intScale As Integer
intCopies As Integer
intDefaultSource As Integer
intPrintQuality As Integer
intColor As Integer
intDuplex As Integer
intResolution As Integer
intTTOption As Integer
intCollate As Integer
strFormName As String * 32
lngPad As Long
lngBits As Long
lngPW As Long
lngPH As Long
lngDFI As Long
lngDFr As Long
End Type
Public Sub CheckCustomPage(ByVal rptName As String)
Dim DevString As str_DEVMODE
Dim DM As type_DEVMODE
Dim strDevModeExtra As String
Dim rpt As Report
Dim intResponse As Integer
' Opens report in Design view.
DoCmd.OpenReport rptName, acDesign
Set rpt = Reports(rptName)
If Not IsNull(rpt.PrtDevMode) Then
strDevModeExtra = rpt.PrtDevMode
' Gets current DEVMODE structure.
DevString.RGB = strDevModeExtra
LSet DM = DevString
If DM.intPaperSize = 256 Then
' Display user-defined size.
intResponse = MsgBox("The current custom page size is " & _
DM.intPaperWidth / 254 & " inches wide by " & _
DM.intPaperLength / 254 & " inches long. Do you want " & _
"to change the settings?", vbYesNo + vbQuestion)
Else
' Currently not user-defined.
intResponse = MsgBox("The report does not have a custom page size. " & _
"Do you want to define one?", vbYesNo + vbQuestion)
End If
If intResponse = vbYes Then
' User wants to change settings. Initialize fields.
DM.lngFields = DM.lngFields Or DM.intPaperSize Or _
DM.intPaperLength Or DM.intPaperWidth
' Set custom page.
DM.intPaperSize = 256
' Prompt for length and width.
DM.intPaperLength = InputBox("Please enter page length in inches.") * 254
DM.intPaperWidth = InputBox("Please enter page width in inches.") * 254
' Update property.
LSet DevString = DM
Mid(strDevModeExtra, 1, 94) = DevString.RGB
rpt.PrtDevMode = strDevModeExtra
End If
End If
Set rpt = Nothing
End Sub
Private Sub Form_Load()
cmbAno.InitalizeFilterCombo Me.acc, "Año", FromBeginning, True, True
End Sub
Private Sub acc_AfterUpdate()
Dim MiArgumento As String
If Informe = 1 Then
MiArgumento = "1" & Me.acc
DoCmd.OpenReport "04-H Gastos anuales mensuales", acViewPreview, , "[Año]='" & Me.acc & "'", , MiArgumento 'Si es texto
End If
If Informe = 2 Then
MiArgumento = "1" & Me.acc
DoCmd.OpenReport "04-H Gastos anuales mensuales", acViewPreview, , "[Año]='" & Me.acc & "'", , MiArgumento 'Si es texto
End If
If IsNull(Informe) Then
MsgBox "No has seleccionado qué tipo de informe quieres ver.", vbCritical, "Atención"
End If
DoCmd.Close acForm, Me.Name
End Sub¡Gracias!
Si quieres que te lance el mensaje, pon, por ejemplo un botón, y en el evento "al hacer clic" ponle:
CheckCustomPage “04-H Gastos anuales mensuales”
Claro, a eso me refería. Ahora bien, con la otra, para que lo coja, deberé, creo, poner esto así:
Private Sub acc_AfterUpdate()
Dim MiArgumento As String
If Informe = 1 Then
MiArgumento = "1" & Me.acc
SwitchOrient ("04-H Gastos anuales mensuales")
DoCmd.OpenReport "04-H Gastos anuales mensuales", acViewPreview, , "[Año]='" & Me.acc & "'", , MiArgumento 'Si es texto
End IfLo que pasa es que no me cambia la orientación. ¿A ti se te ocurre otra manera de poder conseguirlo?
¡Gracias!
En este ejemplo hace lo que quiero conseguir. ¿Cómo puedo hacer para adaptarlo a mi caso?
¡¡Lo tengo!!
Así:
Private Sub acc_AfterUpdate()
Dim MiArgumento As String
If Informe = 1 Then
MiArgumento = "1" & Me.acc
DoCmd.OpenReport "04-H Gastos anuales mensuales", acViewPreview, , "[Año]='" & Me.acc & "'", , MiArgumento 'Si es texto
Reports("04-H Gastos anuales mensuales").Printer.Orientation = acPRORPortrait
DoCmd.OpenReport "04-H Gastos anuales mensuales", acViewPreview, , "[Año]='" & Me.acc & "'", , MiArgumento 'Si es texto
End If
End Sub
- Compartir respuesta