Insert custom word template icon into word template table - vba

I have Integrated a tab name "Philip Prosenber" and a button (also added custom Bulb icon for this button) to run a VBA macro using office Custom UI Editor.
My .docm template "Springer Publishing" looks like this:
My plan is to insert a textbox for the user so that they could use predefined format which should look like this (with all requirements):
But someone suggest me that to:
use a table otherwise it won't get the lightbulb automatically centered vertically in the text box.
Use a quick part/building block instead of using code.
so I used following code to enter a table into .docm template:
Sub Insert_Table_Textbox()
Set newDoc = ActiveDocument
Set mytable = _
newDoc.Tables.Add(Range:=Selection.Range, NumRows:=1, _
NumColumns:=2)
mytable.Cell(1, 1).SetWidth ColumnWidth:=InchesToPoints(1.3), RulerStyle:=wdAdjustNone
mytable.Cell(1, 2).SetWidth ColumnWidth:=InchesToPoints(5.3), RulerStyle:=wdAdjustNone
mytable.Shading.BackgroundPatternColor = -603917569
mytable.Cell(1, 2).Range.InsertAfter "<Enter information content here>"
mytable.Cell(1, 1).Range.InsertAfter "icon" ''here I need to select insert icon which I already have embedded in given template.
mytable.Cell(1, 2).Range.Select
End Sub
how do I insert that bulb icon in cell 1? so that it should look like my required Table.
bulb icon is already added to template using custom user interface editor by Microsoft.
Note: I am using MS word 2010-13 both.
.docm template so that anyone can use it.

You need to adjust the cell's vertical alignment and paragraph format. Here is an example code that will do that and you should modify it to integrate with your existing code.
With ActiveDocument.Tables(1).Cell(1, 1).Range
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Cells.VerticalAlignment = wdCellAlignVerticalCenter
End With

An alternative method requiring no code.
Ensure that document has been saved as a template (.dotx or .dotm)
Add a two cell table and format as required. Insert icon and size as required. Insert a Content Control (see Developer tab) into cell where text is to be inserted.
Select table and from the Insert tab select Quick Parts, then Save Selection to Quick Part Gallery
In the dialog box give your building block a name and select an appropriate gallery for it. The “Custom” galleries aren’t displayed by default so are useful for custom ribbon tabs.
Now delete the table you created.
Edit the ribbon xml for your template to add the gallery you saved the building block into.
<control idMso="CustomTablesGallery" size="large" label="Custom Tables" />
The user will insert the building block from the gallery.
You now have a self-contained template that can be distributed to other users without dependencies on other files.

Related

MS Word: Trying to dynamically update a text field when I change pictures

I create scannable QR codes cards in word for users to report problems. Each time I change the QRCode image, I have to change the Caption under the image to match it to the computer we are labeling. Typing this caption takes time, as 9 cards fit on a single page and each has to be updated when we insert/change the image. I'm trying to figure out how to dynamically change the caption each time I change the picture. I am adding an image as an example below. ANY Help would be appreciated.
enter image description here
I have tried looking at VB to set a label and reference the image filename (which us created with the computer name) and just remove the extension, but I can't figure out how to set the properties for the object and dynamically link them. I have little VB.Net scripting and haven't used it in almost 10 years.
Assumning you need a solution for manual data entry into your 9 cards/labels (even though a mailmerge would work better for bulk card/label creation), if you configure each of them with:
a DISPLAYBARCODE field where your image shows the QR code; and
a text content control where your image shows the serial #,
the following ContentControlOnExit macro, placed in the 'ThisDocument' code module of your document or its template will automatically update the QR code in each label/card to match the serial # whenever its content control is exited.
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
With CCtrl.Range.Cells(1).Range
.Fields(1).Code.Text = "DISPLAYBARCODE " & CCtrl.Range.Text & " QR \q 3"
.Fields.Update
End With
End Sub

Use VBA with the Word FormatFont dialog to change Content Control text name

