Convert Figure Alt Description to Picture Caption in Word - vba

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

Related

How to travel all paragraphs in a document with 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.

Hyperlink one word in a sentence in PowerPoint

I'm currently inserting a sentence after the cursors position. When I insert the sentence I want to hyperlink one word in that sentence, however, for some reason the whole sentence is getting hyperlinked.
My sentence I'm inserting is "Please see {hyperlinkedText} for more information". I want the "{hyperlinkedText}" to be hyperlinked but not the whole sentence.
Please see code below.
The "titleName" is what I want to be hyperlinked and "fullText" is the senetence I'm inserting.
Dim insertText As Tuple(Of String, String, Integer) = listCheckBoxSelectedItem.SelectedItem
Dim titleName As PowerPoint.TextRange = PowePointApplication.ActiveWindow.Selection.TextRange
titleName.Text = insertText.Item1
Dim fulltext = $"Please see {titleName.Text} for more information."
Dim selectedIndex = PowePointApplication.ActiveWindow.Selection.TextRange.InsertAfter("D")
selectedIndex.Text = fulltext
With titleName.ActionSettings(PowerPoint.PpMouseActivation.ppMouseClick)
.Action = PowerPoint.PpActionType.ppActionHyperlink
.Hyperlink.SubAddress = insertText.Item3
End With
As far as I can see, in the code provided, titleName is the entire text selection, and that is why the entire text gets the hyperlink. It probably needs a sub-textrange for the piece you want to put a hyperlink on.
I've not tried this on VB, but from VBA, I believe you can use Find method of TextRange to get a new sub-TextRange just for that part, which you can then update the hyperlink settings. The code below just sets the hyperlink for the text {hyperlinkedText} part, if it is a substring of the selected text.
With ActiveWindow.Selection.TextRange.Find("{hyperlinkedText}").ActionSettings(PowerPoint.PpMouseActivation.ppMouseClick)
.Action = PowerPoint.PpActionType.ppActionHyperlink
.Hyperlink.SubAddress = "http://www.bing.com"
End With

Copy text and picture into new word document

In the active word document I have a macro which extracts a text string and all images from the document.
I want to copy this text and the picture in a new blank word document.
I tried the following
Dim docNew As Document
Set docNew = Documents.Add
With ThisDocument
...
docNew.Content.Text = docNew.Content.Text & vbCrLf & sSentence
For Each iShape In .InlineShapes
iShape.Select
Selection.CopyAsPicture
docNew.Content.Paste
Next iShape
End With
When I execute this code, first the text is copied correctly to the new blank document. But when the picture is pasted, it overwrites the text and only the picture remains in the document.
How do I have to modify the code so that the text as well as all pictures are included?
As you would have discovered from looking at the help text .Content represents the whole of the main body of the document.
Assuming you want to add the pictures at the end of the document, replace
docNew.Content.Paste
with
With docNew.Content
.InsertParagraphAfter
.Paragraphs.Last.Range.Paste
End With

powerpoint vba get default text in shape

