Updating the text displaid in MACROBUTTON in MS Word - vba

I am using macrobutton in VBA to have a field whose value is calculated by accessing some other system, and at the same time, I want to be able to double-click on that field to specify some settings used to retrieve data from that other system.
The whole macro stuff works fine but I can not figure out how to change the label on the macrobutton.
Typically the macrobutton looks like this { MACROBUTTON macro_name label {some_arg}{some_arg2} }
I tried accessing selection.fields(1).code.text and even doing regexp to replace 'label' by something else but that just does not work, i.e. either I lose the args or I screw up the label.
Any advice for this issue, or perhaps a suggestion of some other type of field I could use to achieve this? I wouldn't mind using DOCVARIABLE but these can not respond to clicks and carry arguments?

You should be able to do something like this:
Sub Testit1()
Dim strText As String
Dim strLabel As String
Dim strNewLabel As String
strLabel = "Chew"
strNewLabel = "Devour"
' Replace the field code values in the first field to change the label
strText = ActiveDocument.Fields(1).Code.Text
ActiveDocument.Fields(1).Code.Text = Replace(strText, strLabel, strNewLabel)
End Sub
This will basically do a search and replace inside the field code for the macrobutton field you want to change. ActiveDocument.Fields(1).Code.Text is the part I think you are looking for.

Related

Access VBA - Type Mismatch when working with Combo Boxes (Multi Valued Fields)

I have a form that displays a single record at a time, and allows the record to be edited by displaying it all in Text Boxes and Combo Boxes. Some of these are ComboBoxes based off of lookup fields, where the values are pulled from a preset list (multi-valued fields).
After that, I have a class module with a property defined for each field in the record (a FirstName Property, a LastName Property, an Address Property... you get the idea). I have a function that creates an object from the class, and then takes the values from the form and assigns them to the corresponding property. This works fine for most of the fields, but as soon as it gets to the first Combo Box (multiple selection), it throws a Type Mismatch error. Code I'm using is:
If Me.Issue <> vbNullString Then
ProfileObj.Issue = Me.Issue
End If
'Me.Issue is the combobox on the form - this is in the forms module
'ProfileObj is the class instance
In case you wanted to see the Property in the class module for the ProfileObj object:
Private ProfileIssue As String
'... other variable declarations
Property Get Issue() As String
Issue = ProfileIssue
End Property
Property Let Issue(rData As String)
ProfileIssue = rData
End Property
I've also tried using Me.Issue.Value, Me.Issue.Text, and Me.Issue.Column(0) to refer to the Combo Box, but none of these have worked either. I even tried using CStr(Me.Issue), to no avail. How can I take whatever's displayed int the combo box and assign it to a String variable?
I figured it out...
I needed to read the text from each Combo Box with each box's .Text property. I had tried that inside of the If statements, but not for the actual comparison that the If statement was built on. The working version of the code now reads:
Me.Issue.SetFocus 'You have to set focus in order to read the text, dont ask me why
If Me.Issue.Text <> vbNullString Then 'This is where my code wasn't working
.Issue = Me.Issue.Text 'I had tried it here before, but the code never got there since the line before failed
End If

Insert/replace string in existing text form field in Word (VBA)

So I'm trying to create a conditional dropdown in Word. I've used a Legacy Drop-Down Form Field with multiple options.
What I want to happen is when one of the options is selected from the dropdown (and then I guess you have to Tab to enter it...), the Legacy Text Form Field below, that has a simple default text, should populate with a new string from the case statement.
Things that I've already done/got working:
the drop down is working, and on exit it runs a macro
case statement is written out
the result of the case statement is in a variable (string type), let's call it StringVar
can pull the text form field's text (default text) with ActiveDocument.FormFields("TextBox").Result
What I figure out, however, is how to replace the default text in the already existing Text Form Field with the case statement's string variable text.
I've tried
ActiveDocument.FormFields("TextBox").Result = StringVar
but it doesn't change anything inside the text form field. The default text is still there.
So I can answer my own question after pondering and being hit with an "oh, duh" moment.
I had unprotected the document (ActiveDocument.Unprotect Password:="") in order to do this:
ActiveDocument.Content.InsertAfter Text:=("This is my text")
because I was wondering if I was grabbing the right string.
Turns out,
ActiveDocument.FormFields("TextBox").Result = StringVar
DOES work. BUT the file has to be protected (filling in forms) for it to replace the string in the text form field. Otherwise, nothing shows up even though the result had indeed been updated. Fancy that.

