How to travel all paragraphs in a document with vba - vba

I open a document, and would like to travel and set all paragraghs with bold.
this is the code
for each par in activedocument.paragraphs:
par.range.bold=true
next
After I run this code,I found paragraphs in textbox not be changed.
So,how to travel all paragraphs in a document with vba

This should work for the text boxes, because they are not paragraph objects.
Dim shp As Shape
For Each shp In ActiveDocument.Shapes
ActiveDocument.Shapes.Range(Array(shp.Name)).Select
Selection.Font.Bold = True
Next
There are probably other ways too.

That is the WRONG way to format a document. All you need do is format the relevant paragraph Styles with the bold attribute. For example:
ActiveDocument.Styles(wdStyleNormal).Font.Bold = True
Then it doesn't matter whether the content is in the document body, a header or footer, a footnote or endnote, or in a textbox in any of those ranges.

Related

Auto fit a textbox(shape) to a text in a Word document

To give the context of my problem, I have to work with documents with text boxes, these text boxes hide the whole sentence almost all the time, so I have to resize the text box by hand so that the text is visible. The problem is that on some documents there are over 700 text boxes. Then later I've found that i can do this (Resize shape to fit text in EN) :
So I was wondering if there is a way to select all the text boxes and resize them automatically selecting this option with VBA. Thank you !
EDIT
So I've tried to start my code doing this :
Dim eShape As Word.shape
Dim i As Long
For i = ActiveDocument.Shapes.Count To 1 Step -1
Set eShape = ActiveDocument.Shapes(i)
Then I start the condition by checking the object type in this case TextBox with
If eShape.Type = msoTextBox Then
But for the rest I didn't found the method to resize the element.
Salut Satanas,
Assembled this from various bits of code found lying around several sites:
Sub AllTextBoxesAutoSize()
Dim MyShape As Shape
For Each MyShape In ActiveDocument.Shapes
If MyShape.Type = msoTextBox Then
MyShape.TextFrame.AutoSize = True
End If
Next
MsgBox ("All text boxes autosized!")
End Sub
I added the MsgBox because otherwise it's not apparent that anything's happened :-)
Bon courage!
Steve
#TimothyRylatt If you don't want to help, then just scroll by.

Convert Figure Alt Description to Picture Caption in Word

