Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-18-2023, 05:30 PM
Useful Useful is offline Word VBA code unable to find Bookmark Windows 10 Word VBA code unable to find Bookmark Office 2019
Novice
Word VBA code unable to find Bookmark
 
Join Date: Jul 2023
Posts: 6
Useful is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 07-18-2023, 07:08 PM
Useful Useful is offline Word VBA code unable to find Bookmark Windows 10 Word VBA code unable to find Bookmark Office 2019
Novice
Word VBA code unable to find Bookmark
 
Join Date: Jul 2023
Posts: 6
Useful is on a distinguished road
Default

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
Reply With Quote
  #3  
Old 07-18-2023, 09:46 PM
Guessed's Avatar
Guessed Guessed is offline Word VBA code unable to find Bookmark Windows 10 Word VBA code unable to find Bookmark Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #4  
Old 07-18-2023, 10:48 PM
Useful Useful is offline Word VBA code unable to find Bookmark Windows 10 Word VBA code unable to find Bookmark Office 2019
Novice
Word VBA code unable to find Bookmark
 
Join Date: Jul 2023
Posts: 6
Useful is on a distinguished road
Default 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
Reply With Quote
  #5  
Old 07-18-2023, 10:55 PM
Guessed's Avatar
Guessed Guessed is offline Word VBA code unable to find Bookmark Windows 10 Word VBA code unable to find Bookmark Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Is the vba userform still open? Does the textbox named txtName actually exist in that userform?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 07-18-2023, 11:07 PM
Useful Useful is offline Word VBA code unable to find Bookmark Windows 10 Word VBA code unable to find Bookmark Office 2019
Novice
Word VBA code unable to find Bookmark
 
Join Date: Jul 2023
Posts: 6
Useful is on a distinguished road
Default

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
Reply With Quote
  #7  
Old 07-18-2023, 11:23 PM
Useful Useful is offline Word VBA code unable to find Bookmark Windows 10 Word VBA code unable to find Bookmark Office 2019
Novice
Word VBA code unable to find Bookmark
 
Join Date: Jul 2023
Posts: 6
Useful is on a distinguished road
Default

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
Reply With Quote
  #8  
Old 07-19-2023, 06:46 AM
gmaxey gmaxey is online now Word VBA code unable to find Bookmark Windows 10 Word VBA code unable to find Bookmark Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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?
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 07-19-2023, 04:18 PM
Guessed's Avatar
Guessed Guessed is offline Word VBA code unable to find Bookmark Windows 10 Word VBA code unable to find Bookmark Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #10  
Old 07-19-2023, 10:42 PM
Useful Useful is offline Word VBA code unable to find Bookmark Windows 10 Word VBA code unable to find Bookmark Office 2019
Novice
Word VBA code unable to find Bookmark
 
Join Date: Jul 2023
Posts: 6
Useful is on a distinguished road
Default

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
Reply With Quote
Reply



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
Word VBA code unable to find Bookmark Field Code: Show a value only if bookmark is not empty Cosmo Word 7 08-24-2018 01:46 PM
Word VBA code unable to find Bookmark 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

Other Forums: Access Forums

All times are GMT -7. The time now is 08:03 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft