Document Last Author

Started by incubii, April 24, 2008, 12:17:16 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

incubii

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

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

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

When i look at http://office.microsoft.com/en-gb/visio/HP010686121033.aspx you can see it does save that data somewhere.

QuoteFile 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)

QuoteCreator, 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

You might try somethink like this. When I run into interesting VBA challenges I check Randy's site (vbnet.mvps.org)
al

Visio Guy

#5
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!


' 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







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

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...


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

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


Should replace:

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.

Quote from: incubii on April 24, 2008, 12:17:16 AM
I want to keep this as shapesheet only.

Visio Guy


Quote from: incubii on April 24, 2008, 12:17:16 AM
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

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

#9
...True, still would be 'cleaner' not to run the code unless you need it.
Not that it matters much in this case.