Board index » delphi » Passing parameters to a Macro.

Passing parameters to a Macro.

<<Shamresh khan:
I am having problems running a macro that has parameters.

Quote

In what program?

--
Deborah Pate (TeamB) http://delphi-jedi.org

  Use Borland servers; TeamB don't see posts via ISPs
  http://www.borland.com/newsgroups/genl_faqs.html

 

Re:Passing parameters to a Macro.


Hi to all,

I am having problems running a macro that has parameters.

I am not sure how to create the macro (with parameters) and then run it
from Delphi, passing edit1.Text, Memo1.Text etc as the parameters.

I would really appreaciate it if anyone can give me an
example on how to do it.

Thanks
Shamresh.

Re:Passing parameters to a Macro.


Hi,

Basically I do not know how to pass data from a form
to a macro in Word. I am not familiar with VBA and
from the documentation they mention using
Application.Run(Macro_Name, varg1...varg30).

I ran a simple macro (which I created in a Word document)
without parameters from Delphi using Word.Run('macro1') and it
worked fine but I do not know how to write a macro
with parameters and also how to pass the information
(via the parameters) from the form to the macro that is expecting
the data. What I want to do is to use bookmarks together
with the passed parameters to insert data in a word document
at specific places.

The above method Run has no parameters to pass.

Thanks
Shamresh.

Re:Passing parameters to a Macro.


<<Shamresh khan:
Basically I do not know how to pass data from a form
to a macro in Word.

Quote

If you have a macro in VBA like this

Public Function AddThese(Int1 As Integer, Int2 As Integer)
As Integer
  AddThese = Int1 + Int2
End Function

You can call it from Delphi like this:

  Arg1 := 3;
  Arg2 := 4;
  Caption := IntToStr(Word.Run('AddThese', Arg1, Arg2));

--
Deborah Pate (TeamB) http://delphi-jedi.org

  Use Borland servers; TeamB don't see posts via ISPs
  http://www.borland.com/newsgroups/genl_faqs.html

Re:Passing parameters to a Macro.


Hi,

Sorry for being a pain in the ****.

I tried what you mentioned in you last email but the method RUN
for the word application object only has macroName as a
parameter. I get the error "not enought actual parameters".

On top of that due to the fact that I have not got a clue with VBA,
when I wrote the procedure you metioned with parameters
in the word visual basic editor, it does not recognise it as
a macro. What I mean here is when I try to run it in the editor,
the macro dialog box does not show it.
Is that because it is now a subroutine??? In which case
how do I call it in delphi???

What I wanted to do was move lots of parameters from a
form (i.e edit.text, memo.text etc)  in a
single macro instead of calling Document.Goto_ to find
a bookmark and then inserting the text with the range
object returned.

Shamresh.

Re:Passing parameters to a Macro.


<<Shamresh khan:
I tried what you mentioned in you last email but the method RUN for
the word application object only has macroName as a
parameter. I get the error "not enought actual parameters".

Quote

The Run method for the TWordApplication is overloaded so that you can
supply a variable number of parameters. If you use a Word
_Application interface you have to supply all 30 possible arguments
for your macro too (using EmptyParam) - or use a variant instead:
  var
    WordVar: OleVariant;
  ...
    WordVar := Word.Application;
    WordVar.Run('AddThese', Arg1, Arg2);
Does that help your problem? If not, is this a compile-time error or
a run-time error, and can you show us some code?

<<Shamresh:
What I mean here is when I try to run it in the editor,
the macro dialog box does not show it. Is that because it is now a
subroutine???

Quote

It's because it takes parameters (and returns a result). Strictly
speaking a macro is something you can call by clicking a button or
pressing a key in Word, so parameters aren't possible. But it doesn't
matter at all from your point of view - you can create functions and
subroutines in VBA and call them with the Run command just the same.

