Where can I save the settings entered by the user?

一曲冷凌霜 提交于 2019-12-11 01:55:36

问题


VBA.

Step 1
The MS Project file is open;
The user starts the macro;
The form is opened;
The user enters the path;
The user clicks "Save";
The user closes the form;
The user closes the MS Project file.

Step 2
The user opens the MS Project file;
The user wins the macro;
The form is opened;
The form displays the path that the user has registered for "Stage 1";

Questions
How to make that when the user opens the form a second time (Step 2) in the form was displayed the path that was saved in (Step 1)?
In other words, after the form was closed (Step 1), the value of the textbox was retained?

Can this textbox value be saved in the MS Project file?
Or should I save it in a separate file?
How is this best done?


回答1:


Add a custom file property to store information in the MS Project file. For example:

Sub StorePath(newPath As String)
    Dim test As String
    test = GetPath()
    If Len(test) = 0 Then
        ActiveProject.CustomDocumentProperties.Add Name:="UserPath", LinkToContent:=False, Type:=msoPropertyTypeString, Value:=newPath
    Else
        ActiveProject.CustomDocumentProperties("UserPath") = newPath
    End If
End Sub

Function GetPath() As String
    On Error Resume Next
    GetPath = ActiveProject.CustomDocumentProperties("UserPath")
End Function

The information will be stored in the file itself, different files can have different paths stored, and if opened on another computer, the path is still available.

To save a single value on a user's computer, regardless of which file is opened, use SaveSetting and GetSetting, as mentioned by Sam in the comments above. These are not stored with the file and would not be visible on other computers.




回答2:


A project's task 0 (the project summary task) is rarely used, so its notes field can be a good place to store long data. Unlike ActiveProject.CustomDocumentProperties, task 0's notes isn't constrained by a 255 character limit.

Accessing task 0's notes is a little tricky. On any other task, you'd use

ActiveProject.Tasks(someTaskID).Notes = "really long strings"
'where someTaskID is an integer variable

but task 0's notes are accessed by

ActiveProject.Comments = "really long strings"



回答3:


Further to Rachel's answer and in response to Jerred S.' comment, It is easy to overcome the 255 char limit of CustomDocumentProperties and to store War and Peace in there. Write a function such as function storeMyCDPstring(CDPNames as string, CDPVal as string). It will need to chop CDPVal into not-to-exceed 255 character packets and store these as indexed CustomDocumentProperties. Example, you want to store a 1000 char string in CustomDocumentProperty named "MyCDP". You adopt an arbitary naming convention - CDPs will be indexed by "#~#-n":

  • chars 1 to 255 will be stored as CustomDocumentProperty "myCDP#~#-1",
  • chars 256 to 510 will be stored as CustomDocumentProperty "myCDP#~#-2",
  • chars 511 to 765 will be stored as CustomDocumentProperty "myCDP#~#-3",
  • chars 766 to 1000 will be stored as CustomDocumentProperty "myCDP#~#-4"

You will need to write a function such as function getMyCDPstring(CDPNames as string) which must retrieve, concatenate and return all the substrings. You need also a managed delMyCDP function that will delete all the packets.



来源:https://stackoverflow.com/questions/48934885/where-can-i-save-the-settings-entered-by-the-user

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!