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

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.

Related

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.

How to change data label width in an Excel chart with VBA?

I want to change a label width in Excel chart by VBA code:
set lbl = SERIES1. points(1).datalabel
msgbox lbl.width 'this is working
lbl.width = 40 ' compile error: wrong number of arguments or invalid property assignment
I can get the label width but cannot change it. What am I doing wrong?
AFAIK, the width of a chart data point label is not configurable. Even though the width can be retrieved with VBA, it's not possible to set or change it with a VBA command or property.
According to the documentation, the DataLabels.Width property is read only.
Returns the width, in points, of the object. Read-only.
Source
Excel chart labels remain stubbornly uncooperative and resist formatting attempts, be it with VBA or with the UI.
That's (unfortunately) just how Excel works.
Don't shoot the messenger.
If you want to make a difference, consider raising an idea at excel.uservoice.com
This works in Excel 2016, you'll just need to adjust the series collection and point numbers to fit your needs. ActiveChart.FullSeriesCollection(1).Points(1).DataLabel.Width = 363.314.

Different PPT templates reacting different on the same macros

I'm new here, so I might have not seen a possibility to upload my problem files, which would make it easier to describe the problem.
Edit: The files a here: https://drive.google.com/file/d/0B--IbmtX58h8TnVrdlRyUXZ5a2dEOVJBQkplVjFuVEVMVXhJ/view?usp=docslist_api
and: https://drive.google.com/file/d/0B--IbmtX58h8TFR6d3FkWlZpSGFVUGF5bHVhRTR5ZTlnbXAw/view?usp=docslist_api
(Thank you for the idea, Steve)
What it is about:
I have two documents with different master templates reacting completely different on the same set of macros and I have no idea how and why this can happen and how to repair or avoid it.
Two of the macros just create objects - one is a single textbox, the other one a group of a rectangle and a textbox . The first mentioned appears on the position defined in the code in one of the templates, but a bit below it in the other one. Even more strange is the behavior of the group. The rectangle appears on the correct position in both of the templates, the textbox only in one of it.
Next is a macro for increasing the paragraphing between text lines by 3 pt. It works fine in one template, but in the other template it increases the spacing by 43.2 pt!
Macro number four is made to set back the paragraphing space after back to 0. This one works fine in both templates.
Funny enough, the mistakes appear in opposite to each other. The single textbox and the group produce their error in the template, where the spacing tool works fine, and the spacing tool does strange things in the template where the single textbox and the group work well.
Any idea will be appreciated!
Thanks,
RG
I work with PowerPoint 2010.
Your footnote is getting misplaced because the default text settings in one presentation are different from those in the other; in this case the auto fit setting.
' in this section of your FOOTNOTE routine:
With .TextFrame
' Add this next line and it will work as expected
.AutoSize = ppAutoSizeNone
.TextRange.Text = "Note: " & vbCrLf & "Source: "
.VerticalAnchor = msoAnchorBottom
Likewise, in your SectionMarker subroutine:
With .TextFrame
' add this
.AutoSize = ppAutoSizeNone
' then the rest of your code
Then it all works as you'd expect. Or at least, it works the same with both templates.

Word VBA Get Selection (Text) Background Color - But Not Highlighted or Shading

