Insert hyperlink in a PowerPoint table cell with VBA - vba

I'm working with PowerPoint 2007. I want to use a list to create a table on a slide. The first column of each row will have a hyperlink to a different slide in the presentation (like a summary slide).
I'm having trouble using VBA to insert a hyperlink into a cell. The error message is usually something like "object doesn't support that function".
Here is the offending line:
With pptPres.Slides(2).Shapes("Table Summary").Table.Cell(i - 1, 1).Shape.ActionSettings(ppMouseClick).Hyperlink
.TextToDisplay = ThisWorkbook.Sheets(i).Range("B1")
.SubAddress = pptPres.Slides(i).SlideID
End With

You're almost there.
You need to access TextRange Object if you want to add a Link in the text within a table or shape.
Something like:
Sub marine()
Dim t As Table
Dim pptpres As Presentation
Set pptpres = ActivePresentation
Set t = pptpres.Slides(1).Shapes(1).Table
With t.Cell(2, 1).Shape.TextFrame.TextRange.ActionSettings(ppMouseClick).Hyperlink
.TextToDisplay = "Link to Slide"
.SubAddress = pptpres.Slides(2).SlideNumber _
& ". " & pptpres.Slides(2).Name
End With
End Sub
And also, you cannot use SlideID property as SubAddress.
It should be in this format: <slide number><dot><space><slide name> (eg. #2. Slide2)
To get this done we used SlideNumber and Name property instead. HTH

thanks for the above. Below generates a hyperlinked TOC table for each slide into slide 2
Sub DeckTOC() ' Creates a hyperlinked TOC of each slide in deck
' Tip: add a return-to-TOC hyperlink on Slidemaster default layout
' assumes slide 1 is a cover slide, slides 2 is for TOC
' and #2 already includes a table And (important) no other shapes or title
' with col 1 for slide title and 2nd cloumn for slide no
' TOC can be formatted before/after macro has run
Dim slidecount As Integer
Dim t As Table
Dim TOCrow As Integer
Dim pptpres As Presentation
Set pptpres = ActivePresentation
slidecount = pptpres.Slides.Count
If slidecount < 3 Then Exit Sub ' nothing to do
Set t = pptpres.Slides(2).Shapes(1).Table ' grab= ther toc
TOCrow = 2
For i = 3 To slidecount Step 1 ' get slide references for each slide
If TOCrow > t.Rows.Count Then t.Rows.Add ' add rows on fly as needed
' create text entry in cell, then add hyperlink (doing in one step fails)
With t.Cell(TOCrow, 1).Shape.TextFrame.TextRange
.Text = pptpres.Slides(i).Shapes.Title.TextFrame.TextRange.Characters
End With
With t.Cell(TOCrow, 1).Shape.TextFrame.TextRange.Characters().ActionSettings(ppMouseClick).Hyperlink
.Address = ""
.SubAddress = pptpres.Slides(i).SlideNumber & ". " & pptpres.Slides(i).Name
End With
t.Cell(TOCrow, 2).Shape.TextFrame.TextRange.Text = i
TOCrow = TOCrow + 1
Next
End Sub
ex [enter image description here][1]
[1]: https://i.stack.imgur.com/gaMJK.png

Related

How to insert images as per slide title through vba code

I want to write VBA code for inserting the image as per the slide name from the folder means after running the VBA it automatically inserts the images as per the slide name
For eg: if the slide contains "Top View" in the text box then by running the VBA script it should automatically pick the picture having name "Top View" from the particular folder.
As shown in the attached images.
Slide having by text box as top view
Folder Path
I have posted one of the question some days ago but I didn't find the exact solution here is the link of my previous question which I have asked
Previous question
One of member has shared one code but its working properly also I modified it little bit though its not working properly if possible pl. help me
Option Explicit
Sub image_insert()
Dim objPresentaion As Presentation
Dim objSlide As Slide
Dim objImageBox As Shape
Dim sSlideTitle As String
Dim sFolder As String
Set objPresentaion = ActivePresentation
sFolder = "C:\Users\mehta\Desktop\Folder for ppt images\Top
View.jpg"
For Each objSlide In objPresentaion.Slides
sSlideTitle = GetTitleText(objSlide)
' WAS there a title on the slide?
If Len(sSlideTitle) > 0 Then
' make sure the image exists
If Len(Dir$(sFolder & sSlideTitle & ".JPG")) > 0 Then
Set objImageBox = objSlide.Shapes.AddPicture(sFolder &
sSlideTitle & ".JPG", _
msoCTrue, msoCTrue, 25, 25)
Else
' Comment this out later
' MsgBox "Image missing: " & sSlideTitle
End If
Else
' comment this out later:
MsgBox "This slide has no title"
End If
Next ' Slide
End Sub
Function GetTitleText(oSl As Slide) As String
Dim sTemp As String
With oSl
' handle errors in case there's no slide title
On Error Resume Next
sTemp = .Shapes.Title.TextFrame.TextRange.Text
If Err.Number <> 0 Then
sTemp = ""
End If
End With
GetTitleText = sTemp
End Function
Regards.

How can I programmatically move textboxes in a word document to another page

The following VBA code selects textboxes in a word document that contain a specific text. How can I programmatically move every textbox to another page (let's say the first page) preserving it's position relative to the page. The original textboxes are positioned absolute to the page they are on.
Sub searchTexboxes()
'
' searchTexboxes Macro
'
'
Dim shp As Shape
Dim sTemp As String
Dim nrTextboxes As Integer
nrTextboxes = 0
For Each shp In ActiveDocument.Shapes
If shp.Type = msoTextBox Then
shp.Select
Selection.ShapeRange.TextFrame.TextRange.Select
sTemp = Selection.Text
sTemp = Left(sTemp, 1)
If sTemp = "." Then
nrTextboxes = nrTextboxes + 1
End If
End If
Next
MsgBox ("Found " & nrTextboxes & " textboxes.")
End Sub
The following code works for me.
The only way to do this, really (aside from recreating the text box from scratch), is copy/paste. That will carry across all the formatting.
Key aspects about this approach:
Setting the target page: Word doesn't have "page" objects, due to its dynamic layout behavior. Selection.GoTo is the simplest method to get a page. Since the text boxes are formatted relative to the page, it doesn't matter where on the page the anchor is attached. (Unless there's going to be a lot of subsequent editing that could push the anchoring range to a different page.) So this code assigns the first paragraph's range to be the anchor.
Identifying the text box(es) to be copied: It's not necessary to select a text box in order to work with its content. The text can be read from TextFrame.TextRange.Text.
Looping with multiple text boxes: As soon as a text box is created (pasted) in the target range, Word will say "Aha! there's another text box!" and will try to loop that, too, which is not what is wanted. So the code in the question has been modified to add the text boxes which should be copied to an array (shps()). Once all the text boxes that need to be copied have been identified, the code then loops this array, copies each text box and paste it to the target range.
Sub searchTexboxes()
Dim shp As Shape
Dim shps() As Shape
Dim sTemp As String
Dim nrTextboxes As Integer
Dim target As Word.Range
Dim targetPage As Long, i As Long
nrTextboxes = 0
targetPage = 1
Selection.GoTo What:=Word.wdGoToPage, Which:=targetPage
Set target = Selection.Paragraphs(1).Range
For Each shp In ActiveDocument.Shapes
If shp.Type = msoTextBox Then
sTemp = shp.TextFrame.TextRange.Text
sTemp = Left(sTemp, 1)
If sTemp = "." Then
nrTextboxes = nrTextboxes + 1
ReDim Preserve shps(nrTextboxes - 1)
Set shps(nrTextboxes - 1) = shp
End If
End If
Next
For i = LBound(shps) To UBound(shps)
shps(i).Select
Selection.Copy
target.Paste
Next
MsgBox ("Found " & nrTextboxes & " textboxes.")
End Sub

VBA TO copy text from textbox into slideTitle

I have created a macro in Powerpoint that will search for slides that are using a textbox for their title and are replacing them with a Title box. The steps are
1) find the slides that have a textbox in the title area
2) Copy the text in the textbox to a variable called slTitle.
3) Delete the texbox
4) Create a Title Holder for the current slide
5) Copy the text into the Title holder
6) Move on to the next slide
My macro currently is able to get as far as step 4 but I can't figure out how to get the text in slTitle into the Title box. This should be fairly easy to do but I've tried several ways and nothing seems to work. If anyone can help me figure out this step it would be much appreciated.
I am getting a compile error "Invalid Qualifier" on the line:
Set ppPlaceholderTitle.TextFrame.TextRange.Text = slTitle
Here is my current macro.
Sub AddMiMissingTitles()
Dim shpCurrShape As Object
Dim x As Integer
Dim sl As PowerPoint.Slide
Dim sld As Slide
Dim ctr As Integer
Dim s As Shape
'x = ActivePresentation.Slides.Count
'counter ctr used to count number of slides that needed titles added
ctr = 0
'**************************************************************
Set sourcePres = ActivePresentation
x = 1 ' slide counter
'get the title text
For Each sl In sourcePres.Slides
'delete all the empty title text boxes first
For Each s In sl.Shapes
If s.Top < 45 Then ' it's in the title area
'MsgBox s.PlaceholderFormat.Type
If s.Type <> ppPlaceholderTitle Then ' it isn't a proper Title placeholder
If s.HasTextFrame = msoTrue Then
If Trim(s.TextFrame.TextRange.Text) = "" Then
s.Delete ' delete empty text holders
Else
slTitle = s.TextFrame.TextRange.Text
s.Delete
sl.CustomLayout = sl.CustomLayout 'reset the slide
Set ppPlaceholderTitle.TextFrame.TextRange.Text = slTitle
End If
End If
End If
End If
Next
'Is there a title placeholder on the current layout?
If sl.CustomLayout.Shapes.HasTitle Then
lngType = sl.CustomLayout.Shapes.Title.PlaceholderFormat.Type
'*********************************
' With ActivePresentation.Slides()
End If
Next
MsgBox "Done! " & vbCrLf & ctr & " Slides needed Titles."
'*********************************
'sl.Shapes.AddPlaceholder lngType
sl.Shapes.Title.TextFrame.TextRange = slTitle
End Sub

