can't set PageSetup.Orientation = xlLandscape

2019-08-11 18:42发布

Using VBA in MS Project 2003 I create an Excel sheet and write data to it. After that I want to change SetPrintArea and Orientation of the sheet I created so I wrote

with xlsheet                         '// Defined being an Excel.Worksheet
    For i = 1 To .UsedRange.Columns.Count
        .Columns(i).EntireColumn.AutoFit
    Next i
    txtPrintArea = txtPrintArea & "$" & xlCol.Row  '// I created the range to print before
    With .PageSetup
        .Orientation = xlLandscape
        .PrintArea = xlSheet.UsedRange.Address
    End With
End With

It crashes at the .Orientation statement. If I comment that out it crashes at the .PrintArea line. My conclusion is it can't set any property of .PageSetup

How can I specify the PrintArea ?

2条回答
祖国的老花朵
2楼-- · 2019-08-11 18:52

I installed BullZip PDF printer and after that .PageSetup.Orientation works. So it seems PageSetup NEEDS a printer to be installed.

查看更多
看我几分像从前
3楼-- · 2019-08-11 18:56

You are doing the right thing.

Why do you set txtPrintArea but then set the .PrintArea = xlSheet.UsedRange.Address ???

Otherwise you'll need to post more code as an example. I created the following based on your question and it worked for me:

Set xlSheet = Sheet1
Set xlCol = Sheet1.Rows(1)
txtPrintArea = "A"
With xlSheet                         '// Defined being an Excel.Worksheet
    xlSheet.UsedRange.Columns.EntireColumn.AutoFit
    txtPrintArea = txtPrintArea & "$" & Trim(Str(xlCol.Row)) + ":b2"   '// I created the range to print before
    With .PageSetup
        .Orientation = xlLandscape
        .PrintArea = txtPrintArea '//xlSheet.UsedRange.Address
    End With
End With
查看更多
登录 后发表回答