PDA

View Full Version : [SOLVED:] Changing Author and Last Modified By



inkarnation
02-26-2016, 01:31 PM
I was asked to produce a feature that replaces the author and last modified/saved by document properties of any Word document.
The former is easy enough, and for the latter i figured I could simply change the Office user name, save the file and then restore the user name to its original state.
The below code seems to work just fine for me, but when I tested it on other computers the author was changed but not the last modified by.
Does anyone have any idea why this is? Grateful for any pointers/suggestions on how to sort out the code or reach the same result in another way!


Sub ChangeAuthor()
Dim Author As String
Dim orig As String
Dim Doc As Document
Author = InputBox("Enter the new document author name.")
orig = Application.UserName
With Dialogs(wdDialogFileOpen)
If .Display Then
If .Name <> "" Then
Set Doc = Documents.Open(.Name)
End If
Else
MsgBox "No file selected"
End If
End With
Application.UserName = Author
With Doc
.BuiltInDocumentProperties("Author") = Author
.Saved = False
.Save
.Close
End With
Application.UserName = orig
MsgBox ("Done!")
End Sub

gmayor
02-28-2016, 02:05 AM
The last saved by doc property is not user editable, so if you want to make the last saved by name the same as the author name, then you will have to save it twice in order to replace the value in the last saved by field.

With Doc
.BuiltInDocumentProperties("Author") = Author
.Saved = False
.Save
.Saved = False
.Save
.Close
End With

gmaxey
02-28-2016, 07:13 AM
Graham,

I'm missing something. I tinkered withi this yesterday and nothing I did has any effect on the the Last author property including your code:


cratchMacro()
With ActiveDocument
MsgBox .BuiltInDocumentProperties("Last author")
.BuiltInDocumentProperties("Author") = "Joe"
'.BuiltInDocumentProperties("Last author") = "Joe" - no effect.
.Saved = False
.Save
.Saved = False
.Save
MsgBox .BuiltInDocumentProperties("Last author")
.Close
End With

lbl_Exit:
Exit Sub
End Sub



After running and re-running, both msgboxes return me and not Joe.

gmaxey
02-28-2016, 08:32 AM
Graham,

I figured out the reason why nothing was working. I was signed in so "Last author" is determined by the Windows LiveID username and not the Application.Username. After I logged out the follow worked. Note - There was no need to save twice:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim strUserName As String
strUserName = Application.UserName
Application.UserName = InputBox("Enter new author")
With ActiveDocument
.BuiltInDocumentProperties("Author") = Application.UserName
.Save
End With
Application.UserName = strUserName
ActiveDocument.Close
lbl_Exit:
Exit Sub
End Sub

inkarnation
02-29-2016, 01:02 AM
Ah, that would explain it Greg. Thank you both for investigating! Marking the thread "solved".

RAK_da_Pira
08-02-2019, 07:44 PM
A follow up: Note that if (through FILE > Options > Trust Center [Trust Center Settings ...] > Privacy Options) the document property box in front of Remove personal information from file properties on save is unchecked, then the Author name will be made blank when the document is saved, and the names of the authors of comments and alterations will be replaced by Author. More information: https://www.isumsoft.com/office/clear-hidden-personal-data-in-office-document.html. In my case, I was collaborating on the translation and revision of a document, and losing the information about authors (original, of comments, of alterations, ...) was highly frustrastring, until I plunged into the Trust Center Settings and unchecked Remove personal information from file properties on save.