Add a button to a word textbox - vba

I want to add a button to a textbox in a word document.
The button should stick to the textbox like this:
----------------------------------------
word document content
text, text, a textbox follows
------------------[button1]-
|text box content |
----------------------------
text, followed by another textbox
------------------[button1]-
|textbox2 content |
----------------------------
more text
---------------------------------------
I have been able to add a button using vba, but I don't know how to make it stick to the textbox since I can have more instances to textboxes with buttons.
I would like to do this using macros, but a C# addin solution is also welcomed.

I'm afraid it isn't possible to overlay a button to a textbox like you show, and make them stick together. To overlay them, you need to set their layout wrapping style to something else than "in line with text" (e.g. "Square") but then Word won't let you group them (at least not my Word 2003) so in principle they can be moved relative to each other.
However if you create a button and a text box next to each other and leave the wrapping style be "in line with text" which is the default, then they will stay side by side and not move relative to each other (unless you type text in between them).
Selection.InlineShapes.AddOLEControl ClassType:="Forms.CommandButton.1"
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.InlineShapes.AddOLEControl ClassType:="Forms.TextBox.1"
Then you can your other textbox-button pairs on the following lines.

Related

Change the color of a selected record in an Access REPORT

My Access REPORT has a text box with the Record ID that looks like a button with an on click event to go to a form for that specific record. This works great, but when I return to the report I cannot see which record was clicked. I want to temporarily change ONLY the record that was clicked until another record is selected.
The reason I want this on a report and not a form is because I want the user to have a quick way to proof read in the format needed to print, and make a change or check a detail if necessary, then update the report AFTER all proof reading and updates are completed and before final print. But with many records on the screen it is easy to lose track of which record you were checking when returning from the form.
I tried:
Private Sub btn_txt_GoToTransaction_Click()
Dim vColor
vColor = RGB(51, 204, 51) 'green
Me.btn_txt_GoToTransaction.BackColor = vColor
DoCmd.OpenForm "Account_frm", acNormal, , "[TransactionID]=" & Me.TransactionID
End Sub
But this does not work because every button turns color not just the selected record.
Any suggestions? Thanks.
This is a great question because there are many benefits to highlighting a row or item in an Access Report. You are not able to just change the button color in one row only, but you can highlight the whole row so the user knows where they were.
Here are two methods to accomplish this:
Method 1 - Click on a Label
This works great in newer versions of MS Access when using Report View. Use a Label Control instead of a Button. You could make the label look like a button if you format it that way. I prefer to stretch an invisible Label across the whole row on top of all the other controls in that row. Then if you click anywhere in the row, it automatically selects that row and then runs whatever code you have in the OnClick Event. This works best if the Label is not linked to a Text Box.
This picture shows an example of how this method looks. You can click anywhere in the row and it highlights that row with the red outline and grey background.
This is very simple and works well but there are a couple disadvantages:
1- You can not change the color of the highlight.
2- If any of the text boxes CanGrow, the row height may be higher then the Label and create areas where the invisible label doesn't capture your click.
3- Clicking on a Text box does not work for this method.
Method 2 - Change Color of a Text Box
In order to just highlight one row or one piece of data in a report, we can use the "FormatConditions" property. This is the same as Conditional Formating from the MS Access design interface but we are going to change it programmatically on the fly. You can't do this with a button or label - it needs to be a Text Box with unique data, such as your TransactionID.
This picture shows an example of how this method looks. You can set the color of the highlight if you follow the steps below.
STEP 1) I recommend that you add a text box to your report that stretches from the left to the right, set the Back Color and Fore Color to White, set the Control Source to TransactionID, and set the Name to TransactionID. Then right click on this text box and select Position > Send To Back. This works best if the other text boxes and labels on the report have a transparent background.
STEP 2) Add this code:
Private Sub HightlightRow(intRowID As Integer)
With Me.TransactionID.FormatConditions
.Delete
With .Add(acFieldValue, acEqual, intRowID)
.BackColor = vbGreen
.ForeColor = vbGreen
End With
End With
End Sub
STEP 3) Also change your button code to call this subroutine like this:
Private Sub btn_txt_GoToTransaction_Click()
HightlightRow Me.TransactionID.Value
DoCmd.OpenForm "Account_frm", acNormal, , "[TransactionID]=" & Me.TransactionID
End Sub
STEP 4) I like to set it up so if the user clicks anywhere in the row, it will pop up with a modal with more detail regarding that row. Also, the user can't make any changes to the data in the Report View, so I use the pop up modal to allow changes. To accomplish this, I do a couple more things:
First, we need to add the code to the OnClick event for every control in that row. Ofcourse, each OnClick event will simply can that subroutine HightlightRow Me.TransactionID.Value
Second, if the user clicks on a Text Box, the Text Box gets the focus and hides the highlight. Therefore, I like to set the focus to something else. In your case, you could set the focus to the button by adding this line to the end of the HighlightRow subroutine: btn_txt_GoToTransaction.SetFocus
In my case, I am not using a button, so I set up a tiny Text Box with = " " (just an equal sign a space in quotation marks) as the Control Source. Then I position this tiny Text Box to the far right. And in the HighlightRow subroutine, I set the focus to this textbox.
STEP 5) You may also want a button or method of removing the highlight. To do that simply have the code run this line:
Me.TransactionID.FormatConditions.Delete

