Dynamically update text in powerpoint using VBA - vba

I have a report that is generated in PowerPoint, and underneath many of the graphs, there is text that tells the reader to refer to pages in the appendix. I would like to be able to dynamically reference these slides.
For example, under a graph I might have the text "Please see appendix page 54", but I need the 54 to be linked to a slide so that if I insert another slide it will say 55.
Is this possible to do in VBA? I do not expect somebody to write my code for me, I would just like to know if this is a reasonable thing to do before I spend hours attempting to do it.
Side note: I feel horrible asking a question about MS Office on here, but since I believe it would need to be implemented in VBA (I don't think this functionality is built in by default) I think that it is a relevant question.

No need to feel horrible asking this here.
How one might do this:
In PPT, shapes, slides and even the presentation itself can have an associated tag collection; named string values. For example, assuming a reference to the shape in oSh, you can do:
oSh.Tags.Add "AssociatedSlideId", "293"
In this case, you'd apply this tag to your graph; the 293 would be the SlideID of the slide you want to reference. Each slide has a unique SlideID assigned when it's created; the SlideID won't change when you move the slide around/add/delete slides.
To read the tag from the shape:
Debug.Print oSh.Tags("AssociatedSlideId")
In this case, that'd return "293". Feed that to FindBySlideID to get the SlideIndex of the slide (ie, the ordinal position of the slide in the presentation). Or ask it for SlideNumber if you want the number of the slide that'll appear in number placeholders (usually, but not always the same as slide index).
Debug.Print ActivePresentation.Slides.FindBySlideID(clng("293")).SlideIndex
You might also tag the textbox or other shape that you want to use to hold the reference, then write a function along the lines of:
Function ShapeTaggedWith(oSl as Slide, sTagName as String, sTagValue as String) as Shape
This would iterate through the shapes on slide oSl looking for one with a tag named sTagName, value = sTagValue and return it to the caller if found.
Now you can find the shape that's nominated as your caption for the graph, let's call it, and change its text to match the SlideIndex (or SlideNumber) of the slide the chart's supposed to reference.
Hope that's all moderately clear; if not, that's why the StackOverflow gods gave us comments.

Related

Vba code to delete all slides with a blank layout powerpoint?

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

How do I update the slide master from Slideshow Mode and refresh current slide using VBA?

I'm trying to have a counter in all slides of a powerpoint presentation.
The counter needs be controlled by a button in all slides that increases it.
I can do it for one specific slide, but when I do it with a slide master it doesn't refresh the screen during Slideshow mode. I have to exit slideshow and enter again to see the changes.
Code that doesn't refresh in Slideshow mode:
ctr = ctr + 1
ActivePresentation.Designs(1).SlideMaster.Shapes("Counter").TextFrame2.TextRange.Text = ctr
Code that works (but only applies to one slide, not all slides in presentation):
ctr = ctr + 1
ActivePresentation.Slides(1).Shapes("Counter").TextFrame2.TextRange.Text = ctr
Thanks!
If your second method works, why not loop it?
Dim sld as Slide
ctr = ctr + 1
For Each sld in ActivePresentation.Slides
sld.Shapes("Counter").TextFrame2.TextRange.Text = ctr
Next
NB: This will error on any slide which doesn't contain a shape named "Counter", and you will need to add logic to handle that condition, if it exists in your Presentation.
Bookmark this link, it's the PPT Object Model Reference which, while laborious to peruse, will explain just about anything you need.
https://msdn.microsoft.com/en-us/library/office/ff743835(v=office.14).aspx
The hardest part (as a beginner) is knowing what questions to ask, and while the Object Model doesn't help you with that immediately, the more you browse it and search it, the more familiar you'll become with the different objects at your disposal, and what you can do with each of them.
If you're new to VBA entirely, also bookmark this list of VBA Statements. This has examples & definitions for all of the control flow & logic statements you might use in putting some code together.
https://msdn.microsoft.com/en-us/library/office/jj692812(v=office.15).aspx
This answer talks about how to run a macro automatically when changing slides, we'll use that approach for your problem. You'll use the OnSlideShowPageChange event because that's an auto macro and doesn't require the more complicated Application Class event handler.
How to run a macro "OnEnterSlide" or "OnLeaveSlide" in Powerpoint VBA?
Implementing this is actually a moderately complicated problem and moreso for someone who is not familiar with VBA or PowerPoint's object model.
Now, you've mentioned a number of things which either are simply not true, or at the very least it is not intuitive or obvious why these must be true, when presented with alternatives:
For that to work I would have to use a macro to add the shape to each slide individually. But then if I wanted to change the size of the shape I have to use code for that too. Not very elegant...would rather force the refresh somehow and use a master slide for the shape
(Note that the scope of your initial problem keeps increasing and becoming more complex).
The reason is to make it easier to change the shape without using code and creating the shape in all the slides.
Sometimes this is the easiest or best way to do things, sometimes it's not. I am in no position to evaluate this with regards to your specific requirements, because you've given only 2 lines of code to examine, but from my vantage point, it seems you are hesitant to write more code because you simply don't know how.
I'm sorry but your deadline is not my problem, and I've spent a generous amount of time on this answer which I hope will at least point you in the right direction.
This is a different approach, you may consider.
Add a button named Sh1, Sh2 in slide 2, Sh3 in slide 3 etc.
and attach all this shapes to the below code
For ctr = 1 To 3
If ActivePresentation.Slides(ctr).Shapes("sh" & ctr).TextFrame2.TextRange.Text = ctr Then
ActivePresentation.SlideShowWindow.View.Next
End If
Exit Sub
Next
End Sub
This would check the current slide with the counter, if matches, moves next else exits.

MS Word Ignores Content Control Inside a Rich Text Box

Is there a reason why my MS Word VBA macro is ignoring a dropdown list I placed inside a shape (a rich text box)? I've tried referring to it by tag, name, number, etc. I even had the macro tell me the count of content controls:
MsgBox(ActiveDocument.ContentControls.Count)
I get 0.
Nothing works. If I take it out of the shape, it works fine. MS Word gives me a count of 1 item. But for some reason MS Word won't acknowledge it inside the shape. Any help on how to do this?
Edited as my previous post was completely wrong.
Each textbox in the main text story is a Shape which you can access using an index number. A shape has various properties but text etc. is in its Textframe, if it has one. But in that case the Range you need is not called Range but TextRange. So, e.g. the first contentControl in Shape 2 is
ActiveDocument.Shapes(2).TextFrame.TextRange.ContentControls(1)
You will probably need to iterate through your shapes and you may need to verify that a given shape is a textbox and/or that it has a TextFrame.
If your text box is in another Story such as a header or footer, you will probably need to identify the relevant StoryRange.

How can i set the name of a textbox in publisher?

I want to set the name of the text box so it can be easily accessed by code.
e.g
I am looking for an editing field similar to this
Thanks
There's a properties Window that can be accessed for each of the controls on the UI. There you may rename the controls. (Since you do not seem to have a VBA code yet and you want to rename the control from UI)
The other alternative. Record a macro, do some changes to the textbox (e.g. resize, change text etc). Then check the programme assigned default name of the textbox from the VBA editor. As you said, you can access the control via this default name and utilizing your VBA code (as you said), rename the textbox.
If you really want to be editing a worksheet object in Publisher you will have to get the OLEobject of the Shape and interpret it as an Excel.Application.
If you are just looking for a placeholder solution for Publisher documents, you could simply create a textbox that contains a certain string, then loop through all pages, all shapes on each page where HasTextFrame = msoTrue, and compare shape.TextFrame.TextRange.Text to your placeholder string. If it's the one you're after, you can do anything you want with the shape in question.
Sorry for the vague answer, but your images don't work anymore.
Edit: you can work with Shape.Name for your comparison (you mentioned this property in a comment), but I have no idea how you'd set the value from the interface, without using VBA, in the first place, so if you're making templates the approach I outlined above might be easier for users (see https://msdn.microsoft.com/EN-US/library/office/ff939233.aspx for Shape.Name). There is also a .Name property for page objects (https://msdn.microsoft.com/EN-US/library/office/ff940382.aspx), so you should be able to do something like ActiveDocument.Pages("page_name").Shapes("shape_name").TextRange.Text = "your content" once you've figured out how to actually set the name values
Edit 2:
You can also try to use search and replace as per Replacing Text in Microsoft Publisher Using Powershell if you don't need to do anything advanced beyond placing some text
Edit 3: Given the title of your question, unless you can figure something out with Publisher's interface, you can set the .Name property of the selected text box (or other shape) with dim shape = Selection.ShapeRange.TextFrame.Parent and shape.Name = "your_name". You can set the name of the selected page with ActiveDocument.ActiveView.ActivePage.Name="your_name". (Create a VBA macro that prompts you for names and you should be good to go)

Specifying location of new inlineshape in Word VBA?

I'm working on a document "wizard" for the company that I work for. It's a .dot file with a header consisting of some text and some form fields, and a lot of VBA code. The body of the document is pulled in as an OLE object from a separate .doc file.
Currently, this is being done as a Shape, rather than an InlineShape. I did this because I can absolutely position the Shape, whereas the InlineShape always appears at the beginning of the document.
The problem with this is that a Shape doesn't move when the size of the header changes. If someone needs to add or remove a line from the header due to a special case, they also need to move the object that defines the body. This is a pain, and I'd like to avoid it if possible.
Long story short, how do I position an InlineShape using VBA in Word?
The version I'm using is Word 97.
InlineShape is treated as a letter. Hence, the same technique.
ThisDocument.Range(15).InlineShapes.AddPicture "1.gif"
My final code ended up using ThisDocument.Paragraphs to get the range I needed. But GSerg pointed me in the right direction of using a Range to get my object where it needed to be.