I have created an Access Form wherein i take the user input in various text boxs and text areas.
Now plz help me to find out how to create Microsoft Word Document automatically from the information which the user has submitted.
I have provided a button named submit which when clicked will submit the information into the Access Database and using that data i want to create a Word Document into a particular format.
What is the way in which this could be achieved?
Please help me out to find a solution to this problem.
You might do a search for merge it with word.
Assuming the data on your form is based on a table or query, you can export that data to a word document.
In access there is a function called merge with MsWord.
Try that one.
Or you can create a word template, add some bookmarks in it and write your user input to those bookmarks, creating a new doc based on this template.
Haven't tried this, but I think it would work
Or you can create a word template, add some bookmarks in it and write your user input to those bookmarks, creating a new doc based on this template.
Haven't tried this, but I think it would work
Indeed it does, since I use it in a small database I wrote for work. Assuming you want this to function at the click of a button, use the following code as the OnClick property of the button:
Code:
Private Sub cmdSend_Click()
Beep
Select Case MsgBox("Create a Document?" & vbCrLf, vbYesNo + vbQuestion, "Create Document")
Case vbYes:
'*****************************
'START OF FORM PRODUCTION CODE
'*****************************
' Create a Word document from template.
Dim WordApp As Word.Application
Dim strTemplateLocation As String
' Specify location of template
strTemplateLocation = "[COLOR="Red"][B]C:\EXAMPLE\EXAMPLE.DOT[/B][/COLOR]"
On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WordApp = CreateObject("Word.Application")
End If
On Error GoTo ErrHandler
WordApp.Visible = True
WordApp.WindowState = wdWindowStateMaximize
WordApp.Documents.Add Template:=strTemplateLocation, NewTemplate:=False
' Replace each bookmark with field contents.
With WordApp.Selection
.GoTo what:=wdGoToBookmark, Name:="[COLOR="Red"][B]bookmark1[/B][/COLOR]"
.TypeText [[B][COLOR="Red"]form-field1[/COLOR][/B]]
.GoTo what:=wdGoToBookmark, Name:="[COLOR="Red"][B]bookmark2[/B][/COLOR]"
' If the Access form field is blank, fill the bookmark with something else
If IsNull(rmalogged) Then
.TypeText "[COLOR="Red"][B]WHATEVER[/B][/COLOR]"
Else
.TypeText [[COLOR="Red"][B]form-field2[/B][/COLOR]]
End If
' Finish by putting the cursor to the top of the template letter
.GoTo what:=wdGoToBookmark, Name:="[COLOR="Red"][B]bookmarktop[/B][/COLOR]"
.TypeText " "
End With
DoEvents
WordApp.Activate
Set WordApp = Nothing
ErrHandler:
Set WordApp = Nothing
'***************************
'END OF FORM PRODUCTION CODE
'***************************
Case vbNo: 'Do not save or undo
'Do nothing
'Case vbCancel: 'Undo the changes
'DoCmd.RunCommand acCmdUndo
'Me.tbProperSave.Value = "No"
Case Else: 'Default case to trap any errors
'Do nothing
End Select
End Sub
You will need to do two things prior to this code working. Firstly, you will need to setup a template in Word and establish however many bookmarks you require into it. Once done, save it in a secure location on your PC.
Secondly, you'll need to alter all those sections of code that appear in bold red to suit your own needs. It is vital that your bookmarks share the same name as those listed under the Name:="X" section of the code. If you need any more help, just let me know.
I would like to know that when i am passing the values from Access Forms to Word by creating bookmarks then will the Rich Text retained in that...
All the tab information, Table Creation, etc.
Will all these rich text information retained when i try to pass them from Form to Word....
Ya i did try ur application.. It was working perfectly fine...
What my question here was.. In the Access Form what i have done is that i have included a Rich Text Format ActiveX Control..
This control enables me to give Rich Text into the Access Form... Most of the Word Processing Formats....
Now i have included a BookMark in the Word Document...
What i want is that this entire Rich Text Information which i have given via Access Forms is retained there in the Word Document as it is...
In the Application which u gave comprised of simple text.. Just that in place of just simple text i want to give Rich Text as input and that is retained as it is in the Word Document and is not lost in the way...
That was my question.. I hope i have been able to explain it to you..
But I do have one more question. Is it possible to save a document in a specific location, with a filename based on date and Costumername? And ofcourse save this filename in access?
Yes - look through the samples something done by ghudson
has a very smart function of making a directory on a server and creating a folder -
you can use this as the starting place
Indeed it does, since I use it in a small database I wrote for work. Assuming you want this to function at the click of a button, use the following code as the OnClick property of the button:
Code:
Private Sub cmdSend_Click()
Beep
Select Case MsgBox("Create a Document?" & vbCrLf, vbYesNo + vbQuestion, "Create Document")
Case vbYes:
'*****************************
'START OF FORM PRODUCTION CODE
'*****************************
' Create a Word document from template.
Dim WordApp As Word.Application
Dim strTemplateLocation As String
' Specify location of template
strTemplateLocation = "[COLOR="Red"][B]C:\EXAMPLE\EXAMPLE.DOT[/B][/COLOR]"
On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WordApp = CreateObject("Word.Application")
End If
On Error GoTo ErrHandler
WordApp.Visible = True
WordApp.WindowState = wdWindowStateMaximize
WordApp.Documents.Add Template:=strTemplateLocation, NewTemplate:=False
' Replace each bookmark with field contents.
With WordApp.Selection
.GoTo what:=wdGoToBookmark, Name:="[COLOR="Red"][B]bookmark1[/B][/COLOR]"
.TypeText [[B][COLOR="Red"]form-field1[/COLOR][/B]]
.GoTo what:=wdGoToBookmark, Name:="[COLOR="Red"][B]bookmark2[/B][/COLOR]"
' If the Access form field is blank, fill the bookmark with something else
If IsNull(rmalogged) Then
.TypeText "[COLOR="Red"][B]WHATEVER[/B][/COLOR]"
Else
.TypeText [[COLOR="Red"][B]form-field2[/B][/COLOR]]
End If
' Finish by putting the cursor to the top of the template letter
.GoTo what:=wdGoToBookmark, Name:="[COLOR="Red"][B]bookmarktop[/B][/COLOR]"
.TypeText " "
End With
DoEvents
WordApp.Activate
Set WordApp = Nothing
ErrHandler:
Set WordApp = Nothing
'***************************
'END OF FORM PRODUCTION CODE
'***************************
Case vbNo: 'Do not save or undo
'Do nothing
'Case vbCancel: 'Undo the changes
'DoCmd.RunCommand acCmdUndo
'Me.tbProperSave.Value = "No"
Case Else: 'Default case to trap any errors
'Do nothing
End Select
End Sub
You will need to do two things prior to this code working. Firstly, you will need to setup a template in Word and establish however many bookmarks you require into it. Once done, save it in a secure location on your PC.
Secondly, you'll need to alter all those sections of code that appear in bold red to suit your own needs. It is vital that your bookmarks share the same name as those listed under the Name:="X" section of the code. If you need any more help, just let me know.