Author Topic: Document Last Author  (Read 12061 times)

0 Members and 1 Guest are viewing this topic.

incubii

  • Newbie
  • *
  • Posts: 3
Document Last Author
« on: April 23, 2008, 07:17:16 PM »
Hi All, excellent to have some forums now! I asked Chris about this but thought i would put it out there since i cant get anything on this.

I would like to have a shape whose text shows Document Last Author. Now in the shape sheet i can get the author which is CREATOR() but there is no way to get the last author.

I want to keep this as shapesheet only. I noticed you can get the value from ActiveDocument.BuiltInDocumentProperties("Last Author").Value but i have no idea how to use that VBA in the shapesheet.

Any help would be great eve if the answer is it isnt possible. :)

Visio Guy

  • Administrator
  • Hero Member
  • *****
  • Posts: 1716
  • Smart Graphics for Visual People...n' Stuff
    • Visio Guy
Re: Document Last Author
« Reply #1 on: April 23, 2008, 07:36:53 PM »
Hi Incubii,

Here's at least part of your answer, hopefully.

You can't execute VBA directly in the ShapeSheet (anymore - you used to be able to actually stuff in-line VBA code into a ShapeSheet cell)

But you can CALL VBA procedures from a ShapeSheet cell. A classic place to test this is in the Events.EventDblClick cell. First, make a test procedure, such as this:

  Public Sub MyTestMessage()
    Call MsgBox("Hi Dude!")
  End Sub

Then, try these functions:

EventDblClick = RUNMACRO("ThisDocument.MyTestMessage")
EventDblClick = RUNADDON("ThisDocument.MyTestMessage")

After setting the formula, simply double-click your shape, and you should see the message box!

There's a third function, CALLTHIS, that actually passes the calling shape. You have to slightly change the code, and you have to put it in a module--not in ThisDocument:

  Public Sub MyTestMessage(shp As Visio.Shape)
    Call MsgBox("Hi Dude! From: " & shp.ID)
  End Sub

Then, the ShapeSheet formula looks like this:

EventDblClick = CALLTHIS("SomeModule.MyTestMessage")

Hope this gets you a bit closer!


For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

incubii

  • Newbie
  • *
  • Posts: 3
Re: Document Last Author
« Reply #2 on: April 23, 2008, 07:51:27 PM »
Ill give that a try and see how it goes. Just trying not to use VBA as its always a concern for certain people.

thanks for your help mate :)

Lars-Erik

  • Sr. Member
  • ****
  • Posts: 278
Re: Document Last Author
« Reply #3 on: April 24, 2008, 03:13:43 AM »
When i look at http://office.microsoft.com/en-gb/visio/HP010686121033.aspx you can see it does save that data somewhere.

Quote
File properties: Author, Manager, Company, and Last saved by. This information is removed when you select the Remove personal information from file properties on save check box and save the document.

What i find odd (and gives me the idea it cant be done)

Quote
Creator, Manager, and Company text field information is removed when you select the Remove personal information from file properties on save check box and save the document.

Note that the last saved by is no longer mentioned there.

aledlund

  • Hero Member
  • *****
  • Posts: 1412
Re: Document Last Author
« Reply #4 on: April 25, 2008, 11:22:38 AM »
You might try somethink like this. When I run into interesting VBA challenges I check Randy's site (vbnet.mvps.org)
al

Visio Guy

  • Administrator
  • Hero Member
  • *****
  • Posts: 1716
  • Smart Graphics for Visual People...n' Stuff
    • Visio Guy
Re: Document Last Author
« Reply #5 on: April 25, 2008, 03:01:12 PM »
Al attched a VSD to the last post, which contains VBA code in it, all ready to go. But he also sent me the code snippet for those who like to read :)

Thanks Al!

Code
' Everything should be predefined
Option Explicit

'-- Start -- material from Randy Birch (vbnet.mvps.org)

Private Const MAX_USERNAME As Long = 15

Private Declare Function GetUserName Lib "advapi32" _
   Alias "GetUserNameA" _
  (ByVal lpBuffer As String, _
   nSize As Long) As Long
     
Private Declare Function lstrlenW Lib "kernel32" _
  (ByVal lpString As Long) As Long
       
