PDA

View Full Version : Word Bookmark input capture



kkinsella
06-06-2008, 04:34 AM
Hi i am new to vb and have an issue im hoping someone can help me with.
:(
i have a word template that users initially fillin when opening info using input box prompts VB then puts the results of their inpur into designated bookmarks in template. I have a version table with 5 columns in my template the first row of infomration is filled by the VB input box macro by pointing at bookmark names as above, however i want users to be able add more version detail through the doc;s life and the detail to be populated in the next empty bookmark or row in the column. is there any way i can get a macro to look at a bookmark or table cell and test for an entry and if one exists then move to the next row to input the result of the input box?

many thanks in advance.
Kevin

fumei
06-06-2008, 08:26 AM
"is there any way i can get a macro to look at a bookmark or table cell and test for an entry and if one exists then move to the next row to input the result of the input box?"

Yes. Although I suggest that you make a decision here...

"to be populated in the next empty bookmark or row"

Which? Bookmark, or row?

kkinsella
06-06-2008, 10:56 AM
Hi thanks for reply, guess the descision is better made knowing which is easier.

but my preference would be for tables/rows as it can then auto grow as the versions increase.

thanks Kevin

fumei
06-06-2008, 11:04 AM
You need to fully express your logic. I would suggest actually writing it out.

Say you have something that puts text into Row2/Col2.

1. is that text the only text in that cell?
2. if you wish to change it, are you replacing the whole thing, or adding to it? If you are adding to it, are you adding to the end of what is already there? At the beginning of what is already there?
3. if you wish to test if anything is there already (and then put NEW text in a different cell:

"to look at a bookmark or table cell and test for an entry and if one exists then move to the next row to input the result of the input box?"

Does that row already exist? Or do you have to make a new row?

BTW: If you are asking multiple questions, it may be better to do them all on one userform, rather than multiple Inputboxes.

kkinsella
06-07-2008, 02:41 AM
Hi, had a few dabbles at a userform but settled for theinput boxes for ease, will probably have another look at it later.
the table i want to populate is 5 columns and at least 1 row initially. the input boxes prompt for info such as version no, author, date(auto captured), doc description, doc type (ie draft, etc) then fills in the first row as initial when doc is created from template. the user can then work in the doc as normal but they have a version button which they can press to then update the version detail in the table. the button starts another set of inputs asking the same detail as before but it must put answers in next empty row of the table. so in effect for instance first prompt is version no, when user gives answer, macro looks at table col 1, row 1, if there is a value pre exisiting it puts the users answer in col1, row 2, but if that has value it moves ot col 1, row 3 and so on. i can preset the number of rows if thats easier.. when it finds an empty col 1, row it then puts the value from the input box in that cell, and moves to the next action, which is date which it looks at col2, row 1 and checks then moves to col2, row 2 etc iit has value. the object being that each user can input their version detail each time they update the doc.
dont really mind if its using bookmarks or table.cell ref, i used bookmarks because it was easier.

many thanks. Kevin

fumei
06-09-2008, 06:23 AM
"i used bookmarks because it was easier"

Not really.

If you are using a Version button (executing a macro I assume), then a userform is definitely better. But whatever.

Can you post a sample document? I could do so, but it would be better to work with what you have.

kkinsella
06-09-2008, 09:21 AM
Hi Fumei thanks for your replys so far, have had some success i can now test if a bookmark exists and if not create the next one and move across the rows, however in an attempt to shorten the code and tidy things up i wondered if it was possible to set the answer to my bookmark tests as a variable as detailed below, ie, it checks to see if bookmark Ver2 exisits if it does it sets the two variabel vBK2 and vBK3 as the version names "Ver2" and "Ver3" however i cant get my code to then use these variabels in the rest of the calculation, can you help and explain where i am going wrong? many thanks Kevin.

example below
-------------------

Sub addv2test()
'checks if version 2 exists
If ActiveDocument.Bookmarks.Exists("Ver2") = True Then
vBK2 = Yes
End If
If vBK2 = Yes Then
vVersNew = ("Ver3")
vVersPrev = ("Ver2")
Else
MsgBox "failed"
End If
'Selection.GoTo ActiveDocument.FormFields vVersNum
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.FormFields.Add Range:=Selection.Range, Type:= _
wdFieldFormTextInput
Selection.PreviousField.Select
With Selection.FormFields(1)
.Name = vVersNum
.EntryMacro = ""
.ExitMacro = ""
.Enabled = True
.OwnHelp = False
.HelpText = ""
.OwnStatus = False
.StatusText = ""
With .TextInput
.EditType Type:=wdRegularText, Default:="", Format:=""
.Width = 0
End With
End With
'an input box to Update A New version of Doc.
Dim sVersion As String
sVersion = InputBox("Please Enter New Document Version Number and Click ok.", _
"New Document Version Number", _
"")
If StrPtr(sVersion) = 0 Then
MsgBox "You have cancelled all input action!"
ActiveDocument.FormFields(vVersNum).Delete
Exit Sub
Else
If Len(sVersion) = 0 Then
MsgBox "OK pressed but no data entered."
ActiveDocument.FormFields(vVersNum).Delete
addv2
Else

ActiveDocument.FormFields(vVersNum).Result = sVersion

End If 'Len(sVersion)
End If 'StrPtr
'UpDDDate
End Sub


Edit: VBA tags added to code

lucas
06-09-2008, 10:35 AM
Hi Kevin, if you select your code when posting and hit the VBA button it will format it as I have done to your code in the previous post...

fumei
06-09-2008, 10:44 AM
1. Please use the VBA tags when posting code.

2. Try to avoid using Selection.

3. Again, can you post a sample document?

4. " however i cant get my code to then use these variabels in the rest of the calculation, can you help and explain where i am going wrong?"

What does "I can't get my code to" mean? Do you get an error message? If so, then state clearly what it is. Does it go to specific line and highlight it? If so, what line?

Are you using Option Explicit? It does not look like it, as you do not appear to be declaring your variables.

Why do you use an IF test:

If ActiveDocument.Bookmarks.Exists("Ver2") = True Then

then, essentially test again with:

If vBK2 = Yes Then

It could be written as:

If ActiveDocument.Bookmarks.Exists("Ver2") = True Then
vVersNew = ("Ver3")
vVersPrev = ("Ver2")
Else



You use the line:

Selection.MoveDown Unit:=wdLine, Count:=1

but you have no test to see if the Selection is, in fact, in the right place. What if it isn't?? This is the main reason to avoid using Selection. Using Range allows you to run code regardless of where the Selection is.

5. The code:

If ActiveDocument.Bookmarks.Exists("Ver2") = True Then
vBK2 = Yes
End If
If vBK2 = Yes Then
vVersNew = ("Ver3")
vVersPrev = ("Ver2")
Else
MsgBox "failed"
End If

has - to me - a possible logical error. If Ver2 does NOT exist, you would eventually get the message "failed"...and go ahead and do the other instructions anyway. Is this correct?

As you do NOT declare your variables, it is hard to follow what is going on. You have:

ActiveDocument.FormFields(vVersNum).Delete

but I have absolutely no idea where vVersNum comes from, or what its value may be.