I need a way to get an image's Alt Description (specified in HTML) into the Word DOCX image's Caption. This needs to be done with many images in a document, so a macro would be best.
Assume an HTML doc with an img tag similar to this
<img src="http://www.example.com/path/to/image/picture01.jpg"
title="picture 01 title"
alt="this is alt text"
caption="this is a caption field">
This HTML doc is imported into Word 2010 (via File, Open). The image will show in the doc.
Now, how to get the 'title' attribute (which shows up in the Format Picture's Alt-Text dialog as the Description - see screenshot below) into the image's Caption?
Note that the caption parameter in the image tag is not converted to a Word Caption for that image.
Sample Alt-Text dialog for an image which shows the image's alt value as the Description
Microsoft Word has a Range.InsertCaption method, which will insert field codes to automatically number images. As you were asking to insert Alternative Text, my feeling is you were using the term caption simply as a directive to get the text beneath each image on its own carriage return. So that's what this code does:
Sub GenerateAltTextCaptions()
Dim AltTextCaption As String
Dim ImageRange As Range
Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst
With ActiveDocument
For i = 1 To .InlineShapes.Count
AltTextCaption = .InlineShapes(i).AlternativeText
Set ImageRange = .InlineShapes(i).Range
ImageRange.Collapse Direction:=wdCollapseEnd
ImageRange.InsertAfter vbCr
ImageRange.InsertAfter AltTextCaption
Next
End With
End Sub
Yes, I think you've hit on a good way to make the conversion. Glad you were able to find it!
The code below will loop all the InlineShapes in the document and, if the Alternative Text is not empty, create a caption using that text.
InsertCaption is only available in VBA for InlineShapes. The user can insert captions for Shapes (graphics with text wrap formatting) because Word evaluates the selected graphic and creates a Textbox of the same width and positions it immediately below the graphic. The Shape and textbox are not, however, linked together in any way. So this functionality is not offered in VBA and would require some "creative" programming.
Sub InsertCaptionFromAltText()
Dim doc as Word.Document
Dim ils As word.InlineShape
Dim captionText As String
Set doc = ActiveDocument
Set ils = doc.InlineShapes(1)
For each ils in doc.InlineShapes
captionText = ils.AlternativeText
If Len(captionText) > 0 Then
ils.Range.InsertCaption Label:=wdCaptionFigure, _
Title:=captionText, _
Position:=wdCaptionPositionBelow
End If
Next
End Sub

VBA for Microsoft PowerPoint to recognize and hide text boxes in German language

I need to make two presentations with the same slides, backgrounds, and everything except for the text: one in German and one in English. Therefore I have two separate presentations which I must always simultaneously update, otherwise one language version will be outdated and I often forget what I changed.
I have made a custom show with all of the slides copied into one PowerPoint presentation and that works fairly well, but I still must change two of the same slides each time that I make an edit to one language.
Therefore, I'm trying to write a macro that will recognize all textboxes within the presentation with German text in them and hide them during the show, and vice versa. Then, each of the macros would be linked to a hyperlinked box on the title slide called 'English' or 'German' which, when clicked, would hide the textboxes in the other languages while also leaving all pictures and formatting the same. Ideally, the macro would hide all boxes in one language and make all the boxes in the other language visible again within the same step, so that the presentation is always usable and I don't have a user who opens a PPT with 'no text boxes' because they would all be hidden...
In order to achieve this I have two text boxes containing the text in both languages on the same slide, that's why I'd like to hide the textboxes.
I am able to get all text boxes hidden but not text boxes in a specific language (aka, all boxes regardless of their editing language will get hidden but not any specific ones).
PS - text boxes do not NEED to be referenced here... it could just refer to a shape. I was trying to avoid, that Pictures would be hidden and thought text boxes would be the best way to reference what I wanted.
Is there a better way?
I don't really know how to reference a language in VBA, I found this website by accident and thought someone might have a quick trick to help me out with this issue. The code I have which will blend out textboxes but not blend out specific language-boxes is as follows:
Sub GermanTextBoxFinder()
Dim SlideToCheck As Slide
Dim ShapeIndex As Integer
' Visit each slide
For Each SlideToCheck In ActivePresentation.Slides
' On each slide, count down through the shapes
For ShapeIndex = SlideToCheck.Shapes.Count To 1 Step -1
' If the shape IS a text box and DOES have German text
If SlideToCheck.Shapes(ShapeIndex).Type = msoTextBox And _
MsoLanguageID.msoLanguageIDGerman _
Then
' Toggle visibility of German Textboxes
SlideToCheck.Shapes(ShapeIndex).Visible = msoFalse
End If
Next
Next
End Sub
Why don't you name the shapes with German text something that identifies them? E.g. use the prefix "txtGER" for the texts in German and "txtENG" for the ones in English. Then you could use something like the following:
If SlideToCheck.Shapes(ShapeIndex).Type = msoTextBox And _
Left(SlideToCheck.Shapes(ShapeIndex).Name, 6) = "txtGER" Then
' Toggle visibility of German Textboxes
SlideToCheck.Shapes(ShapeIndex).Visible = msoFalse
Else
SlideToCheck.Shapes(ShapeIndex).Visible = msoTrue
End If
(Please see this q+a for information on how to rename the shapes).
This will hide text boxes/shapes with text that contain either English or French. You can modify HideFrench to hide German instead ... Intellisense will provide the correct constants.
Sub HideEnglish()
Dim oSl As Slide
Dim oSh As Shape
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
If oSh.HasTextFrame Then
If oSh.TextFrame.HasText Then
If oSh.TextFrame.TextRange.LanguageID = msoLanguageIDEnglishUS Then
oSh.Visible = False
Else
oSh.Visible = True
End If
End If
End If
Next
Next
End Sub
Sub HideFrench()
Dim oSl As Slide
Dim oSh As Shape
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
If oSh.HasTextFrame Then
If oSh.TextFrame.HasText Then
If oSh.TextFrame.TextRange.LanguageID = msoLanguageIDFrench Then
oSh.Visible = False
Else
oSh.Visible = True
End If
End If
End If
Next
Next
End Sub

Word 2013 VB script to loop through document and change styles

I need a script which iterates through a word document and changes the style of paragraphs following a Headline style or an Image to a custom style without first line indent.
How do I loop through the paragraphs/headers/items in a word document? And how do I get the style? And how do I set the style afterwards?
The Goal is simple: I want the first line of a paragraph be indented, but not if the paragraph is following a Header line or image. And since this is a large document and I get those quite often I'd like some Kind of Automation and not try to do this by Hand.
So I'd like to write a script which is iterating through the paragraphs and changes the style from "paragraph" to "paragraph without indent" when it is after a header style or image.
Here is some basic code to get you started here. Unfortunately, the Paragraph.Style parameter doesn't distinguish between text and images, but you can check and see if a Paragraph.Range object has any InlineShapes, which are images.
Sub indentParas()
Dim doc As Document
Set doc = ActiveDocument
Dim para As Word.Paragraph
Dim i As Boolean
i = False
For Each para In doc.Paragraphs
If i = False Then
para.IndentCharWidth 4
End If
If para.Range.InlineShapes.Count > 0 Then
i = True
ElseIf Left(para.Style, 7) = "Heading" Then
i = True
Else
i = False
End If
Next
End Sub
Note: this is tested in Word 2010.

Apply Font Formatting to PowerPoint Text Programmatically

I am trying to use VBA to insert some text into a PowerPoint TextRange, I use something like this:
ActiveWindow.Selection.SlideRange.Shapes("rec1").TextFrame.TextRange.Text = "Hi"
However, I can't figure out how to apply bold, italic and underline programmatically (I don't see a .RichText property or something similar).
What I have is some simple HTML text with bold, italic and underlined text I would like to convert over.
How to do this?
This is easily accomplished by using the TextRange's Characters, Words, Sentences, Runs and Paragraphs objects and then it's Font object to set Bold, Underline and Italic (amongst other properties). For example:
Sub setTextDetails()
Dim tr As TextRange
Set tr = ActiveWindow.Selection.SlideRange.Shapes(1).TextFrame.TextRange
With tr
.Text = "Hi There Buddy!"
.Words(1).Font.Bold = msoTrue
.Runs(1).Font.Italic = msoTrue
.Paragraphs(1).Font.Underline = msoTrue
End With
End Sub
Try looking at MSDN's documentation on the TextRange object. It contains samples of how to access the Font properties of the TextRange object.
EDIT: You can access things like Bold and Italics programmatically in this manner:
TextRange.Font.Bold = msoTrue
EDIT EDIT: There are several methods by which you can select only certain text in a text range. See the following:
Characters Method
Lines Method
Paragraphs Method
Words Method
According to the sames from this link, you can select a portion of the text using one of these methods and set the font programmatically. For example:
Application.ActiveDocument.Pages(1).Shapes(2) _
.TextFrame.TextRange.Words(Start:=2, Length:=3) _
.Font.Bold = True
That example was taken from the Words Method link.
In addition to the above answer, you should try to name the objects you'll be changing, since selecting them in the middle of a presentation could make PowerPoint act oddly. Create a new TextRange object and set it like this.
dim mytextrange As TextRange
Set mytextrange = ActiveDocument.Pages(1).Shapes(2).TextFrame.TextRange
mytextrange.Words...
Here is how you can do it to change the font of a specific text:
Sub changeFont()
Dim oPresentation As Presentation
Dim oSlide As Slide
Dim oShape As Shape
Dim stringSearched As String
stringSearched = "something"
'all opened presentations
For Each oPresentation In Presentations
'all slide in them
For Each oSlide In oPresentation.Slides
'all shapes (anything)
For Each oShape In oSlide.Shapes
'only those that contain text
If oShape.HasTextFrame Then
If InStr(oShape.TextFrame.TextRange.Text, stringSearched) > 0 Then
'here you need to define where the text ends and start
oShape.TextFrame.TextRange.Characters(InStr(oShape.TextFrame.TextRange.Text, stringSearched), Len(stringSearched)).Font.Underline = msoTrue
oShape.TextFrame.TextRange.Characters(InStr(oShape.TextFrame.TextRange.Text, stringSearched), Len(stringSearched)).Font.Italic = msoFalse
End If
End If
Next
Next
Next
End Sub