Author Topic: GetCurrentDiagram always returns nothing  (Read 23877 times)

bomber

  • EA Novice
  • *
  • Posts: 10
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
GetCurrentDiagram always returns nothing
« on: March 16, 2017, 03:13:57 am »
I have a VBA script and macro that pastes the selected EA diagram into Word. The script worked in EA7 but I recently upgraded to EA13. The GetCurrentDiagram always returns null even if a diagram is open in a single instance of EA running behind Word.

Code: [Select]
[tt]Sub InsertSelectedEADiagram()
  Dim App As EA.App
  Dim dDiagram As EA.Diagram
  Dim sFilename As String
  Dim sShape As InlineShape
 
 Dim rep As New EA.Repository
 
  Set App = GetObject("", "EA.App")
  Set dDiagram = App.Repository.GetCurrentDiagram
'  If dDiagram = Null Then
'    MsgBox "No diagram selected"
'    Exit Sub
'  End If
 
  sFilename = "d:\" & Str(dDiagram.DiagramID) & ".emf "
  App.Project.PutDiagramImageToFile dDiagram.DiagramGUID, sFilename, 0
 
  Set sShape = Application.Selection.InlineShapes.AddPicture(Filename:=sFilename, LinkToFile:=False, SaveWithDocument:=True)
  sShape.AlternativeText = dDiagram.DiagramID
  sShape.Width = sShape.Width * 0.7
  sShape.Height = sShape.Height * 0.7
 
  Set sShape = Nothing
  Set dDiagram = Nothing
  Set App = Nothing
 
End Sub[/tt]

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13387
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: GetCurrentDiagram always returns nothing
« Reply #1 on: March 16, 2017, 03:21:39 pm »
I don't see any code that opens a diagram, so unless you have a default diagram setup it seems logical that it would return empty for current diagram.

Geert

bomber

  • EA Novice
  • *
  • Posts: 10
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: GetCurrentDiagram always returns nothing
« Reply #2 on: March 16, 2017, 07:20:13 pm »
The way this works is as follows:
I have EA open and I edit a diagram
Then I switch (Alt-Tab) to Word and run this macro script from a toolbar button. Then this macro takes the currently open diagram in EA, saves it to a file and inserts it into the document at that point. I could have used the clipboard, but at the time when I used EA 7, the clipboard rendering was bad.

This worked in EA 7 and my current EA 13 when referencing an old EA COM object, but after reinstalling my system after a HDD crash 2 days ago, these scripts did not work anymore. Now it references the latest EA Object Model 2.1.

Any suggestions?

EDIT: just to clarify, this script runs in Word VBA

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13387
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: GetCurrentDiagram always returns nothing
« Reply #3 on: March 16, 2017, 07:55:26 pm »
Ok, I see, the Set App = GetObject("", "EA.App") connects to the running instance of EA.
Check if you by any chance have two ea.exe processes running. This GetObject operation only connects to the first ea.exe process started on the pc, so if you have any ghost processes running on the background it could be connecting to that process instead of the instance you are working on.

Geert

bomber

  • EA Novice
  • *
  • Posts: 10
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: GetCurrentDiagram always returns nothing
« Reply #4 on: March 16, 2017, 08:51:04 pm »
Hmm strange, this now works. Without me having to do anything. I did reboot so, this may have fixed it. Tx for the help..

bomber

  • EA Novice
  • *
  • Posts: 10
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: GetCurrentDiagram always returns nothing
« Reply #5 on: March 17, 2017, 02:15:37 am »
Hmm this is still a problem. When I start off from new, I open Word and EA. In Task Manager there is 1 EA process running. Then when the Set App = GetObject("", "EA.App") runs, another instance of EA process appears. This is probably to be expected, but I am not sure. Then the script fails again as per the OP.
When I close EA, the new (2nd) process still runs and it has to be killed manually.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: GetCurrentDiagram always returns nothing
« Reply #6 on: March 17, 2017, 10:25:40 am »
The example in my book uses
Code: [Select]
Set EAapp = GetObject(, "EA.App"), so with no instead of an empty-string parameter. No idea whether that makes a difference.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13387
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: GetCurrentDiagram always returns nothing
« Reply #7 on: March 17, 2017, 03:35:44 pm »
The example in my book uses
Code: [Select]
Set EAapp = GetObject(, "EA.App"), so with no instead of an empty-string parameter. No idea whether that makes a difference.

q.

Same here, this is the code I use in my excel importer to connect to the current repository. And ASAIK this has been working on all versions of EA;

Code: [Select]
'-------------------------------------------------------------
' Author:   Geert Bellekens
' Date:     17/12/2007
' Description: Return the currently opened EA Model
'-------------------------------------------------------------
Public Function getCurrentRepository() As EA.Repository
    Dim EAapp As EA.App
    Dim EArepository As EA.Repository
    Set EAapp = GetObject(, "EA.App")
    'Warning when EA not open
    If EAapp Is Nothing Then
        MsgBox "Please Open Enterprise Architect"
        'try again
        Set getCurrentRepository = Me.getCurrentRepository
    End If
    'Warning when no repository is opened.
    Set EArepository = EAapp.Repository
    If EArepository Is Nothing Then
        MsgBox "Please open a Model in Enterprise Architect"
        'try again
        Set getCurrentRepository = Me.getCurrentRepository
    End If
    'return the found repository
    Set getCurrentRepository = EAapp.Repository
