Pageorientation and tables

G

gandalf

Hi,
I need a macro to convert, from the cursor location till the end of the document, all the pages from portrait to alndcape (or viceversa) and resize correctly all the tables in the pages.

I wrote something like this:
----------------------------------------
Dim rowRow as Row
Dim cllCell as Cell
Dim lngWidthUsable as Long
Dim lngWidthTable as Long
Dim tblTable as Table

Selection.InsertBreak wdSectionBreakNextPage
Selection.Start = Selection.Start + 1

With Selection.Sections(1).Range.PageSetup
.Orientation = wdOrientlandscape
.SectionStart = wdSectionNewPage
.DifferentFirstPageHeaderFooter = false
End with

With ActiveDocument.PageSetup
lngWidthUsable = .PageWidth - .LeftMargin - .RightMargin
End With

For Each tblTable In Selection.Sections(1).Range.Tables
For Each cllCell in tblTable.Rows(1).Cells
lngWidthTable = lngWidthTable + cllCell.Width
Next cllCell

For Each rowRow in tblTable.Rows
For Each cllCell In rowRow.Cells
cllCell.Width = cllCell.Width * (lngWidthUsable / lngWidthTable)
Next cllCell
Next rowRow
Next tblTable
--------------------------------------------------------------------------

I cannot check if this code makes the job because the .PageWidth and .LeftMargin give me a strange 99999 value (while the .RightMargin gives me the correct value).

Any hint/idea/help/suggetsion/code?

Thanks
 
P

Peter Hewett

Hi =?Utf-8?B?Z2FuZGFsZg==?=

I don't know why you're seeing strange numbers. I'd suggest you write some
code to dump out the 5 metrics (PageWidth, Left, Right, Top and Bottom
margins) you're using to the Immediate window. Do this on a new document,
before you change anything. Just a thought make sure you have a printer
driver selected.

Also, change the Long type declarations to Single.

Sorry that's all for now + Cheers - Peter
 
G

Gandalf

I've done this already and you know what, I get correct numbers until I execute

With Selection.Sections(1).Range.PageSetup
.Orientation = wdOrientlandscape


since now on I start getting 9999999 for width and leftmargin and the correct value for the right margin.
WEIRD!!!!!! Cannot go on without solving this problem...


----- Peter Hewett wrote: -----

I don't know why you're seeing strange numbers. I'd suggest you write some
code to dump out the 5 metrics (PageWidth, Left, Right, Top and Bottom
margins) you're using to the Immediate window. Do this on a new document,
before you change anything.
 
P

Peter Hewett

Hi =?Utf-8?B?R2FuZGFsZg==?=

Try the following code which forces the correct page dimensions and margins
after flipping the page:

Sub PortraitToLandscape()
Dim sngPH As Single
Dim sngPW As Single
Dim sngTM As Single
Dim sngBM As Single
Dim sngLM As Single
Dim sngRM As Single

With Selection.Sections(1).Range.PageSetup
sngPH = .PageHeight
sngPW = .PageWidth
sngTM = .TopMargin
sngBM = .BottomMargin
sngLM = .LeftMargin
sngRM = .RightMargin
.Orientation = wdOrientLandscape
.PageHeight = sngPW
.PageWidth = sngPH
.TopMargin = sngLM
.BottomMargin = sngRM
.LeftMargin = sngBM
.RightMargin = sngTM
End With
End Sub

If the wrong measurements are returned after running the above code I've no
idea what's going on.

Cheers - Peter
 
G

Gandalf

Ok, I got it! I should have understood the problem by myself: if I have a doc with portrait pages and landscape pages I have to use the section's PageSetup.

But now I hae another problem
Look at this code:
-------------------------------------
Sub MyFunction()
...
Dim tblTable As Table

For Each tblTable In Selection.Sections(1).Range.Tables
FitTableToPage (tblTable)
Next tblTable
...
End Sub

Sub FitTableToPage(ByRef rtblTable As Table)
...
...
End Sub
--------------------------------------------------------

Well, the line
FitTableToPage (tblTable)
is giving me a TYPE MISMATCH error!!!!!!
I have to declare rtblTable as VARIANT to have it work
 
P

Peter Hewett

Hi

Why are you surprised? You're calling a procedure (FitTableToPage) you've
defined as a Sub as a Function!

Change:
FitTableToPage (tblTable)
to:
FitTableToPage tblTable

Remember Functions return values Subs don't, hence the syntactical
difference.

Cheers - Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top