Hi Mike
On the word.mvps.org website you'll find a couple of articles dealing with
automating Word from Excel, and vice-versa. Start with those and see how far
they take you... Then you can ask questions with a little less scope, that
are easier to answer without writing an entire book
On some of your points:
1. It may be best to have the user select the Word document from within your
application, so that you can control opening it, and thus have a "hold" on
what was opened. Things can get very tricky if many documents are open, and
your code needs to work with only a specific one. Letting the user pick a
file, then picking up that file path and opening the file through code lets
you generate a document object (instead of "activeDocument") to manipulate.
If everyone will be using Office XP or later, then the FileDialog object
would be ideal for this. If you have to accomodate earlier versions, as well,
then you should ask in the excel.programming newsgroup what interface they
recommend for letting the user pick a file path.
2. The routines you show can certainly be run from Excel VBA, using the
automation techniques described in the article. They aren't the "prettiest"
Word VBA, but if they work for you, that's fine
You'll only need to watch
out for a couple of things, if the code is running from within Excel:
- Don't use "ActiveDocument". Instead, when opening the Word document (as
discussed above), assign it to a document object variable and use that in
place of ActiveDocument. Roughly:
Dim oDoc as Word.Document
Dim rng as Word.Range
Set oDoc = wdApp.Documents.Open("Filepath.doc")
Set rng = oDoc.Range
- Where you have Selection, consider using Range, instead. If you decide to
stick with Selection you MUST prefix it with the Word application object.
Otherwise, Excel will try to use its Selection object, which won't work
Roughly: wdApp.Selection
open the text file from within the Word VBA?>>
Yes, that would also be possible. Basically, the reverse of the above
discussion, where you automate Excel through Word (also on the word.mvps.org
site).
-- Cindy