Hey guys - I just discovered there are ways to use Excel macros to open and edit Word documents, but I can't seem to find much info or examples on how to do a few things. So far I have only been able to input text from my Excel sheet into a specific pre-defined location (via Word "bookmarks"). Anyone have any good websites or info that have some info on the following things?
Excel Sheet for examples:
A B
Field Name - Data
1 Seller John
2 Buyer Jane
3 Price $100
4 Amount 50
5 Terms Terms terms terms blah blah
1) If i set up a Word document that has some text in there like [SELLER], can I have excel go through the Word doc and do a find/replace for [SELLER] and replace it with John above?
2) Say I wanted to insert text somewhere in the Word doc at a bookmarked location, but I wanted to add formatting. For example, I want to underline or bold certain words that I insert. (Like in "terms" above). How can I do that?
3) If I have a checkbox in Word.. how do I toggle checking that on/off from Excel?
thanks.
Hello drdavidge,
Not surprising you haven't found much. Excel is a spreadsheet program and Word is text editor. There isn't a whole lot of crossover between them. One of the most crossover functions is a mail merge. Excel is suited for creating data tables and Word for formatting and arranging the text on the page.
Word has its own Find and Replace function that can be accessed using VBA. All formatting of the document has to be done through Word 's VBA routines.
Your first 2 questions are more Word VBA questions than Excel. The third question is an Excel question, but really doesn't seem germane to the previous two. In cases like this, you really should post the Excel workbook and the Word document.
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
The simplest way of combining Excel Data in a word-document is using Document variables.{DOCVARIABLE} is a field in Word.
You can assign an Excel value to the docvariable and then update in Word.
Sub example() With getobject(C:\example.doc") .variables("seller")=sheets(1).Range("A5") .fields.update .fields.unlink .close -1 End with End Sub
For #2, here's an example:
Excel's cell A1 = "This is what Cell A1 contains"
I have a bookmark in Word called "InsertLocation"
Excel VBA Code:
Word ends up with having this at the "InsertLocation" bookmark location:Dim wrdApp As Word.Application Dim wrdDoc As Word.Document Set wrdApp = CreateObject("Word.Application") wrdApp.Visible = True Set wrdDoc = wrdApp.Documents.Open("h:\file.doc") wrdDoc.Bookmarks("InsertLocation").Range.Text = Range("A1").Value
"This is what Cell A1 contains" (Lacking the intra-cell bold / underline)
How can I have the intra-cell formatting copy over? I tried replacing Range("A1").Value with Range("A1").Text and still had the same result.
Any ideas?
Anyone know how to do this?
As Leith suggested, use mailmerge in Word. That's what it was designed to do.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
You didn't read my suggestion ?
Anyone have any ideas?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks