VBA and bookmark naming

S

Sean

I am trying to reasign a bookmark to a user inputed
item. I am having problems at the
Add Range:=strConName line. I just need to reassign this
bookmark to the string entered in strConName.
Code is below.
Thanks.
Sean
**********************

Sub ConsultantName()
Dim strConName As String
strConName = InputBox(prompt:="Enter Consultant's Last
Name", Title:="Consultant Name")
If strConName = "" Then
Exit Sub
End If

ActiveDocument.Bookmarks("ConsultantName").Range.Text =
strConName

With ActiveDocument.Bookmarks
.Add Range:=strConName, Name:="ConsultantName"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With

End Sub
 
J

Jezebel

The Range argument is the section of the document that the Bookmark points
to. Since the name you have isn't actually in the document, you can't use a
Bookmark. Use a Document Property or Document Variable instead. Within the
document where you want to use the text, instead of using a REF field to get
bookmark content, use a DocProperty or DocVariable field.
 
J

Jezebel

You define a document property manually through the File > Properties
dialog. Built-in properties on the Summary tab, custom properties on the
Custom tab. Or you add them in code using something like:

ActiveDocument.CustomDocumentProperties.Add Name:="ConsultantName" ,
LinkToContent:=FALSE, Type:=msoPropertyTypeString, Value:=strConName

Within your document you have a field like { DocProperty ConsultantName }
 
S

Sean

using this code and a DocProperty "Consultant Name", the
field in the document comes up as "strConName" and not
the text that I enter into the inputbox. For example,
say I enter "Smith" in the inputbox. well the field does
not change.. it just reads "strConName"
Any ideas?
thanks.
sean

Sub ConsultantName()
Dim strConName As String
strConName = InputBox(prompt:="Enter Consultant's Last
Name", Title:="Consultant Name")
If strConName = "" Then
Exit Sub
End If
 
J

Jezebel

Could you post the code where you actually add the document property? My
guess is that you've got Value:="strConName" -- remove the quotes and it
should work.
 
S

Sean

I used the DocProperty fromt the File menu to add it.
In the value field I put strConName, but does it think
that strConName is what I want to be there so it
automatically includes the ""?
My DocProperty is set up with:
ConsultantName
Type is Text
Value is strConName

I could use the code you suggest below. However, I
thought it was the same thing. What section of code would
this be placed in?

ActiveDocument.CustomDocumentProperties.Add
Name:="ConsultantName" ,
LinkToContent:=FALSE, Type:=msoPropertyTypeString,
Value:=strConName
 
C

Chad DeMeyer

Is "strConName" the default value of the document property, until the user
supplies something else? Try running the macro, then go to File>Properties
and see if the value of the property changed. If it did, then it's just
that your field isn't updating. Do you have code that explicitly updates
the field?

Regards,
Chad
 
S

Sean

These are the only lines of code I have for the change.
Shouldn't changing strConName with the InputBox change
the value of ConsultantName?
When I look at the File>Properties DocProperty it is not
updated to reflect a change in the value of strConName...
it simply reads strConName... unchanged.

ActiveDocument.CustomDocumentProperty
Name:="ConsultantName", LinkToContent:=False,
Type:=msoPropertyTypeString, Value:=strConName

Sub ConsultantName()
Dim strConName As String
strConName = InputBox(prompt:="Enter Consultant's Last
Name", Title:="Consultant Name")
If strConName = "" Then
Exit Sub
End If
End Sub

thanx
 
C

Chad DeMeyer

Sean,

The line of code to set the property, as written, will do nothing. If you
are adding a new document property, you would use the add method of the
collection, e.g.,
ActiveDocument.CustomDocumentProperties.Add Name:="ConsultantName", etc.
However, if the property already exists and you just need to change the
value:
ActiveDocument.CustomDocumentProperties("ConsultantName").Value = strConName

Regards,
Chad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top