Private Function GetThreadUserName() As String

  'Retrieves the user name of the current thread. This is the name of the user
  'currently logged onto the system. If the current thread is impersonating
  'another client, GetUserName returns the user name of the client that the
  'thread is impersonating.

   Dim buff As String
   Dim nSize As Long
   
   buff = Space$(MAX_USERNAME)
   nSize = Len(buff)
   If GetUserName(buff, nSize) = 1 Then
      GetThreadUserName = TrimNull(buff)
      Exit Function
   End If
End Function

Private Function TrimNull(startstr As String) As String
   TrimNull = Left$(startstr, lstrlenW(StrPtr(startstr)))
End Function

'-- End -- randy's material


' This routine takes the passed username string and
' adds it to the document shapesheet

Private Sub addLastUser(strUserName As String)

    Dim intRow As Integer
    On Error GoTo ErrPrompt

    Dim docCurrent As Visio.Document
    Set docCurrent = Application.ActiveDocument

    Dim shpDocSheet As Visio.Shape
    Set shpDocSheet = docCurrent.DocumentSheet

    'Insert row and enter data
    'test to see if the cell already exists
    If shpDocSheet.CellExists("user.lastuser", False) = False Then
        intRow = shpDocSheet.AddNamedRow(visSectionUser, "lastuser", visTagDefault)
        shpDocSheet.CellsSRC(visSectionUser, intRow, visUserValue).FormulaU = Chr (34) & strUserName & Chr (34)
        shpDocSheet.CellsSRC(visSectionUser, intRow, visUserPrompt).FormulaU = ""
    Else
        intRow = shpDocSheet.CellsRowIndex("user.lastuser")
        shpDocSheet.CellsSRC(visSectionUser, intRow, visUserValue).FormulaU = Chr (34) & strUserName & Chr (34)
        shpDocSheet.CellsSRC(visSectionUser, intRow, visUserPrompt).FormulaU = ""
    End If
    Exit Sub
   
ErrPrompt:
    MsgBox Err.Description
End Sub

‘ make public so can be called from tools/macro menu
Public Sub ShowUserName()

    Dim strUserName As String
    ' call randy's example code
    strUserName = GetThreadUserName()
    ' tell the user who we think they are
    MsgBox "document opened by " & strUserName
    ' now save it to the document sheet
    addLastUser strUserName

End Sub

' use the document opened event to trigger finding out who the user is

Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
    ShowUserName
End Sub






« Last Edit: April 25, 2008, 03:03:56 PM by Visio Guy »
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

Lars-Erik

  • Sr. Member
  • ****
  • Posts: 278
Re: Document Last Author
« Reply #6 on: April 25, 2008, 03:10:23 PM »
I tried it a bit back and it returns a empty string as user... is it me ? or ?

Also, if I'm reading this right...

Code
Private Sub Document_DocumentSaved(ByVal doc As IVDocument)
ShowUserName
End Sub

Private Sub Document_DocumentSavedAs(ByVal doc As IVDocument)
ShowUserName
End Sub

Should replace:
Code
Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
ShowUserName
End Sub

If you want to use the UserName for documenting who saved it.
The code in the above post will save who opened the document.

Though I think we can safely say that its not possible with just the shapesheet.

I want to keep this as shapesheet only.

Visio Guy

  • Administrator
  • Hero Member
  • *****
  • Posts: 1716
  • Smart Graphics for Visual People...n' Stuff
    • Visio Guy
Re: Document Last Author
« Reply #7 on: April 25, 2008, 04:08:04 PM »

I want to keep this as shapesheet only.

I was hesitating to say "it can't be done in the ShapeSheet", because I thought I had done this before, but I couldn't find the example.

It turns out that we implemented a last edit date in the ShapeSheet, not the last editor.

I don't think that the last editor information is available via the ShapeSheet.

Oh well, off to Visio 14 Feature Requests I go... :)
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

aledlund

  • Hero Member
  • *****
  • Posts: 1412
Re: Document Last Author
« Reply #8 on: April 26, 2008, 11:43:05 AM »
IMHO it probably doesn't matter if you code it on document open or saved, since if you don't save it the original doesn't get over written.  ;)
al

Lars-Erik

  • Sr. Member
  • ****
  • Posts: 278
Re: Document Last Author
« Reply #9 on: April 27, 2008, 05:01:00 AM »
...True, still would be 'cleaner' not to run the code unless you need it.
Not that it matters much in this case.
« Last Edit: April 27, 2008, 03:58:49 PM by Lars-Erik »