We help IT Professionals succeed at work.

Frame Height... VBA

1,801 Views
Last Modified: 2012-08-14
   For i = 1 To ActiveDocument.Frames.Count
   
        ActiveDocument.Frames(i).Select
       
       ' need to add code here to reduce frame height down to 10, for example
       'but keeping the center from top of the page unchanged.

        ' Also what is the code to read frame height?
        'MsgBox Selection.Borders.Height  '<-- doesn't work

    Next i

Question1: How to reduce frame height to a specified value keeping left to right center line of the frame on the same place.

Question2: What is the syntax to read the height of a frame?

Thank you.
Comment
Watch Question

Commented:
Question #2

Sub FrameInfo()

varHeight = PointsToInches(ActiveDocument.Frames(1).Height)
varWidth = PointsToInches(ActiveDocument.Frames(1).Width)
MsgBox "Height is " & varHeight & " inches"
MsgBox "Width is " & varWidth & " inches"

End Sub
CERTIFIED EXPERT
Top Expert 2008

Commented:
Question #1:

ActiveDocument.Frames(1).Top = ActiveDocument.Frames(1).Top + (ActiveDocument.Frames(1).Height - 10) / 2
ActiveDocument.Frames(1).Height = 10

Kevin

Commented:
Question #1

Sub FrameHeightnTwo()

varHeight = PointsToInches(ActiveDocument.Frames(1).Height)
varNewHeight = ActiveDocument.Frames(1).Height / 2
ActiveDocument.Frames(1).LockAnchor = True
ActiveDocument.Frames(1).Height = varNewHeight

End Sub

Commented:
Just Lock the Anchor ; )
Mike EghtebasDatabase and Application Developer

Author

Commented:
Thank you for the good responses.

Testing PointsToInches(ActiveDocument.Frames(1).Height) on a frame gives me -1.38 E-02, and  ActiveDocument.Frames(1).Height gives me -1

How this is possble? What am I doing wrong? I was expecting a possitive value.

After ironing out this part I will test the solution from Kevin.

Mike
Mike EghtebasDatabase and Application Developer

Author

Commented:
Checking the propery of the frames, I have frame height on Auto with blank "At" box.

Using:    ActiveDocument.Frames(i).Height = 10,

Auto changes to "At Least" with "At" = 1.4

So, in order for them all to have the same height, I will apply

ActiveDocument.Frames(i).LockAnchor = True
ActiveDocument.Frames(i).Height = 10

What do you think?

Mike
CERTIFIED EXPERT
Top Expert 2008

Commented:
>How to reduce frame height to a specified value keeping left to right center line of the frame on the same place.
I think I misunderstood your request. What do you mean by "center line"? In the same place relative to what?

Kevin
Mike EghtebasDatabase and Application Developer

Author

Commented:
Kevin,

My apology for the poor description.
                         
   |----------------------------------
   |                                          |
------- - -------- - ----------- - ----------  <-- left to right center line of the frame
   |                                          |  
   |_____________________|  
       ^--- frame
Mike EghtebasDatabase and Application Developer

Author

Commented:
geneus,

It seems the following line is anchoring the top-left corner not left-center point.

ActiveDocument.Frames(i).LockAnchor = True

     /---  top-left corner
   x----------------------------------
   | /--- left-center point          |
--x---- - -------- - ----------- - ----------  
   |                                          |  
   |_____________________|  
       ^--- frame

Commented:
Sub FrameX()
'

varHeight = PointsToInches(ActiveDocument.Frames(1).Height)
varNewHeight = ActiveDocument.Frames(1).Height / 2
varVertPos = ActiveDocument.Frames(1).VerticalPosition
varNewVertPos = varVertPos + varNewHeight
ActiveDocument.Frames(1).LockAnchor = True
ActiveDocument.Frames(1).Height = varNewHeight
ActiveDocument.Frames(1).VerticalPosition = varNewVertPos


End Sub
Mike EghtebasDatabase and Application Developer

Author

Commented:
geneus,

I have old frame and want to change it to new frame.

   ----------------------------
   |                                  |                   |--------------------------|
--x---- - -------- - --------------             --x---- - -------- - --------------            
   |                                  |                   |________________|
   |_________________|                       ^--- new frame
       ^--- old frame                                     - height = 10
           - any height                                    - center line the
                                                                     same as olf frame
 
I am having some problems with your last post. Now, I am preparing some notes to post it here.

Thanks,

Mike
Mike EghtebasDatabase and Application Developer

Author

Commented:
I ran this code (the same as yours). I have uploaded the properties snapshot of both old and new frames for you to view if necessary.

In these codes I have included some comment and numbers for your information:

======= Your Code ============

' find the height of the frame as it is. (reads varHeight = -1.388889E-02)
varHeight = PointsToInches(ActiveDocument.Frames(intFrmCount).Height)

' determine new half-height for the frame (Note: I wanted it to be 10)
' or varNewHeight = 10/2  ' or just 5
' (reads varNewHeight = -0.5)
varNewHeight = ActiveDocument.Frames(intFrmCount).Height / 2

' find the top position of the frame as it is.  (reads varVertPos = 624.9)
varVertPos = ActiveDocument.Frames(intFrmCount).VerticalPosition

' determine the new top position of the frame
' v-- isn't this relative position of the bottom of the frame?
' (reads varNewVertPos = 624.4)
varNewVertPos = varVertPos + varNewHeight

' anchor the frame (does this mean keep the dimensions
' fixed or anchor it at top-left position for example?)
ActiveDocument.Frames(intFrmCount).LockAnchor = True