VBA PowerPoint - fill in textbox based on another textbox content on different slide

I am a beginner with VBA, and I was wondering whether VBA could help me to automate presentation creation process.
My presentation template has on the top of each slide a textbox, which I would like to fill in with the text from a different slide.
Example:
Slide 1 (Divider slide): includes only 1 textbox with text e.g. 1) Project scope
Slide 2, 3, 4 ... (Content slides): include title, charts, other text box, etc. - but also 1 "descriptive" textbox placed always in the same place on the top of each slide
I would like this "descriptive" textbox to include text from Slide 1 (Divider slide).
Text in the "descriptive" textbox would change every time a new Divider slide would be detected, i.e. different "descriptive" textbox for each presentation part.
Not sure whether this can be done with VBA as I could not find anything similar online.
Thank you a lot for any information!

Set text box value in Word VBA

I have a Word 2010 form template with various text boxes, combo lists and radio buttons. I wish to take the value from one text box and insert it as the value of another text box when a Save button is invoked. I have extracted the value from the source text box with the code below but how do I then insert it as the value of the target text box? I have tried a few things including the reverse of the code below but so far without success.
srp = ActiveDocument.SelectContentControlsByTag("RPI").Item(1).Range.Text
The Range.Text property is read/write. So you use the exact reversal of the code you have.
srp = ActiveDocument.SelectContentControlsByTag("RPI").Item(1).Range.Text ' Read
ActiveDocument.SelectContentControlsByTag("SRP").Item(1).Range.Text = srp ' Write

Which text field should I use in word and how to fill it

I am trying to use a text field in my word template document but I confused which one should I use?!!
which one you recommend?
Rick text
Text
Text Form Field
My second question is how to address these component in Macro? I use below code:
ActiveDocument.FormFields("TextboxName")
Third question is how to set a value to this component in Macro? so many website use .Value but I don't know why I cannot find this field in above component.
Last question is: if I use any form field, a gray shadow exist below my component, how I can remove it? it is even coming in check print.:(
I am using word 2007.
You would add : textFormField.
you would pass value by Me.FormFields("Text1").Result = "abc"
in your controls tab,
somewhere next to the formField control find the "formFieldShadding"
control. when you select that control your gray shadding will
disappear.

Excel: Object Text Box can't do Carriage Returns in Protected vs. Unprotected Sheet states (w/ Text Unlocked)...why?

First and foremost, my due diligence rounded up a ton of answers regarding ActiveX Text Boxes, but nothing really about Object Text Boxes. For my project, I cannot use any ActiveX.
OK, so when a Sheet is Unprotected, an Object Text Box (from Insert > Shapes) works pretty much the way I want it to: the most important thing being that I can hit the Enter key and get a carriage return. Then, I go into the Shape Properties, and uncheck Lock Text, and protect the sheet.
Once the sheet is protected, though, the ability to do carriage returns (type Enter, and go down one line) goes away. Shift+Enter and Alt+Enter are no-goes as well.
Is it just not possible to have this functionality available? Are there any workarounds? Why does Excel hate me? Here are some of my ideas:
Unprotect Sheet when Text Box is clicked/activated, Protect when not
(couldn't figure out the syntax in VBA for this. "If Intersect..." is what I'm thinking)
Insert Word Doc Object (don't like this because one-click enters the
formula bar editing, and I can't get the font to stay)
Just use a merged cell and instruct users to double-click to enter
and use Alt+Enter for a new line.
The winning option for now is using a merged cell, but I may just have to see if ActiveX will work on our network. I really want to stick to the KISS principle here if at all possible for the end user...I don't mind coding in the backend to make it work, though.
Thank you for your thoughts!
EDIT: Here's some images to help...
Here's the functionality that I would like to have when my Sheet is protected:
Next, this is an ActiveX text box with it's properties window displayed (Developer > Design Mode > Properties). The properties that make it somewhat usable when the Sheet is protected are circled in red, the Multiline and Enter Key Behavior. But again, I'd prefer to not have to use ActiveX...plus, the user cannot change font color by line.
Finally, I found this interesting: There is another Text box under Form Control that is grayed out. From a search, it looks like this was taken away in favor of the drawing objects version of the text box...or maybe it's the same? The left is the drawing objects one, the middle is the grayed out Form Control, and the right is the ActiveX.
In sum, I would just like to see if there is a way to have the functionality of an unprotected Sheet's Shapes Text Box when the sheet is protected.
Assuming you are working with a TextBox shape, inserted from the ribbon, here:
Then you can use the optional parameter in the Protect method:
Sheet1.Protect DrawingObjects:=False
This will allow the user to edit text boxes on the worksheet, but the sheet itself will remain protected.
If you are using a Form Control (inserted from the Develper/Design ribbon) then you can set the .MultiLine property by accessing the shape's OLEFormat.Object:
Sub test()
Dim tb As Shape
Dim x As Object
'Get a handle on the SHAPE
Set tb = ActiveSheet.Shapes(2)
'You have to access its properties from the OLEFormat.Object:
tb.OLEFormat.Object.Object.MultiLine = True
End Sub
In my test, even on a Protected worksheet this allows the user to Shift + Enter to insert carriage returns:
Ctrl + Shift + Enter seems to work on a protected sheet.