views:

351

answers:

2

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 ?

A: 

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
Christian Payne
Same problem : crash at .Orientation = xlLandscape txtPrintArea has not been used (exchanged by UsedRange) for testing (avoid POSSIBLE problem because of wrong definition of txtPrintArea)'More code'?? Don't know what you are missing (of course xlSheet and xlCol have been set before using them) , it is about not being able to set properties of xlSheet.PageSetup
Were you able to run my example above? If so, then I would then reduce your code to the bare essentials. What happens if you run: xlsheet.PageSetup.Orientation = xlLandscape ???
Christian Payne
I've not been able to run your code (crashes). Also xlsheet.PageSetup.Orientation = xlLandscape doesn't make a difference
??? Where does it crash?
Christian Payne
A: 

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

jackrnl