' new height is expected to be 10. Could we just write
' ActiveDocument.Frames(1).Height = 10
ActiveDocument.Frames(intFrmCount).Height = varNewHeight

' shift the frame to its new position
ActiveDocument.Frames(intFrmCount).VerticalPosition = varNewVertPos
======= End of the Code ============

I will post the link for the snapshots shortly.

Thank you,

Mike

Commented:
I misunderstood originally.  I thought you wanted the Center line to be the top.  I modified the code you just posted.  You stated that you wanted the Height to be 10 but I was unsure of what unit of measure you wanted to use.  All calculations are based on points.  In my original example I converted the points to inches so that you could see the result.

------------------------------------------------------------------------------------------------------------------------
Sub YourCode()
' find the height of the frame as it is. (reads varHeight = -1.388889E-02)
'Removed PointsToInches as all sizes are calculated in Points this was to demonstrate it in inches
varHeight = ActiveDocument.Frames(1).Height

' determine new half-height for the frame (Note: I wanted it to be 10)
' or varNewHeight = 10/2  ' or just 5
' (reads varNewHeight = -0.5)

varNewHeight = 10 'Set to 10 as specified.   Now if you want this to be 10 inches, change it to
'InchesToPoints (10)
'Removed => ActiveDocument.Frames(1).Height / 2

'Calculate your change in Height
varChangeInHeight = varHeight - varNewHeight

'Calculate the Vertical Shift
varVertShift = varChangeInHeight / 2


'-----This Section may not be necessary--------------------
'Relative Vertical Position
    varRelVPos = ActiveDocument.Frames(1).RelativeVerticalPosition
    If varRelVPos = 0 Then
    varRelVPosSet = wdRelativeVerticalPositionParagraph
    ElseIf varRelVPos = 1 Then
    varRelVPosSet = wdRelativeVerticalPositionPage
    ElseIf varRelVPos = 2 Then
    varRelVPosSet = wdRelativeVerticalPositionParagraph
    End If
'----------------------------------------------------------

' find the top position of the frame as it is.  (reads varVertPos = 624.9)
varVertPos = ActiveDocument.Frames(1).VerticalPosition

' determine the new top position of the frame
' v-- isn't this relative position of the bottom of the frame?
' (reads varNewVertPos = 624.4)
varNewVertPos = varVertPos + varVertShift 'varNewHeight

' anchor the frame (does this mean keep the dimensions
' fixed or anchor it at top-left position for example?)
ActiveDocument.Frames(1).LockAnchor = True

' new height is expected to be 10. Could we just write
' ActiveDocument.Frames(1).Height = 10
ActiveDocument.Frames(1).Height = varNewHeight

' shift the frame to its new position
ActiveDocument.Frames(1).VerticalPosition = varNewVertPos

'Set Relative Vertical Position <-If Other section is omitted then take this out too
ActiveDocument.Frames(1).RelativeVerticalPosition = varRelVPosSet


End Sub
Mike EghtebasDatabase and Application Developer

Author

Commented:
Hi geneus,

Thank you so much for the great code. I has solved most of the problems except one. In the link below, I have included two PNG images with some explanations on them:

https://filedb.experts-exchange.com/incoming/ee-stuff/216-blank.txthttps://filedb.experts-exchange.com/incoming/ee-stuff/4905-FrameProperties.PNG
https://filedb.experts-exchange.com/incoming/ee-stuff/4906-Frames.PNG
https://filedb.experts-exchange.com/incoming/ee-stuff/4909-FrameProperties2.PNG


There is a frame where some codes runs to edit its text content a bit-- removes some chr(13) from the text string inside the frame. After runing this edit plus your code, it doesn't remain at the same location. It moves up.

The text in the other frames are also edited (this is not the only one) but at the end this particular frame moves up considerably while the other frame remain in their oroginal locations. I hope the images tell it all.

Thanks again.

Mike
Mike EghtebasDatabase and Application Developer

Author

Commented:
BTW, I have made the images yesterday with the old solution. The difference with the new solution is the final frame-heights are all 10 but the frame with text moves upward. Now, I will make new images base on your new solution and resubmit them for your review.

Thanks,

Mike
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mike EghtebasDatabase and Application Developer

Author

Commented:
Please see the image based on the last solution at:

https://filedb.experts-exchange.com/incoming/ee-stuff/4909-FrameProperties2.PNG 

Mike
Mike EghtebasDatabase and Application Developer

Author

Commented:
re: > Set your Height to Fixed.

This a document with many frams and it is not practical for me to set them to fixed manually. I will close this question now and post a new question asking for solution to set these properties to fixed etc.

Thank you for the great solutions.

Regards,

Mike
Mike EghtebasDatabase and Application Developer

Author

Commented:
geneus,

If you have the time, I have an easy one at:

https://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_22870338.html

Of course, easy one for you.

BTW, there was some errors in my excution of the code. I will update you shortly.

Thanks,

Mike

Commented:
Sub FrameHeightSet()
'Sets the height to an exact number on all frames in document
For h = 1 To ActiveDocument.Frames.Count
'Change the Height from Auto to Fixed
    If ActiveDocument.Frames(h).HeightRule = 0 Then
        varVPos = ActiveDocument.Frames(h).VerticalPosition
        varLineCount = ActiveDocument.Frames(h).Range.ComputeStatistics(Statistic:=wdStatisticLines)
        varFontSize = Selection.Font.Size
        varCalcHeight = 1.2 * varLineCount * varFontSize
        ActiveDocument.Frames(h).HeightRule = wdFrameExact
        ActiveDocument.Frames(h).Height = varCalcHeight
        ActiveDocument.Frames(h).VerticalPosition = varVPos
    End If
Next h

End Sub

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.