I have developed vba code which opens Microsoft Word and inserts data into a Word Document. This works perfectly and I can do whatever I need to do with this if I manually write code for each precedent. What I am trying to do is to automate this code, so that a user can input the fields that need to go into each bookmark in practice, I have created a table which stores the name of the bookmark and the formula for each bookmark. Basically, I am trying to make it easy to create the precedents for each of the clients.
The problem is I don't know how to store the variables as code. For example, if I want to insert the words "insert words here", that is no problem but I cannot store something like "insert words said by " & Customer & "Here". Or alternatively, I might want to use a DLookup value for the bookmark. Does anyone have any idea if what i am trying to do is actually achievable.
So far the code i have is:
BMCount = Oapp.ActiveDocument.Bookmarks.Count
Debug.Print BMCount
x = 0
Set dbase = CurrentDb
Set recset1 = dbase.OpenRecordset("SELECT * FROM [Precedents]")
Set recset2 = dbase.OpenRecordset("Select * from [Precedent Variables]")
With recset1
.AddNew
recset1![PrecedentName] = Precedent
recset1![doctype] = "Court Documents"
recset1![Extension] = Extension
update
Debug.Print recset1![PrecedentName]
Debug.Print recset1![doctype] = Extension
Debug.Print recset1![Extension]
PrecedentID = recset1![ID]
End With
Do Until x = BMCount
x = x + 1
BMark = Oapp.ActiveDocument.Range.Bookmarks(x)
Debug.Print BMark
With recset2
.AddNew
recset2![Bookmark] = BMark
recset2![PrecedentID] = PrecedentID
strvar = InputBox("Please input the formula or data for the bookmark - " & BMark)
recset2![VariableFormula] = strvar
Debug.Print strvar
update
End With
Debug.Print BMark
Debug.Print strvar
With Oapp.ActiveDocument.Bookmarks
.Item(BMark).Range.Text = strvar
End With
Loop
My code and question seems to be confusing everyone including me. I will try to explain further what the problem is. I am trying to insert text into a bookmark in Word. I want the user to be able to decide what variable (or other code) to assign to each different bookmark. So if the bookmark is "customer", they could call a variable called customer (already defined) if the bookmark says "todaysdate" then the user can input Date() and the current date should be put in the bookmark instead of "Date()" In Excel I have no issues because I use the string with an "=" at the front. But Word seems to be different. Hopefully this makes what I am trying to do a little clearer.