
Signature
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
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
> Hi Stephen,
>
[quoted text clipped - 10 lines]
>
> http://word.mvps.org/FAQs/MacrosVBA/NumberDocs.htm
Peter Hewett - 31 May 2004 22:53 GMT
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
Stephen Glynn <stephen.glynn@ntlworld.com>, said:
>Thanks.
>
[quoted text clipped - 51 lines]
>>
>> http://word.mvps.org/FAQs/MacrosVBA/NumberDocs.htm
Doug Robbins - Word MVP - 31 May 2004 22:55 GMT
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.

Signature
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
> Thanks.
>
[quoted text clipped - 51 lines]
> >
> > http://word.mvps.org/FAQs/MacrosVBA/NumberDocs.htm