User Name of document using VBA

593 Views Asked by At

I tried to find the user name of the word document using VBA and tried to compare it with Author of the specified comment, If they are same notify me else no.

I have written code which is as

Dim oComment as Comment
Set oComment = ActiveDocument.Comments.Item(1)

Dim UserName As Application
Dim Author As Comment
Set UserName = Application.UserName
Set Author = oComment.Contact

Dim UN, AU As Strings

Set UN = CStr(UserName)
Set AU = CStr(Author)

If Not (UN Is AU) Then
If (oComment.Ancestor Is Nothing) Then
    MsgBox (ActiveDocument.Comments(LatestCommentIndex).Contact & " has added a new comment")

If I try to run it, it said Type mistach on the line UserName = Application.UserName

Anybody has a workaround it?

1

There are 1 best solutions below

0
On

"Application" is a global object, you don't need to initialize it, "Username" is a property of type String, so you will have the following code:

dim sUserName as string

let sUserName = Application.UserName

debug.print sUsername

Furthermore: to assign a value to a string variable, don't use "Set" but "Let" (which you can ommit at all: UN = sUserName, no casting, no special coomand)