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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word/Access 2000: Change Document Field(s)?

Status
Not open for further replies.

marcus101

Programmer
Jan 21, 2003
64
CA
Hello:

Been playing around for a bit using MS Access and Word 2000.

I have a nice nifty form that is designed to help my office staff open and make simple changes to a Word document that acts as a template.

The template is connected to a series of Access fields that are used as mail merge fields, and they work great, so no problems there.

But here's the thing:

I really would LIKE to have some built in statically positioned, yet changeable-in-content WORD fields in the template document file that I can modify through VBA code from ACCESS.

The fields I am proposing are nothing fancy, but would contain no more than a few words each at most, so it's not sentences or paras or anything like that.

The idea is, open the document and change the field content and then save/close it before merging. At least, that's the plan in theory...

What's frustrating - Bookmarks are not good for this sort of thing. They can't be changed or edited without disappearing from the document entirely, so no luck there.

Word Fields are not much better from the looks of it so far.

I've been having great difficulty trying to figure out how to get them to change or be modified once I've set them initially as DocVariables - I have one static DocVariable in my template that I've added in Word manually called "strB1", which I'm using as a basic proof-of-concept test.

So this isn't a big deal or anything.

I suppose I may just have to break down and simply add these elements into my mail merge query strings and add these field items as merge fields into my template, but that seems like a lot of unnecessary data handling and duplication for SUCH a simple thing in theory/concept.

But I've spent nearly a week on this "simple" thing and can't afford any more time, so any suggestions, if they are fairly straightforward and not too bothersome, would be greatly appreciated.

Maybe someone can see something obvious that I've missed. I don't want to reinvent the wheel here.

For those who want to take a stab at it, here's my code so far-this is all MS ACCESS 2000 Code and works fine in 2003.

NOTE: There are no NULL issues with the code. I've taken care of those things already..;-)

Code:
' Setup/open Word, no problems getting the file loaded..

Dim WordDoc As Object
Dim tempDoc As String
tempDoc = "C:\Templates\File1.doc"
Dim sigVal As String

sigVal = MyForm.NameValue.Value
        
Set WordDoc = CreateObject("Word.Application")
WordDoc.Documents.Open (tempDoc)
    
If Not WordDoc.Visible Then 
    
     WordDoc.Visible = True
    
End If

' This line works in that it sets up the initial value of
' the strB1 Word Field variable along with Fields.Update (below) but does not CHANGE it..

WordDoc.ActiveDocument.Variables("strB1") = sigVal
    
' I was hoping these two lines would take care of the changing part..no luck so far..   

WordDoc.ActiveDocument.Fields.Item("DOCVARIABLE['strB1']") _
.Result = sigVal
WordDoc.ActiveDocument.Fields.Update

WordDoc.ActiveDocument.Save

' Set WordDoc = Nothing

Thanks,

marcus101
Access/SQL/XML Developer
Ottawa, Canada
 
Bookmarks are not good for this sort of thing. They can't be changed or edited without disappearing
Really ?
With WordDoc.Selection
.GoTo What:=-1, Name:="strB1" '-1=wdGoToBookmark
.Collapse Direction:=0 '0=wdCollapseEnd
WordDoc.ActiveDocument.Bookmarks("strB1").Range.Text = sigVal
.MoveEnd Unit:=1, Count:=Len(sigVal) '1=wdCharacter
WordDoc.ActiveDocument.Bookmarks.Add Name:="strB1", Range:=WordDoc.Selection.Range
.Collapse Direction:=0 '0=wdCollapseEnd
End With

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV:

Well, I gave up on this idea about 2 or 3 weeks ago, mostly because of the various issues caused partially by my inexperience and lack of time/urgency to get this little idea completed.

Even factoring that in, it seemed an awful pain to get something this simple conceptually to work even at the most basic level.

I eventually used a different approach, that of adding static customized fields in my query used for the mail merge, and then passed the form values into the SQL directly.

While this approach was a bit more involved, it at least DID work consistently and without issue when I completed the remainder of the code.

Once that was done, it was a fairly simple matter to link this SQL text data and add the new customized fields into the Word document as conventional Word mailmerge fields.

But then I also recently discovered this recently, regarding bookmarks, though I haven't really tried it yet:

(Funny how when you don't need ideas and code, that's when they tend to show up...:) )

Code:
Private Sub InsertTextAtBookmark(strBkmk as String, varText as Variant)

m_objDoc.Bookmarks(strBkmk).Select
m_objWord.Selection.Text = varText & ""

End Sub

NOTE: m_objDoc is a Word.Document private object variable
m_objWord is a Word.Application private object variable

I've used other variations similar to your code before posting my initial message for this thread.

But most of the time, there's always been issues when running it.

The code would either:

a) Not work as expected, such as deleting or removing the bookmark from the saved document (ie: it would seemingly work when running the merge at first, but then I'd close Word and then re-open it and the bookmark/field content changes would not be there, despite auto-saving), or not changing it at all,

b) Raise errors/issues with VBA references and conflicts, particularly with using "wdGoToBookmark" for some reason, which would not be recognized as a valid parameter.

It just took way too much time to figure all that out...

Thanks,

marcus101
Access/SQL/XML Developer
Ottawa, Canada
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top