Exporting to word question

bonekrusher

Registered User.
Local time
Today, 08:49
Joined
Nov 19, 2005
Messages
266
Hi, I copied this code from one of the threads but I am having trouble with one line. The line that loads the info from Access in to my word template:

The Info I want in access is in a table called "Table 2" and the Field is called test "Test" (table2.test)

Please help!!!


Private Sub Command4_Click()


'Declare the follwing
Dim objWord As Object


'Set word as an application and make it invisible
Set objWord = CreateObject("Word.Application")
objWord.Visible = False 'True is visible

'path and name of the template your are using.
objWord.Documents.Add ("C:\Temp\AccessTest1.dot")

'This is for the bookmark that you created in the template
objWord.ActiveDocument.Bookmarks("bmCusDetails").Select

'This is the field in access that containts the data that has to be entered at the
'bookmark
objWord.Selection.Text = ???????



'Word (or the document that you created with the template, will now open)
objWord.Visible = True


Set objWord = Nothing



End Sub
 
The Info I want in access is in a table called "Table 2" and the Field is called test "Test" (table2.test)
Is there just one record in the table? if not what are the details of the criteria to filter to the record you need?

Peter
 
There is no criteria, its just a Table with one record. Eventually it will be one table with many records or may be a query.

Thanks for the help!
 
Well had some luck. I was able to do. I used the following code:

objWord.Selection.Text = Forms![frmselectList]![Text27]

Text27 is on the form which use the control source [table2]![test]. Now my new question is; I have a list box based on a query. I want all the items in the list box to be exported to my word Template. any idea
 
you will need to create a recordset in code and loop through it to insert the text into word.

HTH

Peter
 
Thanks Peter...As I am a novice, where is a good place to start?
 
something along this lines I think
Code:
Sub Word_1()
Dim appWord As Object
Dim wdDoc As Object
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("table2")

Set appWord = CreateObject("Word.Application")
Set wdDoc = appWord.Documents.Add ("C:\Temp\AccessTest1.dot")

rst.MoveNext
While Not rst.EOF
    objWord.Selection.Text = rst![test]
   rst.MoveNext
Wend
 

appWord.Quit 'savechanges:=False
Set wdDoc = Nothing
Set appWord = Nothing
End Sub

Not sure if that will give you a list or just keep over-writing the previous result but it should give you a start.

Peter
 
Can you look at this?

Peter, Can you look at this DB? It has the code above, but just keeps looping and Word never pops up.

Thanks,

Phil
 

Attachments

bonekrusher said:
Having trouble uploading the file.....

Hello i have just download your example db and it looks like just may of forgoten to declare objword as it errors and says "Object required"
 
bonekrusher said:
Thanks, I am a noobie. Can you help me with the code?

I think so when i have done it i will attach it and put in back in here
 
OK, I have tweaked a bit more. The code assumes a bookmark called 'book1'. I don't know the Word object model very well so hopefully alistair will come up with something :)

Peter
Code:
Sub Command2_Click()
Dim appWord As Object
Dim wdDoc As Object
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("table2")

Set appWord = CreateObject("Word.Application")
Set wdDoc = appWord.Documents.Add("C:\Temp\Accesstest2.dot")
wdDoc.Bookmarks("book1").Select
rst.MoveNext
While Not rst.EOF
    With appWord.Selection
        .Move Unit:=4
        .InsertParagraphAfter
        .Collapse Direction:=1
        .TypeText (rst![test])
        'rst![test]
    End With
   rst.MoveNext
Wend
 
appWord.Visible = True
'appWord.Quit 'savechanges:=False
Set wdDoc = Nothing
Set appWord = Nothing
End Sub
 
Bat17 said:
OK, I have tweaked a bit more. The code assumes a bookmark called 'book1'. I don't know the Word object model very well so hopefully alistair will come up with something :)

Peter
Code:
Sub Command2_Click()
Dim appWord As Object
Dim wdDoc As Object
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("table2")

Set appWord = CreateObject("Word.Application")
Set wdDoc = appWord.Documents.Add("C:\Temp\Accesstest2.dot")
wdDoc.Bookmarks("book1").Select
rst.MoveNext
While Not rst.EOF
    With appWord.Selection
        .Move Unit:=4
        .InsertParagraphAfter
        .Collapse Direction:=1
        .TypeText (rst![test])
        'rst![test]
    End With
   rst.MoveNext
Wend
 
appWord.Visible = True
'appWord.Quit 'savechanges:=False
Set wdDoc = Nothing
Set appWord = Nothing
End Sub

Checking my understanding want is the final document going to produce.

Alastair
 

Users who are viewing this thread

Back
Top Bottom