In powerpoint VBA, how to change the spacing of a font in a textbox? - vba

Do you know what the command in VBA is in order to space out some text?
I looked on the internet and didn't find anything that would work.
this "SOMMAIRE" textbox has no spacing
and this, is what I want that textbox to look like :
there is more space in between the letters
more exactly, I am looking for the VBA code for this exact button :

Use the shape's .TextFrame2.TextRange.Font.Spacing property. By default it is probably 0. You can change it to other values to increase the font spacing.

Just wanted to add that auto-kerning is also a factor so you'll want to remove that too:
'Set character point spacing to zero
.TextFrame2.TextRange.Font.Spacing = 0
'Remove automatic kerning
.TextFrame2.TextRange.Font.Kerning = False
The kerning property can be viewed manually if you select the shape and in the Font group on the HOME ribbon, click the notch in the bottom right and then go to the Character Spacing tab - you will see a checkbox there where this property is set.

Related

Update [Style] to Match Selection option on Character Style via macro

I'm having an issue with some character style that doesn't reflect the style change when applied (this doesn't happen always):
Example: I have a character style for italics, and when I apply the character style, the word still appears as normal (but the char style is applied, and checking the properties, the style has the check ok for italics).
In order to fix this, I select the word with the issue, right click on the style and use the option "Update [StyleName] to match selection", and it displays the italics correctly.
The problem here is that when I try to replicate this behavior with a VBA Macro (via recording macro), the macro that Word writes has this error:
Run-time error '5900': The property is not allowed for character styles. This is the line with error:
ActiveDocument.Styles("StyleItalic").AutomaticallyUpdate = False
Looking at the code created via Macro, seems that it is not possible for a character style to be automatically updated.
The character style is also created via macro, and I can't see anything wrong in the style:
Private Sub Creo(style As String, fontName As String, fontSize As Integer, hasItalic As Boolean)
On Error Resume Next
Selection.ClearFormatting
ActiveDocument.Find.style = ActiveDocument.Styles(style)
ActiveDocument.Find.Execute
If ActiveDocument.Find.Found = False Then
ActiveDocument.Styles.Add name:=style, Type:=wdStyleTypeCharacter
ActiveDocument.Styles(style).QuickStyle = True
ActiveDocument.Styles(style).font.Size = fontSize
ActiveDocument.Styles(style).font.name = fontName
ActiveDocument.Styles(style).font.Italic = hasItalic
End If
End Sub
Is there a way to fix this? Hope I explain myself. I am working with 1000+ pages Word Document, so this becomes a bit too tedious for manual editing. Also, sometimes the style in other word works ok, but other doesn't. (All the words have both paragraph style and character style)
Thanks!
Your question describes two unrelated issues.
Applying a character style with the same property as the underlying
paragraph style will cause that property to be turned off in the
text. Updating the character style to match the selection will have
the opposite effect to what you want.
To demonstrate: in a new document type a paragraph of text and apply
a style that is defined as italic, e.g. Quote or Intense Quote.
Select the whole paragraph and press Ctrl+I to turn off italics. Now
select just part of the text and apply the character style named
Emphasis. You will see that it has no apparent effect on the text.
This is because both the paragraph style and the character style are
italic, cancelling each other out and having the same effect as
turning italics off manually.
Now right click on Emphasis and select Update to Match Selection.
The selected text will now be italic but, as the text preview in the
Quick Styles gallery will show you, Emphasis is no longer italic.
Only paragraph and linked styles have the Automatically Update
property, which is why you get an error when attempting to set it on a character style.

Powerpoint VBA to search for and change colour/highlight keywords in textbox

I am very new to VB and am exploring this method to simplify mundane manual work process of highlighting certain text in Powerpoint textboxes.
My intention is for VBA to search for keywords in the textbox, then changes the colour of this line and also a few other lines. e.g. search for the line that contains the word "video", if it returns that line 7 contains this word, I want to change the colour of line 7 and maybe lines 3, 10 and 11 to red colour.
Since your question is generic, We can only give a generic response.
First thing you need to know about VBA in powerpoint for your issue is that you need to access things like objects. You'll first need to access the current Slide and Shape your textbox is in. In this example, Let's assume the textbox you want to access is in the first slide, in the first shape:
Set oTextbox = ActivePresentation.Slides(1).Shapes(1)
With oTextbox
text = .TextFrame.TextRange.Characters.Text 'To access the textbox text.
If InStr(1,text,"some_text")
.TextFrame.TextRange.Font.Color.RGB = [255 0 0] 'To change the color of a textbox.
End If
End With
.TextFrame.TextRange.Characters.Text accesses the shape's text.
To search for a given text in the textbox, you can use the InStr
command to see if the text you want is in your textbox.
.TextFrame.TextRange.Font.Color.RGB accesses the text's color.
This is at least a start for you.

VBA in Word 2010: how do I fix the relative position of a TextBox?

Writing VBA in Microsoft Word 2010 (no-one's favourite job). I'm trying to fix the vertical position of a textbox to a location in the document, so that as text is added before, the text box retains its relative position (i.e. moves down if text is inserted before the location it's linked to).
My code is
ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 372, 400, 120, 120).Select
With Selection
.ShapeRange.TextFrame.TextRange.Select
.Collapse
.TypeText Text:="Text box placement test"
With .ShapeRange
.Select
.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
...
The key I think is this last property, RelativeVerticalPosition.
I have tried changing it to wdRelativeVerticalPositionLine: this works for the first paragraph (i.e. the text box is fixed to the position in text, so if text is added before it moves correctly): however, for the second and later paragraphs the vertical position is completely wrong.
I have tried changing the LockAnchor property but that makes no change.
The frustration is that this can be done manually (Page layout, Arrange group, Wrap text, more layout options, position, move object with text = checked) but Word won't let me record a macro where I change a textbox's properties, so I can't find the combination of settings to make it work.
Any suggestions? Or is this just one of the consequences of using 20+ year old code?
First, you've got to set RelativeVerticalPosition as well.
Second, be careful with using .ShapeRange. Incorrent usage of .ShapeRange may be the problem in some cases.
That's a small snippet from my code. Just tested it with Word 2010.
Dim oShp As Shape
Set oShp = Selection.ShapeRange(1)
oShp.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
oShp.RelativeVerticalPosition = wdRelativeVerticalPositionLine
So if your "move object with text" is unchecked, it'll become checked after running the code.

Getting display text as ###### for some of the cell in excel after writing from Vb.net code

I am writting to an excel file from my vb code. The code goes as below
xlsheet3 = xlBook.Sheets.Add(After:=xlSheet)
With xlsheet3
.Columns(5).NumberFormat = "#"
.Cells(j + 1, 5) = someStringValue 'Here "j" is a row counter and this line is in a "for loop"
end with
After writing to excel, most of the cells in excel are correct. But some of the cell's text comes as ####### however if I click on the cell, formula bar shows the correct result. I have tried giving single code before adding the text still that did not help.
Please help me in resolving this.
Thank you
There is not any issue with your code. You need to increase the width of the column or have to use word wrap. In excel if your value is not fully visible it shows it is "######".
If widening and wrapping text doesn't work and the format is set to text which allows display of only 255 characters, try changing the format to general.
This just indicates that the cell is too small for showing the result: make it wider.
See https://superuser.com/questions/65556/excel-displays-for-long-text-whats-wrong for some common reasons why Excel displays "######" in cells.
Either the cell is too narrow to display the contents or the contents are over 256 characters.
Check what you're writing to the cell. If it's not too long then all you need to do is resize the column to fit the new contents.
This is simply what Excel does when the data in a column is too wide to be displayed in the current column width. Make the column slightly wider and you will see all your data.
To autosize the column so it is wide enough to display all its data, double click the column divider at the right edge of the column, in the header bar.

Eliminate stray whitespace between textboxes on a report

I have 4 stacked textboxes in the body of an SSRS report and am getting a stray space / extra line between textboxes 3 & 4.
This is for an address block - name / title / email / website. Can't put it in a single textbox with intervening vbcrlf tokens because the email and website are links. I've tried formatting it to remove vertical spacing; also calculated the exact position by taking top + height to calculate the position. And of course I've tried positioning it so there are exactly 0 pixels between the text boxes. If I reverse the position of #3 & #4 the rendering looks the same so it isn't stray formatting characters in the data fields.
The solution is to wrap the stacked boxes in a rectangle.
I had this problem as well. It blew my mind until I started over on another part of the form. The new boxes worked perfectly until I moved them to the right of another set of text boxes which had some word wrap in them. I realized the wordwrapped boxes were directly related to the gaps I was seeing the set of textboxes to the right. I guess there's some kind of poor markup going on that tries to line things up horizontally and enclosing the set of textboxes in a rectangle protects them from it.
good idea on putting the info into a table - jumping off that idea - I'm going to construct a dynamic string in my query and output the dynamic string into a textbox. thank you for the idea, I don't know why I didn't think to do that.
Simpler thing is to just check text alignment - the default is "default" which appears to be centered. Changing the text box to the right to "left" fixed this problem for me.
Reduce padding property of the textbox.
Once dragging the textbox one closer to the other the tooltip shows convergence points between two textboxes - make tooltip show 0 points
it is best I could do to control the spacing