Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AddAsk to populate Bookmarks in a document

Status
Not open for further replies.

Waraq

Programmer
May 8, 2001
26
US
I am trying to convert a template so that when a new document is created with the template, the user gets prompted with a series of questions that then populate the bookmarks in the document.

It is at present being done with {} code directly on the document, but I want to convert it to a VB macro to make it cleaner. I haven't been able to track down anything other than doing it from a mail merge...HELP...
 
In your template's Open event place some code like the following:

Private Sub Document_Open()
Dim bkm As Bookmark
Dim strTemp As String
MsgBox "Checking Bookmarks"
For Each bkm In ActiveDocument.Bookmarks
strTemp = InputBox("Enter data for " & bkm.Name)
bkm.Range.Text = strTemp
'Doesn't show up in the MsgBox - don't know why
MsgBox bkm.Name & " " & bkm.Range.Text
Next bkm
Set bkm = Nothing
End Sub

Of course, you will also probably want to do an immediate SaveAs to prevent overwriting the .dot file.

Good Luck!


Have a great day!

j2consulting@yahoo.com
 
thanks for that code. It seems that it is also bringing up the references for the embedded object as well. any way around this?
 
I'm not sure I understand what you are saying. Can you explain a little bit more what you mean? I have to leave for a class so may not be able to check again until tomorrow. Sorry...

If there are some that you don't want to deal with you can put conditional logic in to skip them if that helps at all.

If bkm.Name <> &quot;YourBookMarkName&quot; Then
'Do something
End If

Are you getting some of what you need out of the code above or is it not helping you at all?

Good Luck!

Have a great day!

j2consulting@yahoo.com
 
The code you gave me is accomplishing most of what I want, but I got prompted with some weird bookmarks, not sure where they came from, so I figured if I did a check on the bookmark I could do an AddAsk type command.

basically I wanted to do an Ask to ask a question for information based on the bookmark.

i.e. case bkmName = &quot;clientName&quot;:
AddAsk(clientName, &quot;Please enter client name:&quot;)


Something like that.....


Thanks for all your assistance.

-c
 
Try something like this:

Private Sub Document_Open()
Dim bkm As Bookmark
Dim strTemp As String
MsgBox &quot;Checking Bookmarks&quot;
For Each bkm In ActiveDocument.Bookmarks
'Setup prompt string
strTemp = &quot;Enter data for &quot; & bkm.Name
Select Case bkm.Name
Case &quot;BookMarkName1&quot;
strTemp = InputBox(strTemp)
bkm.Range.Text = strTemp
Case &quot;BookMarkName2&quot;
strTemp = InputBox(strTemp)
bkm.Range.Text = strTemp
End Select
Next bkm
Set bkm = Nothing
End Sub

If you don't include a given BookMark in your select statement then it will not be processed by your code. An example of this might be where you fill in a default value, etc.

The above is based on a Word document BookMarks list. If you want to see them like a user would, go to your Word document and then on the View menu click BookMark. You will see a list of currently defined bookmarks. If you check the Hidden Bookmarks you will also see any that Word has created for its own use. If you select one and then click GoTo it will take you to that spot in the document.

By using the Select statement above in the second example you will only be reacting to those that you wish to handle.

Good LucK!

Have a great day!

j2consulting@yahoo.com
 
Thanks it was a lot of help, I modified the code to include what I needed. But the one problem that I am having is that even though throughout the document I have { REF bookmark name } statements, it is only populating the first instance of the bookmark on the document...any ideas?

Thanks again for the help.

Code:
Private Sub Document_New()
Dim bkm As Bookmark
Dim strTemp As String
    MsgBox &quot;Checking Bookmarks&quot;
    For Each bkm In ActiveDocument.Bookmarks
        'Setup prompt string
        'strTemp = &quot;Enter data for &quot; & bkm.Name
        Select Case bkm.Name
            Case &quot;companyName&quot;
                strTemp = InputBox(&quot;Please enter Company Name:&quot;)
                bkm.Range.Text = strTemp
            Case &quot;contactName&quot;
                strTemp = InputBox(&quot;Please enter Contact Name:&quot;)
                bkm.Range.Text = strTemp
            Case &quot;contactTitle&quot;
                strTemp = InputBox(&quot;Please enter Contact Title:&quot;)
                bkm.Range.Text = strTemp
            Case &quot;numberUsers&quot;
                strTemp = InputBox(&quot;Please enter the Number of Users:&quot;)
                bkm.Range.Text = strTemp
            Case &quot;fixedPrice&quot;
                strTemp = InputBox(&quot;Please enter Fixed Price (i.e. 15000):&quot;)
                bkm.Range.Text = strTemp
            Case &quot;kickoffDate&quot;
                strTemp = InputBox(&quot;Please enter Kickoff Date:&quot;)
                bkm.Range.Text = strTemp
            Case &quot;customerNotes&quot;
                strTemp = InputBox(&quot;Please enter any Customer Notes:&quot;)
                bkm.Range.Text = strTemp
        End Select
    Next bkm
    Set bkm = Nothing

