#1
|
|||
|
|||
Write to document from Access form
I am not very familiar with the ins & outs of Word, so please be gentle.
I am working in Office 2007 Word & Access. I have a form in Access that the user fills out, then a command button that should open the document and fill in certain areas of the document with the data from the Access form so it can be e-mailed out. I have done this once in the 2003 version of Office and it all still works fine, even after converting to 2007. I copied the VBA code from the old database & put it in the one I'm working on now (changing field names of course), so I don't think that is where the problem is. I can get the document to open as new, but nothing is filled in. I think the problem is how to place the points I want autofilled on the Word document itself. I believe I used bookmarks in the past, but it doesn't look the same on the new document. It was a grey space between brackets on the other document, now it's just an I-beam when I go to the bookmark. Does anyone know how I can make this happen, or any links to instructions for 2007? I've searched the help & the forums, but haven't found anything helpful yet. Thanks for your help. |
#2
|
||||
|
||||
Hi NISMOJim,
The grey shading you refer to suggests your previous document used fields of some kind (possibly formfields or mergefields). Mergefields would ordinarily look like '«MyField»', where 'MyField' is the field name. Opeing the old document and pressing Alt-F9 should also quickly tell you which kind you were working with. Mergefields would display as {MERGEFIELD MyField} and formfields would display as {FORMTEXT}. It's also possible your old code used custom document properties and these were used by Document property fields, which would display as {DOCPROPERTY MyProp}, where 'MyProp' is the property name.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thanks for the reply. The spaces on the form say {FORMTEXT} (some say {FORMCHECKBOX}, but I never have been able to get those to work. I think that problem was in how to code it in Access VBA).
In normal view, I can right-click on one of the spaces to bring up Properties. That opens a Text Form Field Options window. The bottom half of this window has a Field Settings area. Under Bookmark: is the name given to this field/bookmark (EDate), which led me to believe it was a bookmark and not a Field. I've look up again how to insert fields on the new form. I clicked Insert, Quick Parts, then Field, but I'm not sure what category to use in this case. The ones I have tried still don't look like what is on the old document. They look more like formulas rather than text from a database. If you have an easily explained method I could use, that would probably help me sooner and with less confusion than the help files. Thanks again for your response. Edit: Here is the code from the Access form that does work if it might help; Code:
Private Sub ChkYInj_GotFocus() 'If the "Yes" box is checked indicating there was an injury, the Injury Report form opens Const DOC_PATH As String = "T:\Injury Reporting\" Const DOC_NAME As String = "Injury Form.doc" Dim appWord As Word.Application Dim doc As Word.Document Dim rst As ADODB.Recordset Dim strSQL As String On Error Resume Next Set appWord = GetObject(, "Word.application") If Err = 429 Then Set appWord = New Word.Application Err = 0 End If With appWord Set doc = .Documents(DOC_NAME) If Err = 0 Then If MsgBox("Do you want to save a copy of this form?", vbYesNo) = vbYes Then .Dialogs(wdDialogFileSaveAs).Show End If doc.Close False End If On Error Goto ErrorHandler Set doc = .Documents.Open(DOC_PATH & DOC_NAME, , True) Set rst = New ADODB.Recordset With doc .FormFields("name").Result = Nz(Forms!Input![J1Name]) .FormFields("date").Result = Nz(Forms!Input![NDate]) .FormFields("details").Result = Nz(Forms!Input![NDesc]) End With .Visible = True .Activate End With Set rst = Nothing Set doc = Nothing Set appWord = Nothing Exit Sub ErrorHandler: MsgBox Err & Err.Description End Sub Last edited by macropod; 06-16-2011 at 04:57 PM. Reason: Added code tags & formatting |
#4
|
||||
|
||||
Hi NISMOJim,
Your code is trying to populate three text formfields in the document, whose internal bookmark names are 'name', 'date' and 'details', respectively. You need to check that the document does indeed have three such text formfields (the ones that display {FORMTEXT} when you toggle the field code dispaly) with those names. To check this, select a given text formfield, right-click on it and choose 'properties'. If the formfield has no bookmark name, or the wrong one, correct it (I note that you refer to one named 'EDate', but that's not the name your code is looking for). Given that you're working with office 2007, you might want to confirm whether: Const DOC_NAME As String = "Injury Form.doc" should be: Const DOC_NAME As String = "Injury Form.docx"
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
I think I may be confusing everybody. This code is from a previous database than the one I'm currently working on. It fills out a seperate Word document than the one I'm working on. This code all works fine (although now that you mention it, I'm not sure why it works since it has the wrong suffix on it), so I was trying to duplicate it for my current application.
What I'm not getting is how to set up my new Word document with the same kind of form fields (or whatever they are) like the ones that are on the old document. I've tried Mail Merge, inserting a field and bookmarks, but nothing looks the same as the old document setup, and nothing I've tried is working. The old document that does work shows the fields as {FORMTEXT} like you said it would, but how do I get one of these on my new document? Thank you for your patience! |
#6
|
||||
|
||||
Hi NISMOJim,
To insert the same kinds of formfield in the new Word document, use the 'Legacy Forms' button on the Ribbon's Developer tab. What you need is the text formfields and you need to give them the appropriate bookmark names (as described above).
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Thanks so much. That Developer tab wasn't on my ribbon, and Legacy Forms don't even sound familiar. I never would have got this working if it weren't for your help!
Not to be too picky, but the field shows up shaded in gray with the brackets around it. How can I get it to show up without this so it looks like the rest of the document? Thanks again for your help. Things are starting to look up for this project! |
#8
|
|||
|
|||
OK, I got the brackets to go away, but not the gray shading in the textboxes. In Word Options, I selected 'Field Shading: Never' but it's still there.
Any ideas? |
#9
|
||||
|
||||
Hi NISMOJim,
Formfield shading is controlled by the 'Shading' button, accessed via the 'Legacy Forms' button on the Ribbon's Developer tab.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
Working perfect now!
Thanks so much for your help. |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Microsoft Access 2010 Error open a Web Form | WilfredoMolina | Office | 1 | 10-10-2010 06:47 AM |
Can you actually write HTML and CSS in a word document and send it as an html page | jackaroo | Word | 0 | 07-12-2010 07:49 AM |
Create a Custome Form and export data to Access | ashleybyrdnc | Office | 0 | 03-05-2010 09:41 AM |
Can I fill in Word document from Access table and/or query? | slim_jim_56 | Word Tables | 0 | 10-03-2007 01:59 PM |
Microsoft Access - Form For Sales Analysis | kennyharrill | Office | 0 | 09-15-2005 08:49 AM |