Change Info in Word from Excel VBA - vba

I have a letter in Word that needs to be sent out to a number of contacts (example of letter). The only thing that needs to change in the letter is the recipients information
(example of how recipients info is stored), but I don't know what the best way to change this info is.
I did a little bit of research and what I've come across is that people either do a search and replace or use fields. I suspect that there is a better method than search and replace since I know exactly where the text needs to change. I'm thinking there might be something similar to named ranges like in excel?
Now this is where I came across fields, but to be honest I don't really understand them. I know they have a name but this whole equation thing throws me off. So my question is how do I change this information in the quickest/most efficient way?
I understand how to access and save word documents and am planning on using a loop to go through each row of contacts one by one, so if you have a solution you can leave that part out (unless you know of a better way!).

Related

Programmatically change paragraph placement in Word range

I seem to be stuck with a rather simplistic problem when writing a macro for MS Word, I even feel quite embarassed for myself.
I have a range of paragraphs (content don't matter here) that need to be sorted by a given criteria (sorting is not the case here as well) - p1-p2-p3
After sorting let's say we'll have p2-p1-p3
I seem not to be able to code the very process of paragraphs exchanging places e.g.
The usual approach for variables does not really apply to Word's objects.
I'm not really looking for a code sample (but will look into ofc), but for a hint of what to approach to use. Thanks

Count frequency of words in a list

I start by saying that I am not an expert user of VBA; I am just learning it.
I have a question regarding the possibility of counting text strings in Excel using a Macro. I cannot use the 'Countif' function, as there are more than 20,000 text strings in the Excel file, and it would be a lengthy procedure I guess.
I was therefore thinking about the possibility of using a VBA code to scan and organize text strings by their frequencies.
For instance, there are a set of words like this in my file (each in a different cell):
home,
balcony,
flat,
apartment,
home,
home,
garden,
thing,
revenue,
flat,
...
window
I would like to know if there's a way to make Excel scan all the different text strings and come back with the frequency for each of them.
I am aware that probably there are easier way to do it (e.g. Python or some add ins), but we have to do it at work, and I can't really ask my colleagues to learn Python or other programming languages, nor we have the capability to buy any software at the moment.
Thanks to those who can help!
Simple Pivot Table (without VBA - but could be code if needed)
See Debra's great website

Excel VBA - how to handle many cell references without creating a mess

Suppose you have an Excel workbook and you need to write a macro that takes inputs from many different cell references, and updates values at many different cell references, how do you keep the code neat and maintainable?
It would be helpful to know:
best practices
useful tricks
unavoidable difficulties
necessary trade offs
If there are links to existing guides or discussions that would be helpful too. I haven't been able to find any.
Edit: I found http://www.eusprig.org/best-practice.htm very useful - from the European Spreadsheet Risk Interest Group (EuSPRIG).
It slightly depends om what the macro is going to do, but I use 'calculation' sheets, where I gather together the data I need for the macro, and output the results there.
I tend to do this do in defined ranges.
The result data can then be referenced from elsewhere.
Tricks: One thing I do is to create a visual 'check off' for each piece of input data as it's used. This just enables me to make sure that all the data I thought I was going to use, I have used.
Following Naming criteria is better solution , irrespictve of language.
in_name_range, out_name_range
in_num_range, out_num_range
Check points and adding watch , would surely help.
If you are going for modularity, Take care about updated ranges
(As you can't return more than one value at a time better write ranges in some predefined order in some sheet. This works for me.)

What would be an efficient way to see if a word exist when comparing to hundreds of thousands of words?

Iv'e been working with objective-c for while and I want to attempt to build an app. The app will be similar to a scrabble type of game. There are going to be drag and drop tiles (I already know how to program this) and a submit button. The only thing that I'm having trouble with is figuring out how I am going to compare the letters on the board to hundreds of thousands words without bogging down my program to much. What I have in mind, so far, is to store these the words in a database. Does objective-c have any kind of built in api that can access a standard dictionary database? I'm not referring to a dictionary array but rather an actual database with words and possibly definitions. Any thoughts on this?
I have used Lexicontext, it's $20, but it's worth it in my opinion, is extremely fast, there's a demo, and it contains a API for formatting definitions with CSS.
Looks like you're happy with the $20 solution. For the more adventuresome, the data structure you'd want to learn about is tries.
Imagine a tree with 26 children of the root, one for each letter in the alphabet. Now imagine that each child has 26 children, too. You can spell any word of length N by taking N steps from root to leaf. Now imagine that you prune the tree so it contains only valid words. That's your (very fast) word validator. It will take as long on average as your average word length.
Depending on how "Scrabble-like" your game actually is....
Are you going to be validating the word when the player presses submit?
That's not how Scrabble works. The player can play any (non)word so long as the opponents do not challenge the word.
So you'll need word validation in the "challenge" system, but it shouldn't happen as soon as the player plays the word. An unscrupulous player could then just place "maybe" words and press submit, to see if it actually is a valid word.

How to search an entire column for one or two keywords

I am in the process of creating a program, using Visual Basic Express, for a physics professor who has recently had to rename all 1500 questions for his course. I created a database that has the old names, new names, and the entire contents of each question. The program seemed an easy enough idea, but has proved to be very difficult for me. I am new to programming (this is only my third go at it) and have searched for an answer to my problem for at least 20 hours, and have yet to find anything that will work for what I need.
What I would like is to have a text box in which he can type in a keyword (such as "Sun", "ISS", "Force", etc) and for the program to search throughout the entire "questioncontents" column and pull out any questions that contain those words into the datagrid. As of yet the closest I have gotten is for it to search the first word of the column, which is not very useful since most of them start with "The".
I had also thought of creating several keyword columns and then just having it search through those, but I couldn't figure out how to apply the filter to multiple columns.
This may be a stupid question, and if so I apologize, but I am desperate to figure this out as the semester begins relatively soon and I would like to show him how to use it before classes start. Any suggestions or help would be very greatly appreciated.
According to my understanding of the question:
SELECT * FROM question WHERE questioncontent LIKE '%SUN%';
As simple as that, you just have to replace SUN in the above query with the value entered in the textbox