Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Display Message Box Value from Macro in Word Bookmarked Cell

0

So I am creating an order form that is comprized of Table/Cells in Word (365). I would like it to generate a random "8" digit number (Between 11111111-99999999) upon opening the document and insert said "Random Number" in a specific cell (which I have bookmarked as "BOL".

I have found some code that will generate a random number between the values I need but displays it in a message box. I would like the "Random Number" to not display in a message box and only fill in the specific cell/bookmark mentioned previously with the value that was generated. I've searched all over and cannot figure out what to edit the code to use the "value" generated for the "Random Number" and populate the cell/bookmark. 

Document will ultimately be protected with editing restrictions limited to filling in forms. I have successfully run the code listed below but could use some help in figuring out the rest. 

Sub Document_Open()

    'Generate_Random_Number ()

        maxNumber = 99999999

        minNumber = 11111111

        randomNumber = Int(maxNumber * Rnd) + minNumber

        MsgBox randomNumber

End Sub

Answer
Discuss

Discussion

Hi. Please note that I've corrected my Answer (originally done from distant memory!). It should work fine now.
John_Ru (rep: 6092) Sep 20, '22 at 8:59 am
Add to Discussion

Answers

0

Hi and welcome to the Forum. 

This is a forum for Excel only really but in your Word macro, replace the MsgBox line with that in bold below:

Sub Document_Open()

    'Generate_Random_Number ()

        maxNumber = 99999999

        minNumber = 11111111

        randomNumber = Int(maxNumber * Rnd) + minNumber

    ' write to bookmark
    ThisDocument.Bookmarks("BOL").Range.Text = randomNumber

End Sub

Hope this fixes things for you. If so, please remember to mark this Answer as Selected 

Discuss

Discussion

Guess you didn't see my answer (or think it worthy of comment/ selection). Oh hum! 
John_Ru (rep: 6092) Oct 4, '22 at 7:03 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login