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

Export Access Data to Word Document Issue

Status
Not open for further replies.

handsrfull

Technical User
Feb 22, 2008
8
US
The following code below will export data from access to a word document. The problem lies here: If one of the fields on the access form does not have data, the data export will stop at that field in the word document. Is there a way to skip that field if it does not have any data?

Here is my code:

Private Sub cmdExportToWord_Click()
On Error GoTo cmdExportToWord_Err

Dim objWord As Word.Application

'Start Microsoft Word 97.
Set objWord = CreateObject("Word.Application")

With objWord
'Make the application visible.
.Visible = True

'Open the document.
.Documents.Open ("C:\Path\Docname.doc")

'Move to each bookmark and insert text from the form.
.ActiveDocument.Bookmarks("Bookmarkname").Select
.Selection.Text = (CStr(Forms!frmTest!txtTest))
.ActiveDocument.Bookmarks("Bookmarkname").Select
.Selection.Text = (CStr(Forms!frmTest!txtTest))
.ActiveDocument.Bookmarks("Bookmarkname").Select
.Selection.Text = (CStr(Forms!frmTest!txtTest))
.ActiveDocument.Bookmarks("Bookmarkname").Select
.Selection.Text = (CStr(Forms!frmTest!txtTest))
.ActiveDocument.Bookmarks("Bookmarkname").Select
.Selection.Text = (CStr(Forms!frmTest!txtTest))
End With

'Save Form with new name
objWord.ActiveDocument.SaveAs InputBox("Save As what file name? This file will save" & _
" under your My Documents folder:", "Save FormName")

cmdExportToWord_Err:
'If a field on the form is empty, remove the bookmark text, and
'continue.
If Err.Number = 94 Then
objWord.Selection.Text = ""
End If

End Sub
 
Have a look at the IsNull or Nz functions.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top