PowerPoint VBA search and delete paragraphs in Notes

I have several PowerPoints with a great deal of text in the notes. I need to search the note text and delete any paragraphs that start with "A."
Here is what I tried - but am getting type mismatch error
Dim curSlide As Slide
Dim curNotes As Shape
Dim x As Long
For Each curSlide In ActivePresentation.Slides
Set curNotes = curSlide.NotesPage.Shapes(2).TextFrame.TextRange
With curNotes.TextFrame.TextRange
For x = 1 To Len(curNotes.TextFrame.TextRange)
If Mid(curNotes.TextFrame.TextRange, x, 2) = "A." Then
curNotes.TextFrame.TextRange.Paragraphs = ""
End If
Next x
End With
Next curSlide
End Sub
Thanks for your help!!
You get a mismatch error whenever you try to assign data of a different type specified by your variable. This is happening in your code because you defined curNotes as type Shape and then tried to set that object variable to a different data type, TextRange. You are then trying to process the object TextRange as a string. You need to work on the .Text child of .TextRange The use of Mid is not checking the start of the string and finally, when you set the text to "", you are deleting all the text in the Note but that's not what you said you're trying to do.
This is the corrected code to delete only paragraphs starting with "A."
' PowerPoint VBA macro to delete all slide note paragraphs starting with the string "A."
' Rewritten by Jamie Garroch of youpresent.co.uk
Option Explicit
Sub DeleteNoteParagraphsStartingA()
Dim curSlide As Slide
Dim curNotes As TextRange
Dim iPara As Long
For Each curSlide In ActivePresentation.Slides
Set curNotes = curSlide.NotesPage.Shapes(2).TextFrame.TextRange
With curNotes
' Count backwards in any collection when deleting items from it
For iPara = .Paragraphs.Count To 1 Step -1
If Left(.Paragraphs(iPara), 2) = "A." Then
.Paragraphs(iPara).Delete
Debug.Print "Paragraph " & iPara & " deleted from notes pane on slide " & curSlide.SlideIndex
End If
Next
End With
Next curSlide
End Sub

