Button to Show Userform for editing mistakes

L

LittleAnn

HI,

I was just wondering is there a way I can use a button to recall the
Userform so that if there was mistakes made on the initial input of the data
when the documented was created that it will show the Userform again with all
the inputted on it still, so that it can be edited again or changed?

Thanks
 
L

Lene Fredborg

Yes, there is – provided you have stored the data so that you can somehow
retrieve it.

You can do this by adding code to the UserForm_Initialize event that
retrieves the existing data and inserts the data in the individual controls
in the user form before displaying it.

If, for example, the data entered initially is stored in custom document
properties, you will need to fetch the values of the relevant properties. If
the values were stored in bookmarks, you will need to fetch the content of
the bookmarks.

Example:
Let’s say you have two text boxes in the user form. The code below would
insert the value of the bookmark “MyBookmark†in TextBox1 and the value of
custom document property “MyProp†in TextBox2:

Private Sub UserForm_Initialize()

With ActiveDocument
TextBox1.Value = .Bookmarks("MyBookmark").Range
TextBox2.Value = .CustomDocumentProperties("MyProp").Value
End With

End sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
J

Jay Freedman

HI,

I was just wondering is there a way I can use a button to recall the
Userform so that if there was mistakes made on the initial input of the data
when the documented was created that it will show the Userform again with all
the inputted on it still, so that it can be edited again or changed?

Thanks

There is no such thing as "the" Userform -- each userform is unique, programmed
by someone to do something. Therefore, there is no specific button to recall
"the" userform, unless that is also programmed by you or someone else
responsible for the code.

Complicating this, there are several ways that a userform, or the macro that
displays the userform, can insert data into a document -- by inserting text into
a previously existing bookmark, by putting the text into a document variable
that is then displayed by a DocVariable field, etc. Whatever you program to
recall the userform would have to find the proper part of the document and put
its text into the userform's text box.

If you can be much more specific about the existing code, perhaps posting the
code of the OK button's Click procedure or the section of macro code that places
the text into the document, then we can suggest what additional code would be
needed to repopulate the userform.
 
G

Gordon Bentley-Mix

Ann,

I do this sort of thing regulary - create templates that can be "re-run" -
and it's not an easy proposition. The method I use is similar to what Jay
suggested: storing the input in document variables in the document and
recalling the values to populate the UserForm when the template is re-run. I
provide access to the re-run functionality through a custom toolbar button,
which invokes a routine that basically runs the AutoNew code again.

To give you an idea of just how complex this process can be, I was working
on a template today that had a UserForm with a 9-page MultiPage control on
it. Each page contained anywhere from 10 to 30 controls (TextBoxes,
CheckBoxes, OptionButtons, ComboBoxes, ListBoxes, etc.). In some instances,
the page was configured to allow the input of multiple instances of related
data points - contact information for the parties to a legal agreement. Each
of these data points was stored in an array, and each array element required
its own document variable, which was created "on-the-fly" when the document
was built. A single document created using just the bare minimum of
information (some fields are optional) contained more than 70 document
variables, and an extremely complex document could have as many as 200...

If you're really keen on providing this sort of functionality, I can give
you some pointers to get you headed in the right direction. Just post back
and ask.
-----
Cheers!

Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
L

LittleAnn

HI Gordon

Thanks for your reply, since your post I have been trying to implement the
coding but I am still getting no where with my document, I was wondering
could you give me some pointers and how I can get started as I just seem to
be making loads of mistakes and it is taking alot longer then initially
anticipated.

My coding for the OK button is as follows so far:
Option Explicit
Private Sub Userform1_Initialize()
End Sub
Private Sub CmdOK_Click()
Dim oRng As Word.Range
Dim oBM As Bookmarks
Set oBM = ActiveDocument.Bookmarks
Set oRng = oBM("DocumentTitle").Range
oRng.Text = DocumentTitle.Text
oBM.Add "DocumentTitle", oRng
Me.Identifier.TextColumn = 2
Set oRng = oBM("Identifier").Range
oRng.Text = Identifier.Text
oBM.Add "Identifier", oRng
Me.Team.TextColumn = 2
Set oRng = oBM("Team").Range
oRng.Text = Team.Text
oBM.Add "Team", oRng
Me.Zone.TextColumn = 2
Set oRng = oBM("Zone").Range
oRng.Text = Zone.Text
oBM.Add "Zone", oRng
Me.DocumentType.TextColumn = 2
Set oRng = oBM("DocumentType").Range
oRng.Text = DocumentType.Text
oBM.Add "DocumentType", oRng
Me.SeqNumber.TextColumn = 2
Set oRng = oBM("SeqNumber").Range
oRng.Text = SeqNumber.Text
oBM.Add "SeqNumber", oRng
Set oRng = oBM("IssueDATE").Range
oRng.Text = IssueDATE.Text
oBM.Add "IssueDATE", oRng
Set oRng = oBM("IssueSTATUS").Range
oRng.Text = IssueSTATUS.Text
oBM.Add "IssueSTATUS", oRng
Dim rngStory As Word.Range
For Each rngStory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
rngStory.Fields.Update
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
Me.Hide

End Sub

Private Sub Userform_Initialize()
Dim myArray1 As Variant
Dim myArray2 As Variant
Dim i As Long
myArray1 = Split("Select Identifier|Arup Halcrow Joint Venture|ESB|" _
& "Irish Rail|Rail Procurement Agency", "|")
myArray2 = Split(" |AH|ES|IE|RP", "|")
With Me.Identifier
.ColumnWidths = "60;0"
For i = 0 To UBound(myArray1)
.AddItem
.List(i, 0) = myArray1(i)
.List(i, 1) = myArray2(i)
Next i

End With

End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then Cancel = True
End Sub


My other problem that I am having now is that I had to set up extra list
boxes for new options, Greg Maxey kindly sorted out my list box problem, but
now that I have added new ones, i.e. Team, Zone, DocumentType, SeqNumber I
cant get the extra coding to do anything, as want it to do the same task as
the "Identifier" code works for me.

I also need to add a spin box so that people can spin the number option "00"
up to as far as 20. But again I seem to be doing this wrong, any help please.
 
G

Greg Maxey

I'm not Gordon, but since I am mentioned by name in your post I will jump
in. Looking at the code your posted, you have not initialized any
additional listboxes. What have your tried wrt to the spinbutton coding.

I am sending you a small sample interactive UserForm\Document (Word2007).
It contains examples of what you will need to do for most common controls.
 

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