MS Word VBA Selection.Range only works after 1st page - vba

I am using the following code to insert my signature after writing a report. The template for the report is pre-determined, and I cannot change or add tags/bookmarks to it. Our admin fills out the top part, and then sends the corresponding person the document. After which we write it up.
Sub Signature()
Dim pic As Shape
Set pic = ActiveDocument.Shapes.AddPicture(FileName:="C:\My Documents\My Pictures\CompressSig.png", _
LinkToFile:=False, _
SaveWithDocument:=True, _
Left:=0, _
Top:=-25, _
Width:=140, _
Height:=50, _
Anchor:=Selection.Range)
pic.WrapFormat.Type = wdWrapBehind
End Sub
The problem is, on my one page reports, it will stay at the top left, unless I manually adjust the Top/Left attributes. If it's a two page report, the signature goes right where the cursor is (which is where I want it to go). Why is selection.range only working on the first page and/or is there something else I can do? Thank you!!!

A shape's anchor is not it's position. Instead, it is the paragraph to which the shape is anchored. Here's an explanation of how anchors work.
You probably don't want a Shape. Instead, use InlineShape. This inserts a shape inline with text, which makes more sense for a signature.
But wait! Word already has a feature to do what you want! Building blocks (or AutoText) can save a graphic to your template. You can save your signature as a building block, then give that entry a keyboard shortcut and place your signature wherever the insertion point is blinking. No programming required.

Related

Combining Rich Text Content Control Content in MS Word using VBA

I'm trying to create a form for a non-technical user in MS Word to capture some text content in MS Word. This word doc consists of several rich text content controls where the user will type in or paste in some formatted data (bold, underlined, links, ...).
Once they get all the content entered into these various content controls I'm trying to make it easy for them to combine them together to paste in a consistent order into some podcast show notes which is in an HTML form.
So basically, I want to take three rich text content controls that have formatted data in them, combine them together into one formatted piece of content, and then copy it to the clipboard so they can then go to this web form, paste it in, and do some minor cleanup. The problem is that whenever I try to combine the RTF content it loses the formatting.
The only way I seem to be able to keep the formatting is if I copy the range object and then paste it. However, this doesn't paste just the formatted text. It pastes the whole rich text content control.
I've tried creating a blank RTF field at the bottom of the Word doc to combine everything in but I just can't figure it out. I wouldn't think this would be rocket science.
Being none of the code I've tried works and keeps the formatting I"m not sure if posting it here will do any good. Here's how I'm getting the value of the text object:
ActiveDocument.SelectContentControlsByTitle("txtShowNotes").Item(1).Range.Text
tried this:
ActiveDocument.SelectContentControlsByTitle("txtShowNotes").Item(1).Range.Copy
ActiveDocument.SelectContentControlsByTitle("txtCombinedContentSection").Item(1).Range.Paste
but this copies the whole RTF and not just the text.
Try something based on:
Sub Demo()
Dim Rng As Range
With ActiveDocument
Set Rng = .SelectContentControlsByTitle("txtCombinedContentSection").Item(1).Range
Rng.FormattedText = _
.SelectContentControlsByTitle("txtShowNotes").Item(1).Range.FormattedText
Rng.InsertAfter vbCr & vbCr
Rng.Characters.Last.FormattedText = _
.SelectContentControlsByTitle("txtShowNotes").Item(2).Range.FormattedText
End With
End Sub

Looking to Create a Macro for Printing Addresses to Envelopes

First question on here, so hopefully I have followed all the guidelines etc.
I am looking to create a macro for a Word document which prints a headed paper copy, a yellow paper copy and an envelope (based on the address section of the letter). I have the paper copies sorted but I am having difficulty with the envelope.
My understanding is I should be able to use a field, where the address would be typed in and then use the field in the macro to print the envelope out. I have the envelope printing side of this sorted too, in that I can get an envelope to print from the document, just not get it to print only the address.
Whenever I use fields it does not seem to allow me to type into it, it just deletes it out. I have never used Fields before, so this may be something very simple I am missing. Based on some Youtube videos I watched it looks like MacroButton is the field I need to be using?
If anyone was able to assist me in how to use fields for this it would be greatly appreciated.
Currently my code only includes the printing side of things (envelope currently not included):
Sub OrCo()
'
' OrCo Macro
'
'
With ActiveDocument.PageSetup
.FirstPageTray = 259
.OtherPagesTray = 259
End With
Application.PrintOut FileName = "", Range:=wdPrintAllDocument, _
Copies:=1, Pages:="", PageType:=wdPrintAllPages, Collate:=True,
PrintToFile:=False
With ActiveDocument.PageSetup
.FirstPageTray = 260
.OtherPagesTray = 260
End With
Application.PrintOut FileName = "", Range:=wdPrintAllDocument, _
Copies:=1, Pages:="", PageType:=wdPrintAllPages, Collate:=True,
PrintToFile:=False
End Sub
You probably want to look at the ask field as this field links the entered text to a bookmark which makes it easier to access the entered text from vba.
MS help for the Ask field
This if you have an ask field of the form
{ask street "Enter the street name"}
then from VBA you would be able to access the text using
activedocument.bookmarks("street").range.text
NOTE: The ask field does not display the entered text. To do that you need to add a separate ref field (or field containing just the bookmark name). This allows you to put the ask fields anywhere you like and use multiple ref fields to display the result.
You may also want to review the envelope printout method.
MS help for the envelope printout method
Good luck with your endeavors.

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.

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)

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.