Insert caption method in VB/VBscript - vba

I am trying to format an inserted image caption using vbscript. I have tried the following but it doesn't work. I couldn't find any InsertCaption method that allows font formatting. On some forums I did find out that its possible to change caption style from word settings but couldn't find a vb code for the same and in any case that is ineffecient. I am trying the following. The first line works perfectly. The rest has no impact.
objselection.InsertCaption "Figure", ": " & Object_Title , "", wdCaptionPositionAbove
objSelection.Font.Color = RGB(107, 143, 122)
objSelection.Font.Name = FontNameCaption
objSelection.Font.Size = FontSizeCaption
objSelection.Font.Style = FontColorCaption

The inserted caption does not become part of the image or the selection. It's just another paragraph with a particular format. You could do something like this to change the formatting of all captions:
For Each para In ActiveDocument.Paragraphs
If para.Style = "Caption" Then
With para.Range
.Font.Color = RGB(107, 143, 122)
.Font.Name = FontNameCaption
.Font.Size = FontSizeCaption
.Font.Style = FontColorCaption
End With
End If
Next
However, a better approach would be to modify the "Caption" style in your document template, so that you don't need to modify the caption format afterwards.

Related

Word VBA Macro - Center+bold+caps first line of text

I am trying to center the first line of text on a document, which would usually be the title.
I am able to center the line with
Selection.Paragraphs.Alignment = wdAlignParagraphCenter
But I am not sure how it is selecting the file line, as I would also like to set the title to bold and caps.
Selection.Font.Bold = wdToggle
Selection.Font.AllCaps = True
_
Also is there a way to "detect" any text that is centered already and has an empty space(line) above and below it, or would that be too difficult to achieve?
Use a with statement to apply multiple formats. Here is an example:
With Selection
.Paragraphs.Alignment = wdAlignParagraphCenter
.Font.Bold = wdToggle
.Font.AllCaps = True
End With

Word vba macro find text and get its position to replace it with a textbox

I aim to replace a certain text in a ms word document with a textbox. This textbox needs to have the replaced text in it. The textbox should appear at the same position like the replaced text and the following content must wrap around the textbox.
With the following code I can accomplish the replacement of the text with a textbox and with the replaced text in the textbox, but the textbox is always at the same place.
What I need now, is the possibility to get the position of a found text to use it for the textbox to replace the found text. :)
My code snippet:
positionLeft = 50 /* here I need the left position of the found text */
positionTop = 50 /* and here the top position */
With ActiveDocument.Range.find
.Forward = True
.Wrap = wdFindStop
.Text = TextToReplace
.Execute
If .Found = True Then
Dim Box As Shape
Set Box = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, positionToLeft, positionToRight, 100, 100)
Box.TextFrame.textRange.Text = TextToReplace
.Parent = ""
End If
End With
Is it even possible to get the absolute left and top position of a text in the word document? And if yes, how can i get this done?
You can use Selection.Information(wdVerticalPositionRelativeToPage), Selection.Information(wdHorizontalPositionRelativeToPage) to find the coordinates but it is just relative to that page.
I haven't tested this but if you add
.Select
x = Selection.Information(wdVerticalPositionRelativeToPage)
y = Selection.Information(wdHorizontalPositionRelativeToPage)
Set Box = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,x, y, 100, 100)
It should work.

Word VBA next paragraph style not updating

