2015-07-01 14 views
0

在我的宏中,我想提示頁面方向 - P或P爲縱向,L或L爲橫向。字Vba輸入框設置文檔方向 - 如果與或

這是我開發的。它的工作原理 - 但想知道是否有更好/更有效的開發方式If語句爲「OR」---我必須使用「嵌套」如果??

Dim i As Integer 
    Dim oTbl As Table 
    Dim rng As Range 
    Dim Orientation As String 

    With Selection.PageSetup 
     .LineNumbering.Active = False 
     .Orientation = wdOrientPortrait 
     .TopMargin = InchesToPoints(1) 
     .BottomMargin = InchesToPoints(1) 
     .LeftMargin = InchesToPoints(0.4) 
     .RightMargin = InchesToPoints(0.4) 
     .Gutter = InchesToPoints(0) 
     .HeaderDistance = InchesToPoints(0.5) 
     .FooterDistance = InchesToPoints(0.6) 
     .PageWidth = InchesToPoints(8.5) 
     .PageHeight = InchesToPoints(11) 
     .FirstPageTray = wdPrinterDefaultBin 
     .OtherPagesTray = wdPrinterDefaultBin 
     .SectionStart = wdSectionNewPage 
     .OddAndEvenPagesHeaderFooter = False 
     .DifferentFirstPageHeaderFooter = False 
     .VerticalAlignment = wdAlignVerticalTop 
     .SuppressEndnotes = False 
     .MirrorMargins = False 
     .TwoPagesOnOne = False 
     .BookFoldPrinting = False 
     .BookFoldRevPrinting = False 
     .BookFoldPrintingSheets = 1 
     .GutterPos = wdGutterPosLeft 
    End With 


' set Orientation 


    Orientation = InputBox(Prompt:="Enter P for Portriait, L for Landscape") 

    If Orientation = "L" Then 
     With Selection.PageSetup 
       .Orientation = wdOrientLandscape 
     End With 
    ElseIf Orientation = "l" Then 
     With Selection.PageSetup 
       .Orientation = wdOrientLandscape 
     End With 
    Else 
     With Selection.PageSetup 
       .Orientation = wdOrientPortrait 
     End With 
    End If 

回答

0

我不知道有沒有更好的(更有效)的方式來開發的MS Word頁面方向......我不會用InputBox,但如果你想使用它,我會做這樣的事情:

AskAgain: 
    Orientation = InputBox(Prompt:="Enter P for Portriait, L for Landscape") 

    Select Case UCase(Orientation) 
     Case "L" 
      Selection.PageSetup.Orientation = wdOrientLandscape 
     Case "P" 
      Selection.PageSetup.Orientation = wdOrientPortrait 
     Case Else 
      GoTo AskAgain 
    End Select 

說實話......把決定留給用戶,不要阻止用戶從VBA代碼設置頁面方向,而是調用打印預覽窗口。這是最好的方式 - 在我看來。

+0

我感謝你的回答---但如果用戶輸入小寫而不是大寫?這是否意味着我需要有4個CASE語句或4個IF語句? – user3264432

+0

查看'Select Case'語句...我使用返回大寫字母的'UCase()'函數。所以你不需要使用4個if語句。 –

相關問題