Word VBA to return the height and width of a preselected image - vba

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.")

Related

Inserting images automatically on slides referencing model #? MICROSOFT POWERPOINT

I have about 100 slides here on powerpoint that look like this:
Slide example screenshot
I need to insert an image into the text box on each slide with the sizing of
height = 5, width = 3.64
and positioning of:
Horizontal = 6" / Vertical = 1.2" (Both from top left corner)
For good measure, here are all of the image format settings:
Image format settings screenshot
The image files are the same as the model number, such as MHS1306.jpg.
Is there any way to use a macro to automatically reference the model number, insert the corresponding image, and then resize it to the correct size?
All of the images are in the same folder.
The image just needs to be in that position, the text box isn't important, just shows where the image should go.
I am very new to VBA and scripts, and basically know nothing about it yet, so I would appreciate any help.
Thanks.

vb.net increase gap between dynamically created textbox conrol

i have dynamically created textbox i want to increase margin between them for, if i increase the font size they will not overlap each other. is there any properties which i can use . i already used
txtbox.top = txtbox.top +5
but it is pushing all text boxes to down i want to increase gap between text boxes
for i = 0 to pnl.controls.count-1
pnl.controls(textboxD & i).top = pnl.controls(textboxD & i).top +5
next
i am using above code for moving toolboxes. thank you in advance

VBA for Word - position text boxes based on page

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

Scrollable image in userform

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

Label position should fixed right and grow to left

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.