I have been trying various ways to allow a user to easily update the text font name in a couple of Word Content Controls on a contract template. There is a form that is used to edit the control's text which can have a command button to run the procedure. I would like to make it easy and isolate the user from the Content Controls which are delicate things ;)
This is what I would like to do (I have tried many permutations of this code):
Dim dlg As Dialog
' Initialize the dialog
Set dlg = Dialogs(wdDialogFormatFont)
' Open the font dialog
dlg.Show
' Set the contentcontrols' text font name to the chosen one from the font dialog
ThisDocument.ContentControls(1).Range.Font.Name = Dialogs(wdDialogFormatFont).Font.Name
Instead of dlg.Show use dlg.Display. The Show method combines both Display and Execute, so you won’t be able to get the chosen font to apply.
See the VBA reference for further info.
Edit:
'check that the user pressed OK
If dlg.Display = -1 Then
MsgBox dlg.Font
End If
This works...I recorded a macro to get the answer. Sorry I bothered you all:
Set dlg = Dialogs(wdDialogFormatFont)
' Open the font dialog
dlg.Show
' Show the selected font name
MsgBox Selection.Font.Name
...any font attribute from the font dialog can be gleaned from the .Font property and then applied to whatever text you wish ;-)

Macro to Modify Content Control Properties for Directory of Documents

I have over 300 documents with a table coverpage, within one of the cells is a content control box with incorrect properties (the title and the tag are both named wrong). I can so far create a macro to fix the properties but only after I click into the cell by opening each document one by one. Is there a way to run a macro that can find this content control box in a table and amend the properties and save?
To search for a Content Control with a certain Title and/or Tag in a document and then change the Title or Tag, you'd use code something like this ...
Dim cc As ContentControl
For Each cc In ActiveDocument.ContentControls
If cc.Range.Information(wdWithInTable) Then
If cc.Tag = "InErrorTag" And cc.Title = "InErrorTitle" Then
'then correct the Tag and Title
cc.Tag = "CorrectedTag"
cc.Title = "CorrectedTitle"
End If
End If
Next
To then perform this replacement in a batch process of updating multiple documents you need additional code.
There is a Wiki article Batch Editing MS Word Documents that provides cross platform (Windows and Mac) VBA code for that purpose. You can merge the article's code with the Content Control replacement code above to accomplish you task.

MS-Word VBA reference identification

I have a custom content control in Microsoft Word from a third party I am trying to resize the width and height of. Normal content control selection via VBA is not working because this control has no Title or Tag. However, if I manually select the object and resize it programmatically using "Selection.ShapeRange.Height = x" or ShapeRange.Width it does work. So to do it all programmatically I need to determine the name of the "selection" without having to manually select it.
Is there a way to "inspect" the complete reference to the currently selected object in word, so we can then get a starting point to work with it in VBA?
It's hard to know what type of object your dealing with. I tested this by inserting a blank ActiveX image control, selecting it and then running the macro. The code has two methods but one is commented out.
Sub FindName()
MsgBox (Selection.Fields.Item(1).OLEFormat.ClassType)
'MsgBox (Selection.InlineShapes.Item(1).OLEFormat.ClassType)
MsgBox (Selection.InlineShapes.Item(1).Field.Index)
MsgBox (Selection.InlineShapes.Item(1).AlternativeText)
'Show current name
MsgBox (Selection.Fields.Item(1).OLEFormat.Object.Name)
'Set new name
Selection.Fields.Item(1).OLEFormat.Object.Name = "Image5"
'Re-display name to show that it changed
MsgBox (Selection.Fields.Item(1).OLEFormat.Object.Name)
End Sub
The result was this:

Accessing ScreenTip text

I have a long MS Word doc that has many bookmarks/hyperlinks. The only purpose of the bookmarks is to be able to use the screen tips to view additional information about what is bookmarked.
Is there a way to access this screen tip text, and store it in a variable?
Eventually I will use this to create a macro that will change the displayed text to include the screen tip text (so the information can be printed, when necessary).
Here's how to print-to-console all the Hyperlink ScreenTips in your document:
Sub printScreenTips()
Dim link As Hyperlink
For Each link In ThisDocument.Hyperlinks
Debug.Print link.ScreenTip
Next
End Sub
Note: I'm not aware of a way to add a ScreenTip to a bookmark, but you can add a ScreenTip to the hyperlink that points to a bookmark.