Hyperlink one word in a sentence in PowerPoint - vb.net

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

Related

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

Is there a way to list broken internal hyperlinks with VBA in MS Word? (Hyperlink Subaddress)

In MS Word, you can create hyperlinks to a "Place in this document" so that a link takes you someplace else in the same Word file. However, if you change headers or move things around these links will sometimes break. I want to write some VBA to check for broken links.
With VBA, you can list each hyperlink subaddress using the code below:
Sub CheckLinks()
Set doc = ActiveDocument
Dim i
For i = 1 To doc.Hyperlinks.Count
Debug.Print doc.Hyperlinks(i).SubAddress
Next
End Sub
The output from the code above also matches what is shown in the field codes for the hyperlink.
However, I'm not really clear on how to verify if the SubAddress is correct. For example, an excerpt from the program output shows this:
_Find_a_Staff_1
_Edit_Organization_Settings_2
_Set_the_Staff
_Find_a_Staff_1
But there's no obvious way to tell what the "correct" suffix should be for a given heading. Any thoughts on how to check if these are valid?
Is there a way to get the list of all valid subaddresses for the headings in the document?
The code below will list the hyperlinks where the corresponding bookmark does not exist in the document. (Note that it only detects missing links, not links that go to the wrong place.)
Sub CheckLinks()
Dim doc As Document
Set doc = ActiveDocument
Dim i, j
Dim found As Boolean
For i = 1 To doc.Hyperlinks.Count
found = False
For j = 1 To doc.Bookmarks.Count
If doc.Range.Bookmarks(j).Name = doc.Hyperlinks(i).SubAddress Then
found = True
End If
Next
If found = False Then
Debug.Print doc.Hyperlinks(i).SubAddress
End If
Next
End Sub

Check Word 2007 bookmark content and do something if it exists

Is it possible to search the content inside a bookmark and if it exists, do something.
For example, if there is a word document with a bookmark named Bookmark1. The enclosing text for Bookmark1 was created by highlighting the the text "Entered Text Goes Here". I want to create a macro that will check to see if the text inside the bookmark was changed, and if NOT, delete the text, the bookmark, the section break before it.
The code below does this except that it deletes the bookmark even if the text is different because it is looking for the name of the bookmark, not its content.
If ActiveDocument.Bookmarks.Exists("Bookmark1") = True Then
ActiveDocument.Bookmarks("Bookmark1").Select
Selection.Delete
With Selection
.EndKey Unit:=wdStory
.TypeBackspace
.Delete
End With
End If
I really want the If statement to say something like:
If the text inside the Bookmark1 = "Entered Text Goes Here" Then do all the stuff below, else quit.
Ideas anyone?
Word 2007.
The below should work if your document is set up how I think it is, otherwise you will need to have a play around with it:
'TestTxt is the default text in the bookmark (assuming that you are not including the paragraph mark in the bookmark)
Dim TestTxt As String: TestTxt = "Enter text here"
'DMRng is the range of the the bookmark you are looking at
Dim BMRng As Range: Set BMRng = ThisDocument.Bookmarks("Bookmark1").Range
If BMRng.Text = TestTxt Then
'Start is the beginning of the bookmark - 1 (as the character before hand should be your section break?!)
BMRng.SetRange Start:=BMRng.Start - 1, End:=BMRng.End
BMRng.Delete
End If

Insert highlighted sentence at bookmark in MS Word?

So I have a userform that I use to populate a template with text at certain bookmarks. To make it easy to see what text has been inserted, I would like it to be highlighted with yellow. Is there a convenient way to do this for every text that is inserted without typing it, selecting it and then highlighting it? As an example, this is what part of my code looks like atm:
With ActiveDocument
Options.DefaultHighlightColorIndex = wdYellow
'[highlight=yellow].Bookmarks("Modtager").Range.Text = TxtModtager.Value[/highlight]
.Bookmarks("Modtager").Range.Text = TxtModtager.Value
.Bookmarks("KSnr1").Range.Text = txtKSnr.Value
.Bookmarks("KSnr2").Range.Text = txtKSnr.Value
The first line doesn't seem to do anything - even without the option turned on new text is still not highlighted.
The second line is something I found at another site but had to be commented as it's not working.
The last three lines insert the actual text and I guess you could choose to select the bookmark first and then Selection.TypeText Text:="Whatever value I need", followed by selecting the new phrase again (how?) and choose .HighlightColorIndex = wdYellow.
There should be a better way though, any suggestions? :)
This works for me:
SetBkmkText "Modtager", TxtModtager.Value
SetBkmkText "KSnr1", txtKSnr.Value
SetBkmkText "KSnr2", txtKSnr.Value
Sub SetBkmkText(bkmk as String, NewText as String)
With ActiveDocument.Bookmarks(bkmk).Range
.Text = NewText
.HighlightColorIndex = wdYellow
End With
End Sub

How can i make link with selected range in word?

I am using word2003 document for processing in my document i have to made link with two string variables (Not in the sense Footnotes and Endnotes)
{Page 1} Best quote from David Brinkley[1]
{Page 6}[1] A successful person is one who can lay a firm foundation with the bricks that others throw at him
I suppose to use Footnote/Endnote for the value [1] to link but it cause some changes while editing the the Footnote/Endnote. Is there any other way to make link between the selected string?
There are probably many ways to do this, but you could bookmark the text on page 6, then add a hyperlink on page 1 that points to the bookmark. This can be done without VBA code:
Select the text for the bookmark on page 6.
Insert/Bookmark
Name the bookmark and add it
Select the text for the bookmark on page 1.
Insert/Hyperlink
Click the Bookmark button and select the bookmark in the list
The equivalent in VBA:
Option Explicit
Sub AddHyperlinkToBookmark()
Dim oBookmark As Bookmark
Dim oHyperlink As Hyperlink
'***** Add code to select text for bookmark
Set oBookmark = ThisDocument.Bookmarks.Add("BookmarkName", Selection.Range)
'***** Add code to select text for link
Set oHyperlink = ThisDocument.Hyperlinks.Add(Selection.Range, "", "BookmarkName")
Set oBookmark = Nothing
Set oHyperlink = Nothing
End Sub