4

I am trying to create a word document from a template via JACOB/JAVA. I can't seem to find any decent documentation on JACOB. Can someone please explain how Dispatch works (.get | .put | .toDispatch)? I am trying to convert the following code to JACOB:

   Selection.MoveRight Unit:=wdCharacter, Count:=2, Extend:=wdExtend
   Selection.Cells.Merge

I tried:

   Dispatch.call(oSelection, "MoveRight", 2, "Extend"); 
   Dispatch.call(oSelection, "Merge").toDispatch();

But it certainly isn't working.

Any help would be appreciated.

Cole
  • 349
  • 5
  • 16
  • Oh wow, usually the “please convert language X to Y for me” questions are terrible but yours is certainly more researched. – Konrad Rudolph Jun 04 '14 at 14:05
  • I have been trying for a while and I have looked into the (scarce) examples of code conversions, but I can't seem to apply it to my own code. Jacob looks really useful, but if i can't figure some of this out, then I might have to try an alternative. – Cole Jun 04 '14 at 14:08

1 Answers1

1

There is nothing wrong with Jacob, it works as advertised.

So first, you have to find the object reference for Word 2003, which you appear to be using The reason for that is that when using named parameters, some optional parameters may be omitted, and some the parameters may be specified out of order. So I first need to confirm the signature of Selection.MoveRight. Here is the documentation for MoveRight: http://msdn.microsoft.com/en-us/library/aa212388(v=office.11).aspx

expression.MoveRight(Unit, Count, Extend)

Unit and Extend are enumerations, so we will have to find the right integer constants for them. wdCharacter is 1, and wdExtend is also 1 (how you come up with these values differs, easiest way is to look at the object browser inside the Office application's VBA editor).

Assuming oSelection is a valid object, this should work:

Dispatch.call(oSelection, "MoveRight", 1, 2, 1); 

Now for the second line, you forgot about cells in the translation. You will need something like this:

Dispatch cells=Dispatch.get(oSelection,"Cells").toDispatch();//Selection.Cells.
Dispatch.call(cells, "Merge");                                       //Selection.Cells.Merge()

Note that I do not call toDispatch on the second line, because Merge does not return anything. toDispatch is used to convert a return value, like on the first line, into a Dispatch object I can use later to make calls on that object.

Tony BenBrahim
  • 7,040
  • 2
  • 36
  • 49