PDA

View Full Version : Problems with bookmarks



francozola25
07-21-2008, 05:56 AM
Hello

I was wondering if someone could help me. I am currently using inproperty fields in my macro to input a value into a usefrom textbox and update all inbuiltproperties field with that value.

I want to add another textbox called TextBox2 to my userform and do the same again but this time i want to use bookmarks.

I have tried numerous times but can't seem to get it to work please

Please see code


Set bDoc = Documents.Open("\\myserver\Forms\" & ExcelFN & ".Doc", ReadOnly:=True, Visible:=False)
'Set Title of aDoc = bDoc
bDoc.BuiltInDocumentProperties("Title") = _
aDoc.BuiltInDocumentProperties("Title")
Call UpdateStoryRanges(bDoc)
Call FillABookmark(bDoc, "here", TextBox2)



Sub UpdateStoryRanges(CurrentDoc As Document)
Dim oStory As Range
'Loop to update the main body of the document
For Each oStory In CurrentDoc.StoryRanges
Application.StatusBar = ""
'Update the all the FormFields
oStory.Fields.Update
Application.StatusBar = ""
If oStory.StoryType < wdMainTextStory Then
'Loop through main body of document until all fields are updated
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Application.StatusBar = ""
Wend
End If
Next oStory
Application.StatusBar = ""
Set oStory = Nothing
End Sub

Sub FillABookmark(CurrentDoc As Document, strBM As String, strText As String)
Dim oRange As Word.Range
Set oRange = CurrentDoc.Bookmarks(strBM).Range
CurrentDoc.Bookmarks(strBM).Range.Text = strText
With oRange
.Collapse Direction:=wdCollapseEnd
.MoveEnd Unit:=wdCharacter, Count:=Len(strText)
End With
CurrentDoc.Bookmarks.Add strBM, Range:=oRange
End Sub


I am opening files and updating the bookmarks.

i have inserted a bookmark called here before hand and saved. When i run the macro it does not input the value and i can't understand why.

OTWarrior
07-21-2008, 08:03 AM
What sets the value of strBM?

francozola25
07-21-2008, 08:10 AM
Thanks for your reply

This is when i go into the document and insert a bookmark named as here.

I have gone in and set this bookmark named "here" for 5 documents, so when my popup appears and insert data into TextBox2, the entered data will appear on the 5 documents

OTWarrior
07-21-2008, 08:59 AM
So when you insert data into textbox2, all 5 documents that have a bookmark called "here" will have that data filled into them? Your code says "current doc", yet it doesn't look like it cycles the document names. Would you mind explaining what you mean by 5 documents please.

Anyway, if you are adding multiple bookmarks, they cannot have the same name. The easiest way to populate multiple bookmarked fields would be to use an array and loop through.

peuso code:

List = array("here1", "here2", "here3", "here4", "here5")

for i = 0 to 4
Call FillABookmark(bDoc, List(i), TextBox2)
next i

francozola25
07-21-2008, 09:54 AM
Sorry OTWarrior

I was using 5 an example.

The background of the macro is that i have in total i have 25 documents each that need to have Title and Product Code in it.

What happens is that i am in word document, click a macro that asks me to enter in two values, one for Title and the other for Product Code. An excel sheet is called, taking the word document name and delcare this as variable, find this variable in the excel spreasheet. When found offset values to the right and add the values to a list then open the documents like so

Documents.Open("\\myserver\Forms\" & ExcelFN & ".Doc", ReadOnly:=True, Visible:=False)

Depending on the word document name found in the excel sheet, you could have 3,4,5 any amount of documents open and updated every time. Some will have as little as 2 forms attached while others may have 10.
Call UpdateStoryRanges(bDoc) so this will update any inbuiltproperties named Title. I want to use Bookmarks though to update the Product Code.

macropod
07-21-2008, 04:02 PM
Hi francozola25,

Assuming 'TextBox2' is a textbox on your userform, I think all you need to do is to change:
Call FillABookmark(bDoc, "here", TextBox2)
to:
Call FillABookmark(bDoc, "here", TextBox2.Text)

francozola25
07-22-2008, 02:22 AM
Hey i tried that

When i put this is in get a compile error TextBox2 invalid qualifier

OTWarrior
07-22-2008, 02:37 AM
if textbox2 is the name of the bookmark, it could be:

Call FillABookmark(bDoc, "here", "TextBox2")

francozola25
07-22-2008, 02:49 AM
here is the name of the bookmark

TextBox2 is the name of the textbox on mu userform

OTWarrior
07-22-2008, 04:23 AM
I am purely guessing here as I haven't used Userforms, but wouldn't you need to reference the fact that the object is on the userform than on the document itself?

something like:
Call FillABookmark(bDoc, "here", Forms!TextBox2)

or
Call FillABookmark(bDoc, "here", [TextBox2])

macropod
07-22-2008, 04:41 AM
Hi francozola25,

What is the code with the 'Call FillABookmark(bDoc, "here", TextBox2)' line attached to? Your userform, or some other sub? The reason I ask is that, if I create a userform with a textbox named 'Textbox2', then the following code updates the bookmark correctly:
Sub Test()
Dim bDoc As Document
Set bDoc = ActiveDocument
Call FillABookmark(bDoc, "here", UserForm1.TextBox2.Text)
End SubAs you'll see, I pointed bDoc to the ActiveDocument (for testing) and, because my sub wasn't attached to the userform, I had to specify the textbox reference as 'UserForm1.TextBox2.Text'. Other than that, everything worked as it should. You definitely need 'TextBox2.Text' (or 'TextBox2.Value') and, if the contents of TextBox2 are being retrieved by a sub that's independent of the userform, you'll need to add the userform reference.