Author Topic: MS Word + VBScript to import EA diagrams  (Read 2438 times)

Viking

  • EA User
  • **
  • Posts: 247
  • Karma: +1/-2
    • View Profile
MS Word + VBScript to import EA diagrams
« on: November 25, 2017, 06:37:32 am »
Hello,
I am quite sure that it exists already.
I am looking for a script (VBScript), that replaces keyword (resp. appropriate search-statements) in MS Word with Sparx EA diagrams.
In other words, I write a document with Word and add keywords, which will be replaced by diagrams out of EA, as soon as I start the script.
Thank you very much in advance for your feedback, V.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 8479
  • Karma: +207/-26
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: MS Word + VBScript to import EA diagrams
« Reply #1 on: November 25, 2017, 05:44:51 pm »
I don't have anything like that.

But it seems simple enough. If you can't find anything you can always ask one of the EA consultants (like myself) on the forum to write it for you.

Geert

Viking

  • EA User
  • **
  • Posts: 247
  • Karma: +1/-2
    • View Profile
Re: MS Word + VBScript to import EA diagrams
« Reply #2 on: December 06, 2017, 03:34:05 am »
I wonder if it possible to retrieve a diagram (as a picture) from Word using the Sparx Object Model. I did not find anything but an addin using the clipboard.

It would be could if no installation would be required.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 8479
  • Karma: +207/-26
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: MS Word + VBScript to import EA diagrams
« Reply #3 on: December 06, 2017, 03:54:10 am »
Do you want to retrieve a diagram image from EA and use it in Word, or get an image from Word and put it in EA?

For the first one you can use EA.Repository.PutDiagramImageToFile ()

Geert

Viking

  • EA User
  • **
  • Posts: 247
  • Karma: +1/-2
    • View Profile
Re: MS Word + VBScript to import EA diagrams
« Reply #4 on: December 06, 2017, 05:47:04 pm »
Do you want to

(1) retrieve a diagram image from EA and use it in Word, or
(2) get an image from Word and put it in EA?

For the first one you can use EA.Repository.PutDiagramImageToFile ()

Geert

I want to do (1). Thank you very much for your help.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 8479
  • Karma: +207/-26
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: MS Word + VBScript to import EA diagrams
« Reply #5 on: December 06, 2017, 05:55:36 pm »
In the past I've used this workaround to get the actual image from a diagram:
Code: [Select]
/// <summary>
/// returns diagram image
/// </summary>
public Image image
{
    get
    {
        EA.Project projectInterface = this.model.getWrappedModel().GetProjectInterface();
        string diagramGUID = projectInterface.GUIDtoXML(this.wrappedDiagram.DiagramGUID);
        string filename = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".png";
        //save diagram image to file (format ".png")
        projectInterface.PutDiagramImageToFile(diagramGUID, filename, 1);
        //load the contents of the file into a memorystream
        MemoryStream imageStream = new MemoryStream(File.ReadAllBytes(filename));
        //then create the image from the memorystream.
        //this allows us to delete the temporary file right after loading it.
        //When using Image.FromFile the file would have been locked for the lifetime of the Image
        Image diagramImage = Image.FromStream(imageStream);
        //delete the temorary file
        System.IO.File.Delete(filename);

        return diagramImage;
    }
}
Geert

Viking

  • EA User
  • **
  • Posts: 247
  • Karma: +1/-2
    • View Profile
Re: MS Word + VBScript to import EA diagrams
« Reply #6 on: January 16, 2018, 06:19:06 am »
Hello,

I am trying to use the script above in Word resp. in VBA.

I am new to VBA. I cannot find MemoryStream.

Can somebofy tell me, how I can link MemoryStream to my code resp. which library I have to reference?

Many thanks in advance, V.

Simon M

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 6453
  • Karma: +55/-6
    • View Profile
