Sparx Systems Forum

Discussion => Automation Interface, Add-Ins and Tools => Topic started by: Viking on November 25, 2017, 06:37:32 am

Title: MS Word + VBScript to import EA diagrams
Post by: Viking 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.
Title: Re: MS Word + VBScript to import EA diagrams
Post by: Geert Bellekens 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
Title: Re: MS Word + VBScript to import EA diagrams
Post by: Viking 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.
Title: Re: MS Word + VBScript to import EA diagrams
Post by: Geert Bellekens 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
Title: Re: MS Word + VBScript to import EA diagrams
Post by: Viking 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.
Title: Re: MS Word + VBScript to import EA diagrams
Post by: Geert Bellekens 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
Title: Re: MS Word + VBScript to import EA diagrams
Post by: Viking 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.
Title: Re: MS Word + VBScript to import EA diagrams
Post by: Simon M 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.
Title: Re: MS Word + VBScript to import EA diagrams
Post by: qwerty on January 16, 2018, 09:47:46 am
How about entering "vba memorystream" into Google? That seemed to give promising results.

q.
Title: Re: MS Word + VBScript to import EA diagrams
Post by: Viking on January 16, 2018, 06:03:18 pm
I identified it. I had to reference mscorlib.dll. Many thanks.
Title: Re: MS Word + VBScript to import EA diagrams
Post by: Viking 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.
Title: Re: MS Word + VBScript to import EA diagrams
Post by: qwerty on January 17, 2018, 08:22:58 am
(1) the clipboard does not need file operations and thus is (a lot) faster.

q.
Title: Re: MS Word + VBScript to import EA diagrams
Post by: Arshad 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
Title: Re: MS Word + VBScript to import EA diagrams
Post by: Geert Bellekens 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
Title: Re: MS Word + VBScript to import EA diagrams
Post by: Viking 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.
Title: Re: MS Word + VBScript to import EA diagrams
Post by: Arshad on January 18, 2018, 04:08:04 pm
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.


Cheers  ;D ;D


Arshad