The word document have many pictures. The pictures should show one pre page. Normally I just need to select them all to insert. All of them will occupy in good order one picture per page.
However, sometimes, word overlay them together. Resulting only one page with many pictures stacking up each other.
I find some VBA script that change the scale of pictures. I want to change that to disable overlap of pictures or change the wrap settings of them.
Dim i As Long
With ActiveDocument
For i = 1 To .InlineShapes.Count
With .InlineShapes(i)
.ScaleHeight = 50
.ScaleWidth = 50
End With
Next i
End With
How to change the above script to disable them overlay each other ?
As John says:
If pictures are stacked or overlapping, they are not inline with
text.
You can correct that with code like:
With ActiveDocument
Do While .Shapes.Count > 0
.Shapes(1).ConvertToInlineShape
Loop
End With
Related
I am coding using PowerPoint VBA and am trying to place text inside a rectangle shape, but ensure that the text fits (so there is no overflowing). I do not want the shape itself to resize, but the text to resize.
I have seen that I can use
oShp.TextFrame2.AutoSize = msoAutoSizeTextToFitShape
However, the problem with this is that the text will only resize after the user has clicked on the textbox when PowerPoint is in normal mode. I want this functionality when the PowerPoint is running!
I would be grateful to know is there a way to get the text automatically resized or do I need to find an alternative method?
Thank you for any comments!
I thought I would answer my question and close the thread.
After doing much research I found that there was no apparent method to get the text to auto-resize itself when the PowerPoint Show runs. I tried a number of approaches e.g. inserting text, trimming the text and turning word wrap off and on - however, none of these worked. I note (Bhavesh) I was fully aware of how to select the auto-size text settings via PowerPoint's GUI.
In the end my solution was to make a do loop and change the size of the text.
Below I pasted my key lines in the hope that it might help someone else who is trying to do the same. I made a variable overflow which attempts to assess if the height of the shape's textbox is bigger than the size of the rectangle.
Dim overflow As Integer
Dim counter As Integer
counter = 0
With ActivePresentation.Slides(i).Shapes(stringToTest)
overflow = CInt((.TextFrame.TextRange.BoundHeight) - (.Height - .TextFrame.MarginTop - .TextFrame.MarginBottom))
Do While overflow > 16 And counter < 50
'*** I note that the shape is overflowing when its value is >0 but I found 16 to be the most "aesthetically pleasing" number!
'*** Also using a counter prevents the code from potentially getting stuck in an infinite loop
If .TextFrame.TextRange.Font.Size > 20 Then
.TextFrame.TextRange.Font.Size = .TextFrame.TextRange.Font.Size - 1
Else
.TextFrame.TextRange.Font.Size = .TextFrame.TextRange.Font.Size - 0.5
End If
'**** By reducing the font size by 0.5 this provided a better fit for the text _
'**** (even better than using on PowerPoint's auto-size function!)
counter = counter + 1
overflow = CInt((.TextFrame.TextRange.BoundHeight) - (.Height - .TextFrame.MarginTop - .TextFrame.MarginBottom))
Loop
End With
In shape format, under text options, choose the option to shrink text on overflow.
Then, using .Shapes("Title 1").TextFrame.TextRange we input text via VBA.
The text automatically changes its font size.
I am trying to write a script to autofit tables first to content, then to window n Word 2010, as long as they are at least a certain width.
When I manually autofit to content then to window, the tables look nice and evenly spaced, however if I ONLY autofit to window they tend to have really wide first columns and squished up other columns.
code:
Sub AutoFitTables()
If ActiveDocument.Tables.Count > 0 Then
Dim objTable As Object
Application.Browser.Target = wdBrowseTable
For Each objTable In ActiveDocument.Tables
' Check the table width
If objTable.Columns.Width > 11520 Then
objTable.AutoFitBehavior (wdAutoFitContent)
' Again only fit tables above a certain width
If objTable.Columns.Width > 11520 Then
objTable.AutoFitBehavior (wdAutoFitWindow)
End If
End If
Next
End If
End Sub
issue:
The result is the same as when I manually ONLY auto fit to window. It's like it ignores the autofit to contents first
Is there something that I'm missing here? Or does anyone have suggestion of how to achieve the same effect as manually fitting to content, then to window?
edit:
It also seems to be ignoring the width constraint. I believe the table with is returned in Twips, so there should be only 11907 along the short edge and 16840 along the long edge, but not matter how large I set the cutoff (eg objTable.Columns.Width > 20000) it still autofits all tables. So if anyone knows why that is happening that would be helpful too
Right now I have a Word macro that moves an image in front of specific text by copying and pasting it to that location. This works pretty well, but it is costly. If I have 1,000s of images in my word doc it could take 30 minutes to run the macro.
There has to be a better way right?
Can I move the image anchor without copy/pasting the entire image?
My end goal is to take text + an image that is aligned in a table (text left, image right), break it out of the table, but maintain that left/right nature.
Specifically, I am looking for images in tables (row 1 column 2) and want to move them to the beginning of the text in that same table (1st column, 1st row). Here is a snippet:
For Each shape In innerTable.Cell(1, 2).Range.InlineShapes
If shape.Type = wdInlineShapePicture Then
shape.Select
Selection.Cut
innerTable.Range.Paragraphs(1).Range.Characters(1).Paste
'Do it only for the 1st image found:
Exit For
End If
Next
Note, I am leaving out some safety checks for simplicity sake (this assumes I already have found a table of valid size, etc.
It is only possible to move a Shape if the target is on the same page. In that case, a Shape can be moved by changing its Top and Left properties. Unfortunately (extremely), there is no way to move an image by changing the anchor point. So the only way to move an image to another page (or story) is using copy/paste.
If an InlineShape is to be moved, just assign InlineShape.Range.FormattedText to the target Range. Or, extend a range with text to also include the InlineShape. As far as Word is concerned, an InlineShape is a character.
To achieve the stated end goal
take text + an image that is aligned in a table (text left, image
right), break it out of the table, but maintain that left/right nature
use a table with an additional column, on the right. Put the image in it as an InlineShape. Then the entire row can be handled, for example as follows.
This creates a new row in the desired position, copies the Range.FormattedText of the row to be moved to the new row. Removes the additional row this creates and also deletes the original row.
Sub MoveRow()
Dim tbl As Word.Table
Dim rwToMove As Word.Row
Dim rwTarget As Word.Row
Dim rwBeforeTarget As Word.Row
Dim posNewRow As Long
posNewRow = 1
Set rwToMove = Selection.Rows(1)
Set tbl = rwToMove.Range.Tables(1)
Set rwBeforeTarget = tbl.Rows(posNewRow)
Set rwTarget = tbl.Range.Rows.Add(rwBeforeTarget) '(posNewRow)
rwTarget.Range.FormattedText = rwToMove.Range.FormattedText
tbl.Rows(posNewRow + 1).Delete
rwToMove.Delete
End Sub
Writing a macro to automatically fix paraphrase spacing issues in MS Word docs generated from software we use.
Goal:
All standard paragraphs have 0pt before and after spacing.
All bullet lists have 3pt before and after spacing.
Progress:
Currently I have a function that sets the entire document to 0pt, then looks through for all lists and changes them to 3pt. (currently also have a highlight on so I can easily see what is being treated as a list).
It works great on some parts, but on other parts (I assume based on how the software we use generates the document), the list doesn't exist and it will continue to format blocks of text and heading to 3pt when it is not wanted (see attached images).
Current code is:
Sub Paragraph()
ActiveDocument.Range.ParagraphFormat.SpaceAfter = 0
ActiveDocument.Range.ParagraphFormat.SpaceBefore = 0
Dim li As Word.list
For Each li In ActiveDocument.lists
li.Range.ParagraphFormat.SpaceBefore = 3
li.Range.ParagraphFormat.SpaceAfter = 3
li.Range.HighlightColorIndex = wdYellow
Next li
End Sub
Working:
Not working:
According to the MSDN:
List Object: Represents a single list format that's been applied to specified paragraphs in a document.
So if you have more than one list with some non-bulleted paragraph(s) in the middle, the Range will start with the first item of the first list and end with the last item of the last list including all non-bulleted paragraph(s) in the middle.
To fix this issue, you need to separate the lists (right-click on the bullet and select Separate List). However, you mentioned that the document was generated by some software, so that is probably not an option. In that case, you will have to iterate though the paragraphs of the Range of each List and check if it has a ListFormat.ListTemplate which indicates that it is a list item, otherwise it is a non-bulleted paragraph:
Sub Paragraph()
ActiveDocument.Range.ParagraphFormat.SpaceAfter = 0
ActiveDocument.Range.ParagraphFormat.SpaceBefore = 0
Dim li As Word.List
Dim p As Paragraph
For Each li In ActiveDocument.Lists
For Each p In li.Range.Paragraphs
If Not p.Range.ListFormat.ListTemplate Is Nothing Then
p.Range.ParagraphFormat.SpaceBefore = 3
p.Range.ParagraphFormat.SpaceAfter = 3
p.Range.HighlightColorIndex = wdYellow
End If
Next p
Next li
End Sub
Even before touching VBA:
Use Styles in the document.
Limit the use of Styles in the document to only those that are in the
template.
Set your spacing in the Styles.
If, at some stage, you change your mind and want to use 6pt spacing, you can adjust the template and re-apply it, rather than finding all the VBA code and re-writing it. Not only that, but by using Styles, you can avoid having VBA code, or having VBA-enabled documents which may interfere with some corporate security settings.
Oh, and set up your corporate structure to limit the use of templates to only the approved ones.
Looking for an example vba code that deletes all slides with a blank layout.
I am trying to create a catalog using a UserForm. The user selects which products they want to look at and the code deletes the slides of the products they don't want to look at.
The problem is that if I delete the array of slides specific to a product, it changes the total number of slides and then the other slide number arrays no longer contain the slides specific to the other products.
I was thinking I would add blank slides in place of the undesired slides and then delete all of the blank slides at the end.
I am open to other ideas and suggestions. Thank you for your time and assistance.
It sounds as though you're working with arrays of SlideNumber or SlideIndex.
Never bother with SlideNumber for various reasons.
And in this case, SlideIndex will change after you delete slides.
Instead, either work with arrays of Slide objects or instead of SlideIndex, use an array of SlideId ... SlideIds are assigned when the slide is created and never change.
Example of how to find a slide from its SlideID, in this case, 258 arbitrarily:
Dim oSl As Slide
Set oSl = ActivePresentation.Slides.FindBySlideID(258)
MsgBox oSl.SlideIndex