Bold a certain value of a specified column in a specific grid - vb.net

I have miniature problem. First, I'm using vb.net.
What I want is to get the same value from a column that I could only those values thicken and in some way to express more than in other in my gridview (devexpres grid).
In my picture you can see the name of the first column and the cell values. I want only the name 'KLASA' TO CHANGE IN THE SAME NAME ONLY BOLD (for each cell named 'KLASA').
Thank you!
You should use the RowCellStyle event:
Using the example found on that page, you can get the class that makes up the row like this:
Dim View As DevExpress.XtraGrid.Views.Grid.GridView = sender
Dim ClassX As MyClass = View.GetRow(e.RowHandle) 'This would be the type of data you set as the datasource for the grid
Then read you value or do whatever you need to here to figure out if it should be bold and set the appearance like this:
e.Appearance.Font = New Font(f.FontFamily, f.Size, FontStyle.Bold)

Extracting Substrings from textboxes in VB

I am working on a VB program for school. I am having some trouble extracting a substring from a string and I would really appreciate some help.
The form has different text boxes and one of them is where you type in a person's full name into one text box. The listbox on the form, when hitting the compute button, is supposed to display only the person's last name.
I am not sure how I am supposed to extract just the last name of the string of whatever name is typed into the text box.
All I got so far is:
Dim name As String
name = txtName.Text
(txtName is the name of the text box)
Okay, so I added:
lstOut.Items.Add(name.Substring(6))
That to my code. The name I typed in for an example when I ran the program was Helen Woods. 6 is in the substring because that is where the space starts and when I clicked compute, it listed only the last name, just like I wanted. But, this only works if the first name is five letters long. I need a way to make the program automatically find the space in between the two names.
EDIT:
When I add:
lstOut.Items.Add(name.IndexOf(""))
The listbox gives me a 0 whenever I type in a name and hit the compute button.
try this:
Private Sub GetLastName()
dim lsName as new List(Of String)
dim name as string
for each name in txtName.text.split(" ")
lsName.Add(name)
next
lstOut.items.Add(lsName.item(lsName.count-1))
end sub
You can call this procedure at your button event.

Typing filename in ListBox

when I wish to type the name of a listbox item (the listbox is populated by files in a directory), for example, if I type "apples" pressing A would take me to the first object with A in it's name, but typing "p" after will take me to the first item with p as the first letter. Is there any way I can make it so I can type a few characters and it would take me to that specific item? For example, the list might have;
ability
idea
boring
typing "abi" would select ability, rather than "ability", then "boring", then "idea"
Any help is appreciated. Thanks.
I think it's possible to make System.Windows.Forms.ListBox behave like that, but it would take some non-trivial code to make it work. System.Windows.Forms.ListView has this behavior built-in, so I would suggest using a ListView instead of a ListBox.
' Hide the headers to make the ListView look like a ListBox.
Me.ListView1.View = View.Details
Me.ListView1.HeaderStyle = ColumnHeaderStyle.None
Me.ListView1.BeginUpdate()
Try
' System.Windows.Forms.ListView doesn't have data binding capability.
' The listview's items have to be added using its
' Items.Add, Items.AddRange or Items.Insert methods.
For Each filename As String In Directory.GetFiles("C:\Windows").Select(Function(s) Path.GetFileName(s))
Me.ListView1.Items.Add(filename)
Next
Finally
Me.ListView1.EndUpdate()
End Try
' Add the column after adding the items.
' Setting column width to -1 will make
' the column autosize itself to the longest item.
Dim columnHeader As New ColumnHeader
columnHeader.Width = -1
Me.ListView1.Columns.Add(columnHeader)