首页 > 解决方案 > 保持工作表的页面方向

问题描述

我有一个用于 Microsoft Excel 的 VBA 宏。.xls宏更改文件夹中每个文件的 Headerimage 和 Footercontent 。唯一的问题是页面方向自动设置为横向。我想保持原点的页面方向。这个怎么做?

这是我的宏:

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim ImagePath As String
Dim Validation As String

  ImagePath = "\logo.jpg"
  companyname = "companyname"
  contractnumber = "23-23"

  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual

  Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

    With FldrPicker
      .Title = "Select A Target Folder"
      .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
    End With

NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings

  myExtension = "*.xls*"

  myFile = Dir(myPath & myExtension)

  Do While myFile <> ""
      Set wb = Workbooks.Open(Filename:=myPath & myFile)
    
      DoEvents
    
      On Error Resume Next
        Validation = Dir(ImagePath)
      On Error GoTo 0
    
      If Validation = "" Then
        MsgBox "Pfad zu Logo falsch: " & ImagePath
        Exit Sub
      End If

      For Each sheet In ActiveWorkbook.Worksheets
        sheet.activate
          sheet.PageSetup.LeftHeader = "&G"
          sheet.PageSetup.LeftHeaderPicture.Filename = ImagePath
          sheet.PageSetup.LeftFooter = "&[Datei] © " + companyname + ", Vertragsnummer: " + contractnumber
          sheet.DisplayPageBreaks = False
    
      Next sheet
    
      wb.Close SaveChanges:=True
      
      DoEvents

      myFile = Dir
  Loop

'Message Box
  MsgBox "Done"

ResetSettings:
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub

标签: excelvba

解决方案


看来您可以先获取工作表的方向值,然后再将其设置为方向。

Sub test()
    Dim Ws As Worksheet
    Dim i As Integer
    
    Set Ws = ActiveSheet
    
    With Ws.PageSetup
        i = .Orientation
        .Orientation = i
    End With
    MsgBox i
End Sub

推荐阅读