#1
|
|||
|
|||
Form updating Bookmarks - writes to the bookmarks multiple times
I have a template with one form having 3 fields that runs 1 time when a new doc is created.
I assign the input to 3 variables and assign the variables to bookmarks in the footer of the document. That works fine. Then I start to write the text in the document and sometimes the input stops to appear on the screen and the bookmarks in the footer have their content added one time every time I press enter. I have this on several PCs now. My code for the form is: Code:
Private Sub CommandButton1_Click() Set Snr = ActiveDocument.Bookmarks("Snr").Range Snr.Text = Me.TextBox1.Value Dim Bes As Range Set Bes = ActiveDocument.Bookmarks("Bes").Range Bes.Text = Me.TextBox2.Value Dim Ini As Range Set Ini = ActiveDocument.Bookmarks("Ini").Range Ini.Text = Me.TextBox3.Value UserForm1.Hide End Sub Any ideas? |
#2
|
||||
|
||||
Ideally, you'd only press Enter once. However, the basic problem is that you're not updating the bookmarks - you're simply using them as placeholders to insert the text. To update the bookmarks, you should be using code like:
Code:
Private Sub CommandButton1_Click() Application.ScreenUpdating = False Call UpdateBookmark("Snr", Me.TextBox1.Value) Call UpdateBookmark("Bes", Me.TextBox2.Value) Call UpdateBookmark("Ini", Me.TextBox3.Value) Application.ScreenUpdating = True UserForm1.Hide End Sub Sub UpdateBookmark(StrBkMk As String, StrTxt As String) Dim BkMkRng As Range With ActiveDocument If .Bookmarks.Exists(StrBkMk) Then Set BkMkRng = .Bookmarks(StrBkMk).Range BkMkRng.Text = StrTxt .Bookmarks.Add StrBkMk, BkMkRng End If End With Set BkMkRng = Nothing End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thanks.
The first part of your code is in place. I do not know where to put the second part of your code and when to run it. Could you clarify that for me? |
#4
|
||||
|
||||
You use the lot exactly as I posted it. Even so, the order in which you insert the subs doesn't matter.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Sorry I was too quick on the keyboard ... I should have read the code one more time before posting - sorry about that.
Your suggestion is doing this: As before the bookmarks are being filled out ... But when I start to type rest of the text in the document and a couple of lines down hit enter to start a new line and then hit enter again to have a blank line nothing happens . If I type a space or a character, I can type on. Your code has stopped the bookmarks to have their content added every time I press enter when the input stops to appear on screen. |
#6
|
||||
|
||||
I think you need to explain more clearly what you're doing with the document. The code I posted has no effect whatsoever on anything you type into the document or your userform - all it does is to ensure that clicking on CommandButton1 doesn't keep adding new content.
If your document is misbehaving, I can't really comment on that without seeing it. Can you attach a document to a post with some representative data, including your userform (delete anything sensitive)? You do this via the paperclip symbol on the 'Go Advanced' tab at the bottom of this screen.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Here is my setup
brev.dotm is the template I use to open a new letter placed in workgroup templates. mp2015.docm is placed in startup Blank/new Normal.dotm is in user templates enclosed 3 files |
#8
|
||||
|
||||
So which file are you having trouble with? I opened your MP2015.dotm & Normal.dotm files and can see nothing special about them - your Normal.dotm doesn't even contain any code. Indeed I can't even see how those files might be related to what's in this thread. I also opened your Brev.docm file and can see that, apart from your Document_New macro needing the same kind of changes made for the userform, it seems to work OK. Presumably, it too should be saved as a template; otherwise your Document_New macro will never run.
Code:
Private Sub Document_New() Call UpdateBookmark("DagsDato", Format(Date, " dd. mmmm yyyy")) UserForm1.Show End Sub Sub UpdateBookmark(StrBkMk As String, StrTxt As String) Dim BkMkRng As Range With ActiveDocument If .Bookmarks.Exists(StrBkMk) Then Set BkMkRng = .Bookmarks(StrBkMk).Range BkMkRng.Text = StrTxt .Bookmarks.Add StrBkMk, BkMkRng End If End With Set BkMkRng = Nothing End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
sorry wrong file uploaded .....
here is the correct Brev.dotm |
#10
|
||||
|
||||
Your template would work better if you had either the AutoNew macro or the Document_New macro. Presently, both load the userform, requiring everything to be input twice. Personally, I'd be inclined to use just the Document_New macro (it doesn't try to update the bookmark) and, instead of using code to insert the date, just use a CREATEDATE field in the header. Aside from that, I have no problems updating the document once the userform has closed.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
The problem does not occur in all new documents based on the brev.dotm template.
And sometimes it occurs more than one time in the same document. I can type in the adressee and the Re: line and maybe 2-3 paragraphs before it happens. |
#12
|
||||
|
||||
As I have already said, you template has both an AutoNew macro and a Document_New macro. Those macro both load your userform, requiring the data to be input twice. That's why you'll see it flicker and clear after the first time you input the data then close the form. If you check both the NewMacros module and the ThisDocument module in the template, you'll see what I am talking about.
As for what happens once the userform has closed, that has nothing to do with any code related to the userform. It is likely to be due to other things going on in your system - the problem does not occur on my system.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
Sorry for not being clear .
I have corrected the Document_New and the AutoNew - only one is in use now. It has just been tested on 2 PCs in another organisation ... same problem. If i remove all code from the template - no problem. |
#14
|
||||
|
||||
On my system there is 0 delay between typing and the appearance of the content on screen. Since the code in the template isn't running when I'm typing (or when you are typing), that is not the cause. I suggest you look at what else is running on your system, including any 3rd-party Word Addins that might be compromising things.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
bookmark, form, vba |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Can't See Bookmarks | bobmayo | Word | 21 | 06-04-2013 07:37 AM |
make a form that can be used multiple times at once | Dsp581 | Excel | 19 | 04-05-2013 07:25 AM |
Word only writes top half of input in form | Chippychap | Word | 8 | 07-20-2012 12:49 AM |
VBA code to extract specific bookmarks from multiple word files | Rattykins | Word VBA | 4 | 06-27-2012 10:02 PM |
Bookmarks for a PDF? | Ownaholic | Word | 0 | 10-30-2010 12:27 AM |