My PowerPoint slides has a text boxes that have default text in it, such as "Profile Description" and when you click on it, the words "Profile Description" disappear, so you can type in the profile.
I want to find that specific text box, the one that contains the default text "Profile Description", by looking at the contents of the box (I can't use title, as the title might be different depending which slide its on).
I can find many other properties of this text, by using Slide.TextFrame.TextRange, but I can't figure out how to get that default text.
Once I click in the box and type some text, then I can access the new value using Slide.TextFrame.TextRange.Text, but I need the default text. I've looked at a ton of documentation, but I guess I'm missing it somewhere. Please point me in the right direction. Thanks.
Is that text box created from a placeholder? If so, you'll need to look at the parent placeholder on the custom layout associated with the slide, which is not trivial due the differences in collections indexing between a slide and a layout and the fact that the Name property changes each time the slide is generated from the master. Example:
oSld.CustomLayout.Shapes.Placeholders(index).TextFrame2.TextRange.Text
Note that this will work for custom text placeholders but not for built-in placeholders where text on the custom layout is dynamically replaced by PowerPoint on the slide.
Example:
Slide Master / Layout / Title Placeholder : "Click to edit Master title style"
Slide Placeholder : "Click to add title"
You could get a reference to the shape on the layout by using tags. Tags are bits of invisible meta data that you can add to presentations, slides or shapes.
This is how you add a tag to a shape on a slide:
ActivePresentation.Slides(1).Shapes(1).Tags.Add myName, myValue
You then write a function to return a shape by its tag like this:
' *************************************************************************************
' Purpose : Returns a presentation, slide or shape by its tag from a collection of
' presentations, slides or shapes
' Author : Jamie Garroch of YOUpresent.co.uk
' Inputs : TagObject - collection type to be searched. Presentations, Slides or Shapes
' TagName - The tag name to search for (always upper case)
' TagValue - The tag value to search for
' Outputs : Returns a Presentation, Slide or Shape object if a match is found
' *************************************************************************************
Public Function GetByTag(TagObject, TagName As String, TagValue As String) As Object
On Error GoTo errhandler
Select Case True
Case TypeOf TagObject Is Presentations
Dim oPres As Presentation
For Each oPres In TagObject
If TagExists(oPres, TagName, TagValue, True) Then Set GetByTag = oPres: Exit Function
Next
Case TypeOf TagObject Is Slides
Dim oSld As Slide
For Each oSld In TagObject
If TagExists(oSld, TagName, TagValue, True) Then Set GetByTag = oSld: Exit Function
Next
Case TypeOf TagObject Is Shapes
Dim oShp As Shape
For Each oShp In TagObject
If TagExists(oShp, TagName, TagValue, True) Then Set GetByTag = oShp: Exit Function
Next
End Select
Exit Function
errhandler:
DebugMsg "GetByTag Error : " & Err & " " & Err.Description
End Function
I've been trying to find a way to make this work with Tags, but couldn't.
So instead, my solution has been to pre-check the CustomLayout for the Placeholders and note the top x,y coordinates and what values they should have by default.
After, when the page has been generated, I review the shapes by x,y coordinate and if there's a match, substitute in the default value.

Entering information into the Notes section of a PowerPoint slide using VBA

I am trying to find out how you write VBA to enter a text box into a slide, and enter text. I am also trying to find vba for entering text into the notes section of a PowerPoint slide.
Any help would be greatly appreciated. I have tried to find a site specifically for this, but have not been able to do so
Entering text into a PPT slide is about the same as entering into the notes section.
You have to start out with a Slide object reference, which represents the slide you're adding to; and you add a text box shape to the slides' shapes collection.
Example:
Sub AddTextBoxToSlide()
Dim oDestSlide As PowerPoint.Slide
Set oDestSlide = ActivePresentation.Slides(1)
Dim slideWidth As Single
Dim slideHeight As Single
slideWidth = oDestSlide.Parent.PageSetup.SlideWidth
slideHeight = oDestSlide.Parent.PageSetup.SlideHeight
Dim oTextBox As PowerPoint.Shape
Set oTextBox = oDestSlide.Shapes.AddTextbox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=0, _
Top:=0, _
Width:=slideWidth, _
Height:=slideHeight / 12)
oTextBox.TextFrame.TextRange.Text = "Shape text here"
End Sub
All this does is adds a text box shape to the first slide in the active presentation at the top of the slide. It is as wide as the slide and 1/12th the height of the slide. The parameters for Shapes.AddTextbox() are pretty self-explanatory...
To add to the notes section, I just use the NotesPage object on the slide your notes page is in...so the above code would be about the same, except:
Set oTextBox = DestSlide.NotesPage.Shapes.AddTextbox(msoTextOrientat...
This is an old question, but since you can't record macros in PowerPoint, people will be searching for questions like this until you can.
I didn't need this for adding text to slides, but I tried it for adding text to Notes. However, in Outline View, nothing appeared in my Notes section. It wasn't until I went to View-->Notes Page, and I saw the message I'd added -- at the top of the screen.
You see, when you change Set oTextBox = oDestSlide.Shapes to Set oTextBox = oDestSlide.NotesPage.Shapes, you're not adding text to the Notes. You're adding a textbox to the notes. And that textbox appears only in Notes Page view (at the top of the screen, until you move it).
What we really want to do is add our text to Placeholder 2 (the notes area) on the notes page, like this:
oDestSlide.NotesPage.Shapes.Placeholders(2).TextFrame.TextRange.InsertAfter "Notes text here"