Setting document properties in Word

S

Stephen Glynn

I have, with help from

http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

and advice received here managed to create a user form that opens
whenever someone opens a document from a letter template and asks for
the document's author and typist.

Ideally, though, I only want the form to open the first time the
template's used in a particular session so the user isn't being
constantly asked for the same information.

Is there a way to do this?

Steve
 
D

Doug Robbins - Word MVP

Hi Stephen,

You could use the System.PrivateProfileString in the userform to write the
information to a .txt or .ini file and then have code in the AutoNew macro
in the template use System.PrivateProfileString to recall the information.
If it was there, the UserForm would not then be called, but the autonew
macro would automatically populate the document with the information. You
would also need an AutoExec macro that ran when Word was started to clear
out the information from the .txt or .ini file.

The use of System.PrivateProfileString is covered in the article "Creating
sequentially numbered documents (such as invoices)" at:

http://word.mvps.org/FAQs/MacrosVBA/NumberDocs.htm


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
S

Stephen Glynn

Thanks.

I'm afraid I'm getting myself thoroughly confused.

The code I've got is

Sub autonew()
'
' autonew Macro
' Macro recorded 31-May-04 by Glynn
'
Text1 = System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Text1")

Text2 = System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Text2")

If Text1 = "" Or Text2 = "" Then
UserForm1.Show
Else
Text1 = Text1
Text2 = Text2
End If

System.PrivateProfileString("C:\Settings.txt", "MacroSettings", _
"Text1") = Text1
System.PrivateProfileString("C:\Settings.txt", "MacroSettings", _
"Text1") = Text2


End Sub

It's remembering the values I enter on the user form from document to
document, but each new document I create using the template starts with
the bookmarks blank and the Userform displaying the values it remembers
ffrom the previous document.

Sorry -- the solution must be obvious but, as is clear, I know very
little about VBA.

Steve
 
P

Peter Hewett

Hi Stephen Glynn

The problem is that you're instantiating your Form incorrectly. Try the following:

Sub AutoNew()
Const cPPSPath As String = "C:\Settings.Txt"
Const cPPSSection As String = "MacroSettings"

Dim frmX As UserForm1
Dim strText1 As String
Dim strText2 As String

' Private Profile Strings to use in the document
strText1 = System.PrivateProfileString(cPPSPath, _
cPPSSection, "Text1")
strText2 = System.PrivateProfileString(cPPSPath, _
cPPSSection, "Text2")

' Prompt the user for the missing values
If strText1 = vbNullString Or strText2 = vbNullString Then

' Instantiate and display the Form
Set frmX = New UserForm1
Load frmX
frmX.Show

' Save values for updating the Private Profile Strings
strText1 = frmX.Text1
strText2 = frmX.Text2

' Dispose of the Form
Unload frmX
Set frmX = Nothing

' Save values from the Form in the Private Profile Strings
System.PrivateProfileString(cPPSPath, cPPSSection, _
"Text1") = strText1
System.PrivateProfileString(cPPSPath, cPPSSection, _
"Text2") = strText2
End If
End Sub

In the Forms OK and/or Cancel button event handling procedure make sure you close the Form
using:
Me.Hide

HTH + Cheers - Peter
 
D

Doug Robbins - Word MVP

Hi Stephen,

The code in the autonew() should be

Sub autonew()
'
' autonew Macro
' Macro recorded 31-May-04 by Glynn
'
Dim Author as string, Typist as String
Author = System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Author")

Typist = System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Typist")

If Author = "" Or Typist = "" Then
UserForm1.Show
Else
With ActiveDocument
.Bookmarks("Author").Range.InsertBefore
System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Author")
.Bookmarks("Text2").Range.InsertBefore
System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Typist")
End With
End If

End Sub

And the Autoexec macro should contain:

System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Author") = ""
System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Typist") = ""

In the command button click event on the userform, you need to have (in
addition to the code to populate the .Range of the Bookmarks

System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Author") = txtAuthor
System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Typist") = txtTypist

Note, it's a good practice to identify controls with their type and function
using the convention

TextBox - txt
Label - lbl
ComboBox - cmb
ListBox - lst
CommandButton - btn
CheckBox - chk
OptionButton - opt

etc.
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top