#1
|
|||
|
|||
Word VBA code unable to find Bookmark
I am not a programmer, but I'm trying to help a friend with document automation.
In a Word letter, I have constructed a Dialogue Box and the VBA code uses Bookmarks to go to specific areas and populate the letter with data entered in the Dialogue Box. This works fine on the first page. The problem is on the second page, where a Page Break is inserted by VBA code and the Header contains two Bookmarks. The plan is to go to the first Bookmark and insert text from the Dialogue Box, but the VBA code displays an error that it can't find the Bookmark. I can find that Bookmark manually in that Word document fine, so I don't know what I'm doing wrong. The code is: Selection.GoTo What:=wdGoToBookmark, Name:="Name1" Selection.TypeText Text:=txtName.Text I would be grateful for your help. Regards Useful |
#2
|
|||
|
|||
I have continued to research this problem and replaced my code with this to find the Bookmark:
ActiveDocument.Bookmarks("Name1").Select Selection.TypeText Text:=txtName.Text <--- this line is highlighted on Debug ActiveWindow.ActivePane.Close Now the error is: Run-time error '424': Object required and the line above is highlighted on Debug. I would be grateful for someone's help. Regards George |
#3
|
||||
|
||||
Using a selection object in a range other than the body of the document might be where the problem is coming from. Or perhaps the txtName textbox is no longer available.
If you tried using Range instead of Selection you may get it working better. Note that 'replacing text at a bookmark' is normally problematic as the contents of the bookmark are removed and the text appears beside (not inside) the bookmark unless steps are taken to handle this. See the following thread for inspiration https://www.msofficeforums.com/word-...-bookmark.html
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#4
|
|||
|
|||
Word VBA code displays run-time error 424
Thanks for your response Guessed.
I have done some further testing and with the new code, the Bookmark is now found whether it's in either the body of the document, or in the Header, but the Run-time error 424 is triggered by the next line (which I have marked before). Everything works fine on the first page. The problem is only with the second page. The data is entered in the beginning into the Dialogue Box by the user and is different each time, so I have no choice but to use it. I don't understand Run-time error 424 and why it's triggered, but I'm certain that this is the key. Perhaps the syntax has to be changed after the new code that is being used to find the Bookmark, however, not being a programmer, I have no clue as to what it should be. I would be grateful for your help. Regards Useful |
#5
|
||||
|
||||
Is the vba userform still open? Does the textbox named txtName actually exist in that userform?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#6
|
|||
|
|||
Perhaps it would be easier to understand, if I explained the process.
1. The Dialogue Box is displayed when the letter is opened, the User fills it in and it closes once the OK button is clicked. 2. All of the required fields in the letter are populated with the information entered by the User on the first page of the letter. 3. Subsequently, other code is run by the User to insert a second page into the letter and instead of the required field in the Header being populated from the original Dialogue Box, the Run-time error is displayed. I hope this makes more sense. Regards Useful |
#7
|
|||
|
|||
My apologies, I forgot to confirm that the textbox named txtName does actually exist in that userform and is used fine on the first page of the letter.
Regards Useful |
#8
|
|||
|
|||
First of all, unless you are using a very old version of Word, why are you using bookmarks?
Can you post your complete code or attached the problem document? |
#9
|
||||
|
||||
I agree with Greg that bookmarks are a poor option for the placing of information but that is not where your particular problem is coming from.
From your listed items in post 6 above, it sounds like you are running this code in step 3 (outside the userform itself). This gives me the indication of where your problem is coming from. If the userform is closed then the values you put in the userform are no longer available to other macros (which leads to an object doesn't exist error). While the txtName is available in the context of the Userform, it is not available to macros outside of the userform code if that userform is closed. If the userform was opened and then hidden (not closed) the value would be available but you would need to include a pointer to the userform's name (userformname.txtName.Text).
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#10
|
|||
|
|||
I am very grateful for your time.
The issue has now been resolved, probably clumsily by your standards, but as I am not a programmer, all I could do is use the Record Macro feature and go back to the original field and copy/paste it. Regards Useful |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
REf Fields show Bookmark whole cell when Bookmark is created by code. | pmcpowell | Word VBA | 2 | 11-16-2019 07:05 PM |
How do I code bookmark globally for different template? | Yong Xiang | Word VBA | 17 | 02-06-2019 01:09 PM |
Field Code: Show a value only if bookmark is not empty | Cosmo | Word | 7 | 08-24-2018 01:46 PM |
Find word, Insert Paragraph and bookmark | gattaca714 | Word VBA | 2 | 03-24-2017 09:23 PM |
Find Bookmark, move to bookmark, execute code, repeat | raymm3852 | Word VBA | 10 | 04-15-2016 06:21 PM |