How do I differentiate between placeholders (Tags, Id, Name) on a template and have the label stay once the template is used in the presentation - vba

I am creating a custom slide template with 8 picture placeholders and 4 text placeholders. The idea is that the user would "fill-in" the template with the text and images and once a macro is run, the values of the placeholders would be collected and added to a JSON file. Ideally, two picture placeholders (big_screen_i and small_screen_i) and one text placeholder (caption_i) would be collected as one JSON object. This set would be repeated 4 times on the same slide.
Here are my problems:
1) The name of the placeholder set in the slide master view changes when more than one of the same template is used (haven't pinpointed the exact trigger). The names also change once an image is inserted into the placeholder. Locating an object by name becomes unstable.
2) Using the .type of placeholder (picture vs text) doesn't differentiate between the big_screen_i and small_screen_i.
3) Tags and keys also change or disappear when using the template more than once (because of the repeated name).
4) I am unable to group the objects because its not possible to group placeholders.
Is there a way to identify each shape/placeholder and connect it to the added content?
Note: I am using powerpoint 2016 .potm file
Thank You!

Related

Replace a Picture in MS Word header with another in VBA

Using MS access I need to open a word document and change certain images within the header. Whilst I have the code to find and replace any text that I need, I do not quite understand how to replace an image keeping it to a specific width and height.
The header within the Word document is constructed using a basic table with 3 columns as depicted below. The image in the right hand column will need to change for a specific logo.
I have managed so far to add an image to the document using the following code which gives the expected result...
With WordDoc.StoryRanges(wdPrimaryHeaderStory)
.InlineShapes.AddPicture FileName:="test.jpg"
End With
I understand that this is due to me not specifying a specific location, size etc but I am struggling to find resources which would instead allow me to either remove and add a new image to the right column or just swap the images out.
EDIT 1:

Store image in Word document to insert later

I am currently building a word-template for a report. In this report are used red separators as part of the design. The separators are basically just images of red, curved lines.
Instead of copy and pasting these separators when needing them, is there a way to store the image somewhere in the document, allowing it to be inserted with just the click of a button in the ribbon? My first bet would be to create a macro somehow that would insert the image, however that would require the image to be stored in a very specific path on the computer.
As this document is gonna be used by lots of people without this image stored on their harddrive, i need another way.
Thanks so much in advance!
You could insert the images into bookmarks created via SET fields in, say, the document header, then reproduce them elsewhere in the document via cross-references. The images in the SET fields wouldn't themselves be visible.
For example:
{SET Image1 "Actual image1"}
to create the bookmark and
{REF Image1}
to display the image.
No code required.
By default, the size of the pictures when inserted via a cross-reference will be the same size as they are in the SET field, plus they'll be formatted as in-line with text.
If they're meant to be full-width images, simply make sure to insert the cross-reference into a new paragraph with 0 indenting.
To constrain the images' displayed size to something smaller than the width they're inserted into the SET field at, insert them into a table whose row height and/or column width has the required fixed dimension.
To apply text-wrapping, insert the cross-references into a table and format the table's wrapping as 'around'.
Note: The field brace pairs (i.e. '{ }') for the above example are all created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac or, if you’re using a laptop, you might need to use Ctrl-Fn-F9); you can't simply type them or copy & paste them from this message.

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)

Using VBA in MS Word 2007 to define page elements?

I'd like to be able to create a page element which I can feed text and it will form itself into the preferred layout. For instance:
{MACRO DocumentIntro("Introduction to Business Studies", "FP015", "Teachers' Guide")}
with that as a field, the output should be a line, the first two strings a certain size and font, centred, another line and then the third string fonted, sized and centred.
I know that's sort of TeX-like and perhaps beyond the scope of VBA, but if anyone's got any idea how it might be possible, please tell!
EDIT:
Ok, if I put the required information into Keyword, as part of the document properties, with some kind of unique separator, then that gets that info in, and the info will be unique to each document. Next one puts a bookmark where the stuff is going to be displayed. Then one creates an AutoOpen macro that goes to that bookmark, pulls the relevants out of the keywords, and forms the text appropriately into the bookmark's .Selection.
Is that feasible?
You're certainly on the right track here for a coding solution. However, there is a simpler way with no code - this is the type of scenario that Content Controls in Word 2007 were built for and with Fields/Properties, you can bind to content controls (CC). These CC can hold styles (like centered, bold, etc.). No VBA required.
The very easiest thing to do is to pick 3 built-in document properties that you will always want these to be. For example, "Title" could be your first string, "Subject" your second string and "Keywords" your third. Then, just go to the Insert ribbon, Quick Parts, Document Properties and insert, place and format those how you like. Then go to Word's start button (the orb thingy) and then under Prepare choose Properties. Here you can type, for example "Introduction to Business Studies", into the Title box and then just deselect it somehow (like click in another box). The Content Control for Title will be filled in automatically with your text.
If you want to use this for multiple files, just create this file as a .dotx (after CC insertion/placement/formatting and before updating the Document Properties' text). Then every time all you'll have to do is set these three properties with each new file.
Well, yes, it did turn out to be feasible.
Sub autoopen()
Dim sKeywords As String
sKeywords = ActiveDocument.BuiltInDocumentProperties(4)
ActiveDocument.Bookmarks("foo").Select
Selection.Text = sKeywords
End Sub
Okay, I have some filling out to do, but at least the guts of it are there.