In Word, call up the VBA editor for your document. In the left hand
pane you should have something like Project(MyDocument), and beneath
it (if you've recorded any macros) a Modules folder with a NewMacros
entry. Double click on that and enter this in the NewMacros(Code)
pane on the right:

Public Sub InsertGreetingAtBookmark(Greeting As String)
  ActiveDocument.Bookmarks("SayHello").Range.InsertAfter (Greeting)
End Sub

If you save this in the document, and add a bookmark to the document
called SayHello, you can now call this subroutine to insert text
after the bookmark's position like this:
  Arg1 := 'Hello world';
  Word.Run('InsertGreetingAtBookmark', Arg1);

--
Deborah Pate (TeamB) http://delphi-jedi.org

  Use Borland servers; TeamB don't see posts via ISPs
  http://www.borland.com/newsgroups/genl_faqs.html

Re:Passing parameters to a Macro.


Hi Deborah,

I am still getting "Too many actual parameters" as a compile time error
for the method RUN (using word97). Here is the code:

Class Definition:

  public
    { Public declarations }
  Word: _Application;
  end;

procedure TForm1.Button4Click(Sender: TObject);
var
  FileName : OleVariant;
  Which : ToleEnum;
begin
   FileName := 'd:\testdoc.doc';
   MyDoc := Word.Documents.Open(FileName,emptyparam,
                               emptyparam,emptyparam,
                               emptyparam,emptyparam,
                               emptyparam,emptyparam,
                               emptyparam,emptyparam);

// This is fine
//   Word.Run('macro1');

//  Error in the following line.
    Word.Run('macro1',emptyparam,emptyparam,
                      EmptyParam,EmptyParam,
                      emptyparam,emptyparam,
                      emptyparam,emptyparam,
                      emptyparam,emptyparam,
                      emptyparam,emptyparam,
                      emptyparam,emptyparam,
                      emptyparam,emptyparam,
                      emptyparam,emptyparam,
                      emptyparam,emptyparam,
                      emptyparam,emptyparam,
                      emptyparam,emptyparam,
                      emptyparam,emptyparam,
                      emptyparam,emptyparam,
                      emptyparam,emptyparam);

end;

The error is at compile time : "Too many actual parameters".
In fact, after the error, the cursor is positioned directly
after the first emptyparam word.
Without parameters it is fine.

I am using word97 and when I looked in the WORD97.Pas file
I could NOT find any versions of the RUN method that has more
than one parameter.

I did the same exampe using VARIANTS with the macro
you gave me and I got the error "invalid
number of parameters" but this was at RUN time:

procedure TForm1.Button7Click(Sender: TObject);
var
  Wordvar : OleVariant;
  MyDoc : OleVariant;
  FileName : OleVariant;
  Arg1 : OleVariant;
begin
  Wordvar := CreateOleObject('Word.Application');
  Wordvar.Visible := True;
  FileName := 'd:\test2.doc';
  WordVar.Documents.Open(FileName, EmptyParam);

  Arg1 := 'Hello world';

// This is fine.
 // WordVar.Run('macro2');

 // Get run time error with the following line.
  WordVar.Run('InsertGreetingAtBookmark', Arg1);

end;

Thanks
Shamresh.

Re:Passing parameters to a Macro.


<<Shamresh khan:
I am still getting "Too many actual parameters" as a
compile time error
for the method RUN (using word97).

Quote

Sorry, I had forgotten the added parameters for Run were a
Word2000 improvement. Forget everything I said. :)

However you can still run Word functions with parameters in
Word97, using a variant. The critical thing is that you
must use a document variable, and you must put the VBA code
into the ThisDocument code (not in a macros module).

var
  VarWord: OleVariant;
begin
  VarWord := Word.ActiveDocument;
  Caption := IntToStr(VarWord.AddThese(2, 3));

--
Deborah Pate (TeamB) http://delphi-jedi.org

  Use Borland servers; TeamB don't see posts via ISPs
  http://www.borland.com/newsgroups/genl_faqs.html

Re:Passing parameters to a Macro.


Hi Deborah,

Thanks for all your help. I should have mentioned I was
using word97 a few days ago. I would have slept better.

Just to wrap up, can you fill in the missing holes:
1) You used Document.AddToThese(2,4) in your last email and
    I put the code of AddToThese in the ThisDocument code section.
Basically,
    why does the code go here and what is this section for in word. Why
    don't I just create a module and put the code there? Clearly my lack
    of VB knowledge is the problem here. I could not find any help on
    the ThisDocument word in the VBA help.
2) Where can I read up more on the above so that I can be more
    productive in the future?
3) Since I am now using variants instead of interfaces to pass data
    to Word, wil there be a significant burden on performance since I
    intend to use this technique in moving large amounts of data
    from forms to the Word document? Maybe I should move to
    Word2000 and use interfaces and macros, this should help.

Once again thanks.

Shamresh.

Re:Passing parameters to a Macro.


<<Shamresh khan:
Basically, why does the code go here and what is this
section for in word. Why don't I just create a module and
put the code there?

Quote

The code goes there just so that your document variable
will find it. The other thing that section is usually used
for in VB is handling document events - OnNew, OnClose,
OnSave.

<<Shamresh:
2) Where can I read up more on the above so that I can be
more productive in the future?

Quote

I don't really know, sorry. There are various books on VBA,
but like the help they tend to assume you're doing
everything in VBA, not automating it.

<<Shamresh:
Since I am now using variants instead of interfaces to pass
data to Word, wil there be a significant burden on
performance

Quote

No. In Office automation you're frequently using
dispinterfaces rather than interfaces and passing
everything as variants anyway, so it won't make a great
deal of difference. The thing that will matter is how often
you call the macro - the fewer times, the better.

--
Deborah Pate (TeamB) http://delphi-jedi.org

  Use Borland servers; TeamB don't see posts via ISPs
  http://www.borland.com/newsgroups/genl_faqs.html

Other Threads