'User Name of document using VBA
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?
Solution 1:[1]
"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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Noah |
