View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Daniel Daniel is offline
external usenet poster
 
Posts: 354
Default Run a Word Macro from Excel VBA

Steve, Faisal,
I am trying to modify a text file(fortran script), very few simple
mods/replacements here and there, then run the shell cmd and wait.

Just need to reach the file and make the substitutions, that's all but I am
not there yet

I will try Steve's code, let you know soon

"Faisal..." wrote:

If it is a txt file then it is worth running everything from one place
(excel or word) by using the open commands:

open FILENAME for output as #1
......


On Nov 2, 4:58 pm, "Steve Yandl" wrote:
Daniel,

Below is an example I had handy that I used in a VBScript file but the code
could easily have been run from inside a VBA routine. In this case, I was
making changes to fields within a specific Word document but could have
easily done a find and replace for other items.

If the file you're working with is an actual text document rather than a
Word document, the "Scripting.FileSystemObject" might be an even more
efficient approach. What type text file is it and how complex is the find
and replace operation?

________________________________

Const wdSaveChanges = -1

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("C:\Scripts\Test1.doc")

For Each itemField In objDoc.Fields
strCode = itemField.Code.Text
strCode = Replace(strCode, "DATE", "CREATEDATE")
strCode = Replace(strCode, "TIME", "CREATEDATE")
itemField.Code.Text = strCode
Next

objDoc.Close(wdSaveChanges)
objWord.Quit
________________________________

Steve

"Daniel" wrote in message

...



Steve,
great,


do you have a sample of the alternate approach, I would much prefer that
thx


"Steve Yandl" wrote:


Daniel,


Here is an example. In this case, my macro was stored in a template but
you
can run a macro from a document file as well. As an alternate approach,
you
can create a reference to the word application and have the same tools
available from your VBA routine within Excel which would have the
advantage
of not being tied to some Word document or template.


Set objWdApp = CreateObject("Word.Application")
objWdApp.Documents.Add("C:\Test\myVBA.dot")
objWdApp.Run "myMacroA"


Steve Yandl


"Daniel" wrote in message
...
How can I Run a macro recorded in word in excel VBA.


Note: the word macro performs a function on a text file that I cant
perform
from excel VBA, which is to get a .txt file and make some replacements
and
save it.


thanks- Hide quoted text -


- Show quoted text -