handsrfull
Technical User
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
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