End Sub
 
Hello Waraq,

Items created by the mail merge process are NOT bookmarks, they are part of the document's fields collection. If you open up the Object Browser, you will see Fields and within fields Result and within Result Text which will correspond to your Mail Merge data element name.

You need to convert those to BookMarks. Place your cursor where you want your data to be and then on the main menu click View and then Insert BookMark. This collection is what is being referenced by the code I have posted for you earlier. Sorry for the confusion.

Good Luck!

Have a great day!

j2consulting@yahoo.com
 
I wanted to thank you again, you definately helped me get on the right track. I have put my code down so you could see what I was aiming to do. Also, to fix my AddAsk problem, I just added a {ASK } field to the top of the document and it gets triggered by the Update command at the end.

Thanks Again.

Code:
Private Sub Document_New()
Dim bkm As Bookmark
Dim rngTemp As Range
Dim strTemp As String

Set rngTemp = ActiveDocument.Range(Start:=0, End:=0)
    
'Set Company Name
'MsgBox &quot;begin Set Company Name&quot;
ActiveDocument.MailMerge.Fields.AddSet Range:=rngTemp, _
    Name:=&quot;companyName&quot;, ValueText:=InputBox(&quot;Please enter the Company Name:&quot;, &quot;Company Name&quot;)
Selection.Collapse Direction:=wdCollapseEnd

'Set Contact Name
'MsgBox &quot;begin Set Contact Name&quot;
ActiveDocument.MailMerge.Fields.AddSet Range:=rngTemp, _
    Name:=&quot;contactName&quot;, ValueText:=InputBox(&quot;Please enter the Contact Name:&quot;, &quot;Contact Name&quot;)
Selection.Collapse Direction:=wdCollapseEnd

'Set Contact Title
'MsgBox &quot;begin Set Contact Title&quot;
ActiveDocument.MailMerge.Fields.AddSet Range:=rngTemp, _
    Name:=&quot;contactTitle&quot;, ValueText:=InputBox(&quot;Please enter the Contact's Title:&quot;, &quot;Contact Title&quot;)
Selection.Collapse Direction:=wdCollapseEnd

'Set Number Of Users
'MsgBox &quot;begin Set Num Users&quot;
ActiveDocument.MailMerge.Fields.AddSet Range:=rngTemp, _
    Name:=&quot;numberUsers&quot;, ValueText:=InputBox(&quot;Please enter the Number of Users:&quot;, &quot;# of Users&quot;)
Selection.Collapse Direction:=wdCollapseEnd

'Set Fixed Price
'MsgBox &quot;begin Set Fixed Price&quot;
ActiveDocument.MailMerge.Fields.AddSet Range:=rngTemp, _
    Name:=&quot;fixedPrice&quot;, ValueText:=InputBox(&quot;Please enter the fixed price of the deal:&quot; _
    & Chr(13) & Chr(10) & &quot;i.e. 15000&quot;, &quot;Deal Price&quot;, &quot;15000&quot;)
Selection.Collapse Direction:=wdCollapseEnd

'Set License Price
'MsgBox &quot;begin Set License Price&quot;
ActiveDocument.MailMerge.Fields.AddSet Range:=rngTemp, _
    Name:=&quot;licensePrice&quot;, ValueText:=InputBox(&quot;Please enter the price of each license:&quot; _
    & Chr(13) & Chr(10) & &quot;i.e. 60&quot;, &quot;License Price&quot;, &quot;60&quot;)
Selection.Collapse Direction:=wdCollapseEnd

'Set Support Price
'MsgBox &quot;begin Set Support Price&quot;
ActiveDocument.MailMerge.Fields.AddSet Range:=rngTemp, _
    Name:=&quot;supportPrice&quot;, ValueText:=InputBox(&quot;Please enter the price of support:&quot; _
    & Chr(13) & Chr(10) & &quot;i.e. 15&quot;, &quot;License Price&quot;, &quot;15&quot;)
Selection.Collapse Direction:=wdCollapseEnd

'Set Kickoff date
'MsgBox &quot;begin Set KickOff Date&quot;
ActiveDocument.MailMerge.Fields.AddSet Range:=rngTemp, _
    Name:=&quot;kickoffDate&quot;, ValueText:=InputBox(&quot;Please enter the estimated Kick-Off Date:&quot; _
    & Chr(13) & Chr(10) & &quot;i.e. January 1, 2004&quot;, &quot;Kickoff Date&quot;, &quot;January 1, 2004&quot;)
Selection.Collapse Direction:=wdCollapseEnd


ActiveDocument.Fields.Update
    
 
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top