End Function

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8607
  • Karma: +257/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: GetCurrentDiagram always returns nothing
« Reply #8 on: March 17, 2017, 07:58:13 pm »
As the others have said, make the first parameter null.  If that still fails to work, uninstall EA and see if the VBA code still compiles.  If so, then you have a referencing problem in your VBA project.  If it fails to compile, then your references are OK - reinstall and it should work.

Lastly, we've found that if the EA.exe link is setup as "Run as Administrator", the  Set EAapp = GetObject(, "EA.App") will fail (anybody else seen this?).

HTH,
Paolo
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8083
  • Karma: +118/-20
    • View Profile
Re: GetCurrentDiagram always returns nothing
« Reply #9 on: March 20, 2017, 08:30:09 am »
Lastly, we've found that if the EA.exe link is setup as "Run as Administrator", the  Set EAapp = GetObject(, "EA.App") will fail (anybody else seen this?).
I would expect that behavior unless running the script as administrator too. Windows won't let the script see processes owned by another user if not administrator. I don't know how the reverse would behave.

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8607
  • Karma: +257/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: GetCurrentDiagram always returns nothing
« Reply #10 on: March 20, 2017, 10:53:06 am »
Lastly, we've found that if the EA.exe link is setup as "Run as Administrator", the  Set EAapp = GetObject(, "EA.App") will fail (anybody else seen this?).
I would expect that behavior unless running the script as administrator too. Windows won't let the script see processes owned by another user if not administrator. I don't know how the reverse would behave.
Thanks, Simon,
When I get a chance - sometime this century, I hope - I'll check it out and confirm.

Paolo
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

Glassboy

  • EA Practitioner
  • ***
  • Posts: 1367
  • Karma: +112/-75
    • View Profile
Re: GetCurrentDiagram always returns nothing
« Reply #11 on: March 20, 2017, 02:05:23 pm »
Lastly, we've found that if the EA.exe link is setup as "Run as Administrator", the  Set EAapp = GetObject(, "EA.App") will fail (anybody else seen this?).
I would expect that behavior unless running the script as administrator too. Windows won't let the script see processes owned by another user if not administrator. I don't know how the reverse would behave.

Not to mention completely different registry hives are loaded in that context.  You maybe able to copy the relevant keys across to the  security identifier that starts with S-1-5-21.  But I wouldn't.  You'd be better to create a proper user log in and run EA and then use the powershell stat-process cmdlet to run EA as that user.

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8607
  • Karma: +257/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: GetCurrentDiagram always returns nothing
« Reply #12 on: March 20, 2017, 05:42:48 pm »
Lastly, we've found that if the EA.exe link is setup as "Run as Administrator", the  Set EAapp = GetObject(, "EA.App") will fail (anybody else seen this?).
I would expect that behavior unless running the script as administrator too. Windows won't let the script see processes owned by another user if not administrator. I don't know how the reverse would behave.

Not to mention completely different registry hives are loaded in that context.  You maybe able to copy the relevant keys across to the  security identifier that starts with S-1-5-21.  But I wouldn't.  You'd be better to create a proper user log in and run EA and then use the powershell stat-process cmdlet to run EA as that user.
I may not have been clear enough.  It's the same user.  If I start an EA instance (via shortcut) with [X] Run as Administrator set, the GetObject Fails.  If the setting is unset, then it works.

Paolo
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13387
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: GetCurrentDiagram always returns nothing
« Reply #13 on: March 20, 2017, 09:22:19 pm »
Lastly, we've found that if the EA.exe link is setup as "Run as Administrator", the  Set EAapp = GetObject(, "EA.App") will fail (anybody else seen this?).
I would expect that behavior unless running the script as administrator too. Windows won't let the script see processes owned by another user if not administrator. I don't know how the reverse would behave.
Yes, noticed that too, and the explanation of Simon was exactly how I interpreted it. And it makes sense from a security standpoint.
And yes, if you are running your script/program as admin, and you want it to connect to the running instance of EA then you have to start EA as Admin as well, or it won't find the process.
That is not the fact when you want to debug your add-in and attach to the ea.exe process. In that case you can run your IDE as admin, and EA as regular user.

Geert

Glassboy

  • EA Practitioner
  • ***
  • Posts: 1367
  • Karma: +112/-75
    • View Profile
Re: GetCurrentDiagram always returns nothing
« Reply #14 on: March 21, 2017, 07:29:19 am »
I may not have been clear enough.  It's the same user.  If I start an EA instance (via shortcut) with [X] Run as Administrator set, the GetObject Fails.  If the setting is unset, then it works.

Actually I may be wrong about the security identifier changing, it may just be the token.  Unfortunately I can't test it from my work machine.  But in many ways it isn't the same user.  There's quite a few moving parts to the security context in which a user starts a process.