I have a VB.NET project in which there is a form where there is a TextBox control, a ListBox control and an external text file that contains a list of outlook folder paths for client emails.
Essentially, the user enters into the text box the name of a client and/or their unique reference number, presses the search button (yes - I know I could make the results appear as they type, I want a button!) and it comes up with the matching results for the company name or serial number that are in the text file and puts them in the list box, with the full path of the outlook email folder.
For example:
If I put into the textbox: "06967759-274D-40B2-A3EB-D7F9E73727D7"
It would put the following result into the listbox:
"EIS Admin\Contacts{06967759-274D-40B2-A3EB-D7F9E73727D7}"
And the user can then go to that folder and find the email(s).
I have gone through several revisions both of my own code and code pasted from online with people having the same issue, only to have Visual Studio throw no errors, run the code and have no luck, with it doing nothing but clearing the list box, and not showing matching results of any kind.
I understand this may be a repeat question but I am extremely confused, can't get anything to work and need some help regarding my issue.
Here is the current code (from online - not mine):
lbx_OFL_Results.Items.Clear()
Dim i As Integer
For i = 0 To lbx_OFL_Results.Items.Count - 1
If i > lbx_OFL_Results.Items.Count - 1 Then Exit For
If Not lbx_OFL_Results.Items(i).Contains(tbx_FindText.Text) Then
lbx_OFL_Results.Items.Remove(lbx_OFL_Results.Items(i))
i -= 1
End If
Next
The list box is called "lbx_OFL_Results"
The textbox is called "tbx_FindText"
I start by clearing the list box of all items (when the form loads, it fills the list box will all lines of the text file, so I need to clear it).
Form Load Event Code:
Dim lines1() As String = IO.File.ReadAllLines("C:\ProgramData\WPSECHELPER\.data\Outlook Folder Wizard\outlookfolders.txt")
lbx_OFL_Results.Items.AddRange(lines1)
For the rest of the code it seems to be doing some form of a 'sort search' then removing any excess results.
If anyone can suggest edits to my code, or new code then that would be sublime.
Thanks.
Thanks to #Jimi for the answer.
Code:
listbox.Items.Clear()
listbox.BeginUpdate()
For i as Integer = 0 To lines1().Length - 1
If lines1(i).Contains(searchbox.Text) Then
listbox.Items.Add(lines1(i))
End If
Next
listbox.EndUpdate()
I have another question which solves how to make this search non case-sensitive. It can be found here.
Related
I have a Microsoft Word document with .docm format. A first glance it does not contain any macros (as when clicking the following on the ribbon; View -> Macros -> View macros pops up a window having an empty list).
But when enabling the Developer ribbon tab, and clicking the Visual Basic icon there, and then selecting the Document and ContentControlonEnter from the dropdowns in the VB window the following code appears:
Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
Dim i As Long, j As Long
With ActiveDocument
If ContentControl.Title = "Classification" Then
ContentControl.DropdownListEntries.Clear
For i = 1 To .ContentControls.Count
If Left(.ContentControls(i).Title, 5) = "Level" Then
j = j + 1
ContentControl.DropdownListEntries.Add Text:=j & " - " & .ContentControls(i).Range.Text
End If
Next
End If
End With
End Sub
Selecting the other options in the dropdowns give only "blank" code (that is they contain only function declarations followed by theEnd keyword).
My question is what is the code meant to do?
*
Details:
The Word document in question contains hyperlinks to parts of the same document and a couple of links to Word files and Excel files of the same folder. It also contains lots of content control boxes, which I'm guessing is the focus of the code (as the code contains the ContentControl keyword)
Content controls can trigger macros when the user enters and exits them. Microsoft made the design decision that all content controls should trigger the same "events" - Document_ContentControlOnEnter / Document_ContentControlOnExit - and that the code in the event needs to check which content control was entered / exited.
Content controls are considered as part of the Document because the Document can trigger events. That's why they're in (and MUST be in) the ThisDocument class module.
(Note: View Macros can only show you PUBLIC SUB procedures with no arguments that are located in "normal" code modules. Any Private Sub, any Function, anything that takes a parameter and anything in a class module will not appear in that list. So you can't use that list to determine whether a document contains any code.)
The If ContentControl.Title = "Classification" Then checks which content control was entered. (Note: it usually makes more sense to use Select Case rather than If, especially when the event needs to distinguis between multiple content controls.) What's inside the If only executes if it was a content control with the Title "Classification". (Note that more than one content control can have the same Title, so more than one content control could run the code.)
If another content control is entered, the event is still fired, but nothing happens (in this case).
Catalin Pop correctly explained that the code is, in essence, "resetting" the drop down list.
Legacy Form fields use a similar pattern - macros can fire when the user enters/exits an form field. But the design for that was you had to create a Public Sub and assign that to the form field in the Properties.
I think the logic here is quite simple.
Basically the code searches for a content control named Classification within the entire document.
After it finds it, it clears all of its drowdown entries - like a reset.
After the cleaning part it again searches through the entire document for all content control that start with word "Level" and it collect the text for those controls and their order in appearance.
With this info collected it then fills the dropdown optios for the classification control above. (e.g. 1 Level X, 2 Level Y.. - based on what it finds in the document for controls starting with Level in their name)
I've searched and found similar topics but none that really answer my question, or perhaps it's the fact that I'm a beginner and I don't fully understand yet.
What I'm trying to do is use a ComboBox and a TextBox. Once the ComboBox selection changes, the TextBox will load more information about the item in the ComboBox. This is for my company, trying to store canned messages with a title.
So I want to have a title for these messages in a ComboBox drop-down, and once a title is chosen, the message is displayed in the TextBox. Right now, I've got the message creation, which is also part of my program, appending to two different text files, since I'm not sure yet on how to append data from two controls onto the same line and concatenate a Tab or other delimiter.
So my question is, what method should I be going about doing this? The part I am REALLY not understanding is how to get the line number value from the selected item in the TextBox, they load just fine using the following line:
savedmsgComboBox.Items.AddRange(File.ReadAllLines("MessageListTitles.txt"))
So how do I get which line this selection is on, and how do I correspond that line with the message I need to display in the TextBox?
This will only work if both arrays have the same amount of lines. Load both files into arrays.
'declare the arrays
Private msgTitles() As String
Private msgList() As String
'in load event fill them
msgTitles = File.ReadAllLines("MessageListTitles.txt")
msgList = File.ReadAllLines("MessageList.txt")
'load the combobox
savedmsgComboBox.Items.AddRange(msgTitles)
'get the items by the index
Dim msgListItem As String = msgList(savedmsgComboBox.SelectedIndex)
I am working on a MS Word form for a client where they want the ability to count the number of words, check spelling, have a character limit, and have the rest of the form locked down so that the end user cannot change anything they are not supposed to. I have attempted to convince them that word count and character limit are redundant if we have proper instructions, however, this is what they want. They also want the form to be able to "work" even if the user does not enable macros, meaning, they want it locked and a character limit first and foremost.
I know that if we just rich text content controls and put the form into a group spell check and word count word while also "locking" the remainder of the form, except user content controls do not allow for character limits and using legacy/activex controls in a grouped form locks those controls as well.
So, for now, I have settled on using the ActiveX Textbox (this is negotiable if I have a reason to use the legacy textbox) and have achieved the minimum "workability" (if that's even a word). The only way I have figured out to check the spelling is below:
Sub chkSpelling()
Activedocument.Tables(1).Rows(26).Cells(1).Range.Text = txtRole.Text
Activedocument.Tables(1).Rows(26).Cells(1).Range.checkSpelling
txtRole.Text = Activedocument.Tables(1).Rows(26).Cells(1).Range.Text
'... so on and so forth throughout each text box
End Sub
The issue is that this is not good enough for my standards. Printing the text of each textbox (up to 1700 characters) at the bottom of the screen each time I need to check the Spelling is unacceptable. Does anyone else have any ideas?
Thank you for your assistance.
Answer with help from #bibadia
Dim doc As Document
Set doc = Documents.Add(, , wdNewBlankDocument, False)
doc.Paragraphs(1).Range.Text = txtRole.Text
doc.Paragraphs(1).Range.CheckSpelling
txtRole.Text = Replace(doc.Paragraphs(1).Range.Text, Chr(13), "")
Use CreateObject to create a new word instance
Ensure .Visible = False (I think that is the default)
Create a new document in that instance.
Copy the text to be checked into that
Spell check.
Remove the document and the Word instance.
If I understand your question correctly then,
I need to check the Spelling is unacceptable. Does anyone else have any ideas?
If text boxes are named with numbers, you might be able to use loop, and save your coding lines with time.
want the form to be able to "work" even if the user does not enable macros
If user did not enable macros, he would not be able to see form (is it User Form, you want to say) and no background macro code would run. (Tested on Ms-Excel 2007), ultimately failing all.
regarding text box control on user form
yes that can be set to character limit from properties menu, and spell check can be done using your method.
Set range2 = Documents("MyDocument.doc").Sections(2).Range
range2.CheckSpelling IgnoreUpperCase:=False, _
CustomDictionary:="MyWork.Dic", _
CustomDictionary2:="MyTechnical.Dic"
In VBA for Word 2007, I want to be able to open a document, highlight sections of text and replace those sections with fields linked to a docvariables. The process would be:
Open document.
Select text.
Select docvariable from list.
Insert field linked to selected docvariable.
Repeat steps 1-4 as required.
There is no way to know beforehand what the text to be selected is or which docvariable is going to be linked to which field or how many times these steps are going to be repeated.
Only with Microsoft could the most absolutely fundamental, simple task of allowing the user to make a selection at run-time and pass this selection back to sub-routine be so tortuous and surreal. I have spent 2 days trying to figure this out. If anyone can help, I will name my next child after you.
I think "tortuous and surreal" is a misconception.
Create a small form with a dropdown (named "selVarName", for example) that lets you select all document variable names available. Link the form to a custom button in the Quick Access Toolbar.
Upon clicking "OK" in this form do something like this:
Private Sub btnOK_Click()
Dim v As Word.Variable
Dim n As String
n = Me.selVarName.Value
With Selection
For Each v In .Document.Variables
If v.Name = n Then v.Delete: Exit For
Next v
.Document.Variables.Add n, .Range.Text
End With
End Sub
And this has bells and whistles already. You can do additional checking like "no text selected", for example.
Code in Form onLoad:
country_combo.RowSourceType = "Value List"
Code in a reset function:
Dim lListIndex As Long
With Me.country_combo
For lListIndex = .ListCount - 1 To 0 Step -1
.RemoveItem (lListIndex)
Next lListIndex<br/>
End With
Code to populate country combo:
*For n = 1 To numCountries*
*countryCombo.AddItem (countryRS.Fields("countryName"))*
*countryRS.MoveNext*
*Next n*
I'm having a problem that occurs AFTER the code to populate the country combobox runs. The values are there as I can run Debug.Print(countryCombo.Value) and it prints out the name of the selected country, but I can't see the values in the combobox at all. They're invisible, and as far as I know there is no visiblity property for specific items, unless I'm completely mistaken.
comboBoxError.png http://img110.imageshack.us/my.php?image=comboboxerror.png
I think you should probably use Access's GUI tools to do what you're looking for. In design mode, click on the field you are trying to populate, then click the "lookup" tab. You can then specify a table to populate the field with and your forms should automaticly update as well.
I've also seen what you describe here - as far as I can tell, it's a bug within Access (I was using 2007) that only occurs when you programatically mess with the contents of a combo box. It does not happen every time. The issue corrects itself if you highlight the text that is in the combo box.
I am experiencing a similar issue with Access 2003. Based on the selection of one combo box, the row source of a listbox is set to an SQL string Basically a SELECT DISTINCT [MyField_Selected] FROM MyTable. For some fields the values are visible in the list box and others it is not. The values are there however as I can access them via code. To make it more interesting it works fine in Access 2007.
Just found the resolution on another forum. Check the format property of the field(s) in question on the table. In my case, when Access 2007 created the table, it put an # format in there. I removed that and all works great!