?
Solved

VBA Word Userform call again with button if changes in the content

Posted on 2014-08-07
10
Medium Priority
?
631 Views
Last Modified: 2014-08-12
Hi guys,

I'm trying to figure out a way to recall the userform if there is some changes in the textboxes (typo's)

I found out how to call the userform with this code:

Private Sub Menu_Click()

UserForm.Show

End Sub

Open in new window


but when i make the changes and push ok it does not change the text but add's it to the current text.... is there a way to call it correctly?
0
Comment
Question by:Hakum
  • 5
  • 5
10 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40247612
To be clear about what you are asking.

I suppose that you have a UserForm set up so that when the user presses the 'OK' button, it loads data from the UserForm text boxes on to the document.

You now need a way for the user to open the UserForm again and repeat the process because he has noticed an error in the 'downloaded' text.

You must already have a macro to open the UserForm so select the dropdown arrow at the right of the Quick Access Toolbar (on the left of Word window's title bar). Choose 'More Commands..'  and  then 'Macros' in the top left-hand box of the 'Word Options' window. Find your macro, and move it across to the right-hand list with the 'Add' button. 'OK' the window away and you should have a new QAT button to open the UserForm again
0
 
LVL 1

Author Comment

by:Hakum
ID: 40248079
Hi GrahamSkan,

Thank you for your reply,

it is correctly understood that the userform will insert the data, and i want to repeat the process :)

that was the first thing i tried since it should work but i cant seem to get what i'm doing wrong since the code is not there... :S

have a look:

Untitled.png
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40248179
You are looking in the Normal template (the right-hand combo has 'For all documents' selected). You will need to choose your custom template from that list.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Hakum
ID: 40248300
ahhhh right.. SORRRY!!! my bad!! DOH! thank you for being soo patient with me im really trying to learn here, thank you sooo much for all the help!

That said I have no idea what i'm doing wrong for some reason the macro does not show.... i have tried to make a new module and make a call on it but it does not pop up on the list... am i doing something thing wrong? (obvisouly) hehe

i'm abit fustrated now....
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40248370
Sorry,
The previous advice I gave you refers to which template is to have the button.

The reason that your macro does not appear is that the procedure is 'Private'. Remove that word or replace the word with Public, and you should see the macro in the left-hand list.

This begs the question of 'how do you run the macro in the first place?'
0
 
LVL 1

Author Comment

by:Hakum
ID: 40255026
The button is working now, although the text that is already inserted does not get removed... is there a way to clear the content that was inserted at the bookmarks earlier??
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 40255217
If the bookmark is empty (with the start and the end in the same place) and If you set the text to the bookmark's range, the text will be placed after the bookmark. if you do it again, the same thing will happen, so
ActiveDocument.Bookmarks("mybmk").Range.Text = "ABC"
ActiveDocument.Bookmarks("mybmk").Range.Text = "DEF"

Open in new window

will produce "DEFABC" after the bookmark. If the bookmark is not empty, i.e. the length of its range is greater than zero, then the first line will replace the range with the new text. Unfortunately the bookmark is included in the range, so it gets deleted. The second line will then produce a run-time error.

I use a procedure like this if the bookmarks might have to be reused later:
Sub FillBookmark(doc As Document, strText As String, strBookMarkName As String)
    Dim rng As Range
    Set rng = doc.Bookmarks(strBookMarkName).Range
    rng.Text = strText
    're-add the bookmark to ensure that it still exists and that it contains the new text
    doc.Bookmarks.Add strBookMarkName, rng
End Sub

Open in new window

If the bookmarks have been filled as above, you can read the text and pre-load the userform like this
Sub UserForm_Activate()
    TextBox1.Text = ActiveDocument.Bookmarks("bookmark1").Range.Text
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:Hakum
ID: 40255250
Okay, makes sense! thank you explaining its so well specially for me as a newbie, i'm still learning :)

I havn't writtin it like as you have states and the bookmarks does not get deleted they stay where they should be..  this is how i have coded the bookmarks ref.

ActiveDocument.Bookmarks("momfrad").Select
Selection.TypeText Text:=Me.txtmomfrad

Open in new window


with the code you have states above, will i need to type in all the bookmarks in the code again or how??

I'm sooo fustrated!!!! arrrrrrggghhh!!!!! almost giving up on VBA!
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40255333
I'm afraid you will. It's a part of the learning process.
I have been trying to work out a way of doing a find and replace, but it's a bit too complicated for that.
0
 
LVL 1

Author Closing Comment

by:Hakum
ID: 40255427
Thank you sooo much for your patience and time! you rock!!!!

i will try to have another look at it in a few days... i'm even speaking VB'ish in my sleeep these days lol!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question