Share via

Saving a variable in excel

Anonymous
2022-11-20T11:36:49+00:00

I'm trying to save a variable in excel. I want this variable to stay even if I close excel.

I want to use : 
Sub AddCustomDocumentProperties() 

 ActiveDocument.CustomDocumentProperties.Add Name:="YourName", LinkToContent:=False, Value:="Joe", Type:=msoPropertyTypeString 

End Sub

To save this variable in a document's propriety. However, when I launch this program it says the variable "ActiveDocument" does not exist.

How to solve this problem? Or is it possible to save a variable differently.

Microsoft 365 and Office | Excel | Other | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2022-11-20T13:02:10+00:00

For example:

Dim MyVar As String

MyVar = ActiveWorkbook.CustomDocumentProperties("YourName")

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2022-11-20T12:13:52+00:00

ActiveDocument is for Microsoft Word. The equivalent for Microsoft Excel is ActiveWorkbook. So use

ActiveWorkbook.CustomDocumentProperties.Add Name:="YourName", LinkToContent:=False, Value:="Joe", Type:=msoPropertyTypeString

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

8 additional answers

Sort by: Most helpful
  1. Anonymous
    2022-11-20T13:58:11+00:00

    It doesn't work. It says invalid argument or procedure

    Was this answer helpful?

    0 comments No comments
  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Anonymous
    2022-11-20T12:44:44+00:00

    It works!

    How can I set a variable as this property's value now?

    Was this answer helpful?

    0 comments No comments