inserting images into a table using macro (vba powerpoint 2010)

I've written a vba script which basically inserts a table into a slide with text"Sample text in the cell", Now i want to add images to the cells of the table depending on the size of a table from a folder.
Basically if it's 5X3 table then it should take 15 images from the folder and arrange it into the cell with the image file name. The output which i want is this:
the code which i wrote is :
Sub NativeTable()
Dim pptSlide As Slide
Dim pptShape As Shape ' code to create table in present slide
Dim pptPres As presentation
Dim iRow As Integer
Dim iColumn As Integer
Dim oShapeInsideTable As Shape
Set pptPres = ActivePresentation
With pptPres
Set pptSlide = .Slides.Add(.Slides.Count, ppLayoutBlank)
End With
With pptSlide.Shapes
Set pptShape = .AddTable(NumRows:=3, NumColumns:=5, Left:=30, _
Top:=110, Width:=660, Height:=320)
End With
With pptShape.Table
For iRow = 1 To .Rows.Count
For iColumn = 1 To .Columns.Count
With .Cell(iRow, iColumn).Shape.TextFrame.TextRange
.Text = "Sample text in Cell"
With .Font
.Name = "Verdana"
.Size = "14"
End With
End With
Next iColumn
Next iRow
End With
End Sub
So how can i do it please help me with this and the image should be resizable(if that is possible) please help me with this
Is this possible i've checked the msdn object reference but there is no option for inserting picture in a cell
Insert as cell background. Select a cell > Design > Shading > Picture
I'm a little late to the party, but here's the code in case anyone else needs it. Just make sure to change the variables in parenthesis to fit your needs.
PowerPoint.ActivePresentation.Slides(1).Shapes("table_1").Table.Cell(1, 2).Shape.Fill.UserPicture ("location_of_picture_file")