Re: MS Word + VBScript to import EA diagrams
« Reply #7 on: January 16, 2018, 08:47:02 am »
MemoryStream is a .Net class. I don't know VBA well enough to know if there is an equivalent class (or set of functions) available in VBA.
Simon

support@sparxsystems.com

qwerty

  • EA Guru
  • *****
  • Posts: 9685
  • Karma: +176/-150
  • I'm no guru at all
    • View Profile
Re: MS Word + VBScript to import EA diagrams
« Reply #8 on: January 16, 2018, 09:47:46 am »
How about entering "vba memorystream" into Google? That seemed to give promising results.

q.

Viking

  • EA User
  • **
  • Posts: 247
  • Karma: +1/-2
    • View Profile
Re: MS Word + VBScript to import EA diagrams
« Reply #9 on: January 16, 2018, 06:03:18 pm »
I identified it. I had to reference mscorlib.dll. Many thanks.

Viking

  • EA User
  • **
  • Posts: 247
  • Karma: +1/-2
    • View Profile
Re: MS Word + VBScript to import EA diagrams
« Reply #10 on: January 17, 2018, 06:48:11 am »
Hi,

(1)
Could somebody tell me, why it is better to use PutDiagramImageOnClipboard instead of PutDiagramImageToFile? Is it just because of working memory?

(2)
I got the half of Geerts example to work in VBA. I do not find an equivalent in VBA for the code below (see also Simons comment). Does anybody know the solution?

MemoryStream imageStream = new MemoryStream(File.ReadAllBytes(filename));
Image diagramImage = Image.FromStream(imageStream);
System.IO.File.Delete(filename);

Many thanks in advance, V.
« Last Edit: January 17, 2018, 05:39:40 pm by Viking »

qwerty

  • EA Guru
  • *****
  • Posts: 9685
  • Karma: +176/-150
  • I'm no guru at all
    • View Profile
Re: MS Word + VBScript to import EA diagrams
« Reply #11 on: January 17, 2018, 08:22:58 am »
(1) the clipboard does not need file operations and thus is (a lot) faster.

q.

Arshad

  • EA User
  • **
  • Posts: 131
  • Karma: +6/-1
    • View Profile
Re: MS Word + VBScript to import EA diagrams
« Reply #12 on: January 17, 2018, 03:13:18 pm »
Hello,

I am trying to use the script above in Word resp. in VBA.

I am new to VBA. I cannot find MemoryStream.

Can somebofy tell me, how I can link MemoryStream to my code resp. which library I have to reference?

Many thanks in advance, V.

Hi Viking

If you need to insert images to word (without memory stream )then you can try using below codes

Code: [Select]
Selection.InlineShapes.AddPicture FileName:= _"fullPathOftheImage", LinkToFile:=False, _SaveWithDocument:=True
or

Code: [Select]
objShapes.AddPicture (fullPathOftheImage)   


Thanks
Arshad

Sparx Systems India - Services
Profile | Training PlansConsulting

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 8479
  • Karma: +207/-26
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: MS Word + VBScript to import EA diagrams
« Reply #13 on: January 17, 2018, 03:48:30 pm »
(1)
Could somebody tell me, why it is better to use PutDiagramImageOnClipboard to PutDiagramImageToFile? Is it just because of working memory?

I don't think it's better, I think it's much worse. I hate it when programs hijack my clipboard. >:(
But it might indeed be faster then using the file approach.

Geert

Viking

  • EA User
  • **
  • Posts: 247
  • Karma: +1/-2
    • View Profile
Re: MS Word + VBScript to import EA diagrams
« Reply #14 on: January 18, 2018, 12:34:59 am »
Hi Viking
If you need to insert images to word (without memory stream )then you can try using below codes

Code: [Select]
Selection.InlineShapes.AddPicture FileName:= _"fullPathOftheImage", LinkToFile:=False, _SaveWithDocument:=True
or

Code: [Select]
objShapes.AddPicture (fullPathOftheImage)   
Thanks
Arshad

Thank you, Arshad. That worked.