I have a Word Document that was created by saving a PDF document as a Word Document. The PDF document has some text that was highlighted in Yellow. I need to detect the highlighted text. I guess the conversion is not perfect. Some of the text in the Word Document is showing that it is highlighted, and some just has a Yellow background but is not showing as being highlighted. I can select some of the text and click on the highlight button on the menu and it shows a highlight color, and for some of the words, it shows No Color.
Also in the VBA immediate window if I type
?selection.formattedtext.highlightcolorindex, I get 7 for some selections, and 0 for others.
For those selections that do not show up as highlighted, how can I detect them?
Update: I selected a "Highlighted" word and executed call Selection.ClearFormatting and the font changed, but the yellow highlight remained. So that tells me that the yellow highlight is not a format. I also selected No Color with the text selected and the yellow highlight remained. I have verified that it is neither Highlight or Shading. Hmmm... What could be causing the highlight?
Update: I discovered that if I delete the "highlighted text" that is not really highlighted, the text that is to the right of the deleted text moves over to the left and the highlight remains. So then I discovered that I can Select the highlight if there is no text under a portion of the highlight. Then I can drag the highlight to another portion of the page. So, this highlight is an object. If I right click on it, I have options such as Format Object.
So now I know that these "Highlights" are actually like objects. If there is no text under the object, I can grab it and drag it around. How can I, using VBA, find these types of objects?
What I really want is the entire paragraph under the highlight objects. Is that possible?
Well, I figured out how to do it. I used another StackOverflow question to lead me to the answer How do you select an image that was just pasted to MS Word via VBA
I am running this code from Microsoft Access on a Microsoft Word object.
Public Sub ReadWord2(strPath As String)
'Const wdColorRed = 255
Dim objWord As Object
Dim objDoc As Word.Document
Dim strParagraph As String
Dim intShapeCount As Integer
Dim Shape As Word.Shape
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Open(strPath)
Set objSelection = objWord.Selection
intShapeCount = objDoc.Shapes.Count
'objDoc.Shapes.SelectAll
For Each Shape In objDoc.Shapes
'If Shape.AutoShapeType = 1 Then
Shape.Select
'Debug.Print Shape.Fill.BackColor
'Debug.Print Shape.AutoShapeType, Shape.Name
'Debug.Print Shape.TextFrame.TextRange.Text
objSelection.Expand wdParagraph
Debug.Print objSelection.Text, Shape.Fill.BackColor
'End If
Next
End Sub
There is actually more code in the sub, but I deleted the parts that are not relevant. Hopefully this will help someone else who runs into the same problem. The objSelection.Text will contain the paragraph text that the shape is part of. Now I just need to write code to handle the text and make sure that it is text that I want.

Word VBA: .Hide function doesn't hide?

I took an old MS Word document to adapt it with a new layout.
I finished last week and everything was working fine, the main macro has to hide or display some text.
For this, a zone of text is "bookmarked", and then we get this bookmark and set its font to hidden:
ActiveDocument.Bookmarks("MyBookMarkname").Range.Font.Hidden = True 'Or False
It's how it was done on the old document, and I had only to do the same on the new document(recreate those bookmarks).
But today, when trying again to make this action, the text isn't hidding anymore! When it is supposed to be hidden, the text is like underlined by a small blue line(the same line you have when an word is not spellt correctly, but in blue).
I searched online, I found several things, but none of them worked:
Private Sub HideHiddenText()
For Each myWindow In Windows
myWindow.View.ShowHiddenText = False
Next myWindow
End Sub
I've no "revision mode" enabled either.
What could be wrong?
I believe the wavy blue line that Word is displaying is being triggered by the hidden text because Word uses the blue line to mark formatting inconsistencies. To get rid of the line in Office 2007/2010 go to
Office Orb Menu (2007) or File Menu (2010)|Options|Advanced
and uncheck Mark formatting inconsistencies
The wavy blue line, however, has nothing to do with your hidden text being displayed. I believe this is happening because the "Show/hide formatting marks" function is turned on. To make sure your hidden text is kept hidden by vba, you will need the following:
With ActiveDocument
.ActiveWindow.View.ShowAll = False 'Hide all formatting marks
.ActiveWindow.View.ShowHiddenText = False 'Do not display hidden text
.Application.Options.PrintHiddenText = False 'Do not print hidden text
End With
It is worth noting that an experienced Word user can always choose to display hidden text via Word's user interface and that if this is to be avoided, a great deal of additional work would need to be invested to disable the native Word functions that can be used to display hidden text (if that is even possible).