Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / May 2004

Tip: Looking for answers? Try searching our database.

Setting document properties in Word

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Stephen Glynn - 30 May 2004 22:53 GMT
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
Doug Robbins - Word MVP - 30 May 2004 23:01 GMT
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

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

> I have, with help from
>
[quoted text clipped - 11 lines]
>
> Steve
Stephen Glynn - 31 May 2004 14:51 GMT
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.