I shamelessly recorded a macro to amend the default heading styles 2 - 5 to change their .NextParagraphStyle to ones of my own making called Normal_lvl2, Normal_lvl3 etc :
With ActiveDocument.Styles("Heading 2").ParagraphFormat ' etc etc
.LeftIndent = CentimetersToPoints(1.13)
.RightIndent = CentimetersToPoints(0)
.LineSpacingRule = wdLineSpaceDouble
.Alignment = wdAlignParagraphLeft
.FirstLineIndent = CentimetersToPoints(-0.63)
.OutlineLevel = wdOutlineLevel2
.NoSpaceBetweenParagraphsOfSameStyle = False
.AutomaticallyUpdate = True
.BaseStyle = "Normal"
.NextParagraphStyle = "Normal_lvl2" ' here is the next style
End With
Problem is the document doesn't actually update the next paragraph style, either when I run the macro or set a style for a line manually. The new style works fine for the actual header line but the next paragraph is not changed.
I did try to loop through all paragraphs and set the style but it took far too long (I quit after 20 mins run time, the doc is 160 pages). Specifically I got all headings into an array, used Find to return a range for each of the headers in the array and set the next range style depending on the heading level. Maybe not the best way but I'm not too familiar with the Word Object Model.
So my question is - is there an efficient way to automate the application of my custom styles and to ensure the next paragraph style is also changed?
You should iterate over all paragraphs in your document and then adjust the following paragraph accordingly like it is done in the following sample:
Sub ChangeParagraphsAfterHeading()
Dim para As Paragraph
Dim nextPara As Paragraph
For Each para In ActiveDocument.Paragraphs
If para.Style = "Heading 2" Then
Set nextPara = para.Next
If Not nextPara Is Nothing Then
nextPara.Style = "Normal_lvl2"
End If
End If
Next
End Sub
I assume that you probably want to adjust the style for all paragraphs between two headings. The sample above doesn't do that yet, but it should get you started.

How to create a custom textbox with set text, font type, font size, font colour in Powerpoint?

I need to create a custom textbox in the active slide with set text, font type, font size, font colour, and bold and italics feature in Powerpoint 2007 using a macro.
I am currently using this code:
Sub InsertTextBox()
Set myDocument = ActivePresentation.Slides(1)
Set newTextBox = myDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, _
100, Top:=100, Width:=541.44, Height:=43.218)
With newTextBox.TextFrame.TextRange
.Text = "Slide Title"
.Font.Size = 24
.Font.Name = "Arial"
.Font.Colour = RGB(107, 107, 107)
End With
End Sub
Its throwing an exception in .Font.Colour saying that this property is not associated with the object. Also i need to incorporate a feature for bold and italics..Plz help me on this
You want
.Font.Color.RGB = RGB(107, 107, 107)
And as Kazjaw points out, it's .Color rather than .Colour
You can use Find on a range to locate a string within a textrange and return a new textrange consisting of just the found text:
With newTextBox.TextFrame.TextRange
With .Find("Slide Title")
.Font.Bold = True
End With
End With
In practice, you'd want to use Instr to verify that the text you're looking for is indeed in the text box.

How do we change styles in a word contentcontrol via VB.Net

I'm trying, since a few days, to control a word document via vb.net. I've put some contentControl in it in order to mark the location where I have to make automatic changes.
Writting in it is really easy, replacing also.
Writting a continuous text with a lot of paragraphs is a little more tricky but I manage to do it via functions.
Where I have more problems is by writting one title in a "Style1", a subtitle in a "Style2" and the text in "Normal style".
When I write this :
With tfDocx.BodyCC("startFormulas").Range
.Style = tfDocx.Doc.Styles("Titre 2")
.Text = "Produits"
End With
I have the good text in the good style. But when I add this code:
With tfDocx.BodyCC("startFormulas").Range
.Style = tfDocx.Doc.Styles("Titre 2")
.Text = "Produits"
End With
With tfDocx.BodyCC("startFormulas").Range.Characters.Last
.InsertParagraphAfter()
.Style = tfDocx.Doc.Styles("Titre 3")
.Text = "essais"
End With
The .InsertParagraphAfter is not taken into account and when I debug it I have a single line "Produits essais" in my word document with needer of the two styles.
Does someone have an idea?
Converting your code to VBA (second part where you add 'essais' text) I would have this one:
With CC.Range.Characters.Last
.InsertParagraphAfter
.Move wdParagraph '!!!!!!!!!
.Style = "Nagłówek 1"
.Text = "essais"
End With
As you can see I've added one line with '!!!! comment moving insertion point to next paragraph which was add with .InsertParagraphAfter method.