After adding a large number (150?) of callouts (simple text boxes in the margin, with no borders) to a Word 2010 .docx file with mirrored odd/even pages, I needed to adjust some text. This shortened the document enough to remove a page, and therefore all the odd pages following the adjustment became even, and the even became odd. The text boxes did not move, and now they all fall off the edge of the paper, where it is difficult even to find them all due to the text outside the bounds of the paper not being shown in the document, let alone to move them to their correct positions in the opposite margin. Moreover, I may need to adjust text again and the problem could recur.
I'm looking for an automated way, using VBA (in which I am decidedly a rank novice, though an expert programmer), to record some macro to say, "Center all existing text boxes in the wide margin, and make the width consistent." Something like,
As a text box, if I find myself on an odd page, make the left edge of the text box 0.5" from the left edge of the page; if I am found on an even page, make the left edge of the text box at the right margin + 0.5". My height should be consistent, and my height can stay what it was already.
I need to know in what units to express the Top and Width values, how to determine if I am on an odd/even page, and how to find all the text boxes.
Can anyone help?
Sub AdjustTextBoxes()
Dim myTextBox As Shape
Dim PageNumber As Integer
For Each myTextBox In ActiveDocument.Shapes
If myTextBox.Type = msoTextBox Then
myTextBox.Select
PageNumber = Selection.Information(wdActiveEndPageNumber)
'Mod returns the remainder (rounded up) of the division.
'No remainder is even. Any remainder is an odd page.
If PageNumber Mod 2 = 1 Then
With myTextBox 'Odd numbered pages
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.Left = InchesToPoints(0.5)
End With
Else
'Because wdRelativeHorizontalPositonPage measures from the left
'edge of the page the positioning for text boxes on even pages
'is not based on the distance from the right margin.
'So below .Left = 8.5" (page width) - (1" (textbox with) + .5" (distance
'from the right edge of the page). The measurement for .left below
'will need to be altered based on the position from the right edge
'of the page and the width of your text boxes and pages.
With myTextBox 'Even numbered pages
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.Left = InchesToPoints(7)
End With
End If
End If
Next
End Sub
Related
I have a Word macro that changes the orientation and page size of an individual page to accommodate the image placed on the selected page. My existing macro code does the page and footer resizing correctly (footers are based on our existing footer styles), but I'd like to enhance the code to display the height and width of the selected image before the userform appears to help guide the user's page size choice.
The user could get that information by right-clicking the image, choosing "Picture...", and selecting the "Size" tab in the "Format Object" window, but I am trying to have the height/width displayed as part of the macro instead of asking the user to follow those steps.
Editors in my department sometimes encounter documents in which an image has been placed on a page that is not wide enough to accommodate the entire image, resulting in part of the image being "cut off." My plan has been to have the user select the image on the Word page, then run the macro; the image's height and width (in inches, ideally) should be displayed prior to the user to selecting the optimal page size.
I've tried working with Selection.ShapeRange or Selection.InlineShapes, but I haven't yet been able to get the current height and width of the selected image. Any help will be greatly appreciated.
Thank you,
KRS
Here's how I opted to handle this:
My macro was designed to start with the user selecting an inline shape on the Word page. With that selection, I start by setting variables for the selection's width and height in points, convert to inches, and display the results in a MsgBox:
myHeightPoints = .Height
myWidthPoints = .Width
`Convert the previously established variable to inches`
`Dim myHeightInches As Double
Dim myWidthInches As Double
myHeightInches = myHeightPoints / Application.InchesToPoints(1)
myWidthInches = myWidthPoints / Application.InchesToPoints(1)
`Trim the result to 2 decimal places`
Dim myHeightDisplay
Dim myWidthDisplay
myHeightDisplay = Format(myHeightInches, "#.00")
myWidthDisplay = Format(myWidthInches, "#.00")
`Display the result`
MsgBox ("The selected image is " & myHeightDisplay & " H X " & myWidthDisplay
& " W" & Chr(13) & "Choose a page size that accomodates the figure and its caption.")
Is it possible to have a report with flexible text width and height? I sometimes have two words in this text and some times hundreds. I want to have small text for the first and big text for the second. How do I do that?
I would advise you to set a your text box size to what you think is optimum and use the CanShrink and CanGrow properties (tap the text box and then open the properties windows and you can find them there).
The CanGrow property indicates whether the size of the text box can increase vertically according to its content. Similarly, CanShrink decreases the height of the text box according to its content. Here is a link for better understanding these two properties.
Use the Detail_Format event.
It fires before each line, and you can change the formatting based on the length of the text.
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Len(Field1) < 10 Then
txtField1.FontSize = 18
Else
txtField1.FontSize = 12
End If
End Sub
I have a word doc with a bunch of ActiveX Control buttons or whatever on it, and each time a button is clicked, a corresponding image needs to be displayed in a popup box.
I have a userform called ImageForm, and this is what I'm doing right now:
Sub Button_Clicked()
ImageForm.Picture = LoadPicture("appropriate_image_path")
ImageForm.Show
End Sub
Each of these images has a width of 8.5 inches, but their heights can vary anywhere from like 3 to 20 inches (they're snippets of a pdf). So I've set the width of the userform to a little more than 8.5 inches, and that looks fine. But I need to be able to scroll vertically through the image in the userform, since some of the images could be taller than a user's monitor.
I'm completely stuck on this. What I've tried so far is adding a frame to the form, then adding an image control inside the form, and setting the "ScrollBars" property of the frame to vertical. Then instead of using "ImageForm.Picture = ..." I use "ImageForm.ImageControl.Picture = ..." But it doesn't work.
Any insight here would be greatly appreciated. Hopefully this question is clear enough, I've only been using VBA for a month or so now. (I miss Java so, so much)
Thanks!
Here is a neat little trick based on one of my posts
The idea is to ensure that the image control is in frame control and the image control doesn't have a border. Also the image control's PictureSizeMode is set to fmPictureSizeModeClip so that we can scroll the image
SNAPSHOT (DESIGN TIME)
SNAPSHOT (RUN TIME)
CODE
Private Sub UserForm_Initialize()
With Frame1
'~~> This will create a vertical scrollbar
.ScrollBars = fmScrollBarsVertical
'~~> Change the values of 2 as Per your requirements
.ScrollHeight = .InsideHeight * 2
.ScrollWidth = .InsideWidth * 9
End With
With Image1
.Picture = LoadPicture("C:\Users\Public\Pictures\Sample Pictures\Desert.jpg")
.BorderStyle = fmBorderStyleNone
.PictureSizeMode = fmPictureSizeModeClip
End With
End Sub
I am creating a PPT Add-In and I am struggling to find the answer whether it is possible to determine the SlideRange position on a screen.
I would like to have a userform to be opened in a particular position (for example left = 10, top = 10 starting from left-top corner of the SlideRange). Unfortunatelly its position is based on a screen resolution. As the resolution can be changed and the PPT view type may be modified I am unable to establish the accurate position of SlideRange.
Is it possible to do with VBA?
Thanks in advance!
MJ
SlideRange has no particular position ... it's a collection of slides, and it's unclear what units you're using when you want to position the form at 10,10. But in normal full screen view, you can get the slide show window coordinates in points like so:
With SlideShowWindows(1)
Debug.Print .Left
Debug.Print .Top
Debug.Print .Height
Debug.Print .Width
End With
To get the results in screen pixels you have to use a Win API call to get the screen DPI (dots per inch).
Inches = Points / 72
Pixels = Inches * DPI
<< Do you know if it is possible to catch mouse events in PPT?
Only if the mouseclick changes the selection, and as you know, that's NOT all the time.
There's probably some way of doing it via the Win API but nothing like this is built into PPT itself.
How can I set my labels to align on the right even when they have diffrent lenghts.
I have a set of labels which are occuring next to each other and also underneath each other.
The problem now is that they always align from the left within the label row,but I need them to align on the right as they are showing sums from other rows. Just to verify I am not talking about the text align I am looking for a solution to align my labels.
Thanks in advance
Simply set the AutoSize property to False in the designer. Adjust the size to fit the column. Then set the TextAlign to one of the right-alignment ones.
You should be able to do it at runtime using the following code:
'find the current right alignment position
Dim rightAlign As Integer = Label1.Left + Label1.Width
'set the text (assumes AutoSize is set to True)
Label1.Text = value
'adjust position so the right hand point is in the same position as before
Label1.Left = rightAlign - Label1.Width
My method is even more strange. I create the labels and then when laying out the fields for the report adjust the labels for number (etc) that are to be right aligned
Note: all labels end with 'lbl'
- txtNew is the report column text box.
- get the column's left edge plus the width of the column minus the width of the label. Works! Just not my favorite way to do it.
' *** NEED TO CALC POSITION FOR RIGHT JUSTIFY OF LABEL !!!!!
If ShouldRightJustify(rs.Fields(i).Type) Then
rpt.Section(acPageHeader).Controls(rs.Fields(i).Name & "lbl").Left = _
(lblCol + txtNew.Width) _
- rpt.Section(acPageHeader).Controls(rs.Fields(i).Name & "lbl").Width
End If
If you are asking how to do this from the designer, use the Format Menu.
Select all the controls you want to align, then click the control you want the other aligned to. Do Format > Align > Rights.
If you are trying to do this at run-time you can loop through the controls you want to align and set their .X property according to their width. For example. To align a label so that it's right side is at X=200... SomeLabel.X = 200 - SomeLabel.Width.