I'm hoping someone can help me with this problem I am
having trying to format table columns when sending data
from Access. On the first instance I send data to Word,
the columns are formatted correctly, but if I do not keep
the Word doc open that the first instance created, each
new instance a new doc is opened and the code is ran the
the tables' columns do not get formatted. Anybody know
why????
This is the 'crux' of the code:
Option Compare Database
Option Explicit
Private objWord As Word.Application
Private objDoc As Word.Document
Dim db As DAO.Database
Dim db As DAO.Database
Dim rstPlans As DAO.Recordset
Dim rstAddenda As DAO.Recordset
Dim strPlans As String
Dim strAddenda As String
Dim objTable As Word.Table
' Turn hourglass on.
Screen.MousePointer = 11
Dim temp As Word.Application
'Test if object is already created before calling
CreateObject:
If TypeName(objWord) <> "Application" Then
Set temp = CreateObject("Word.Application")
Set objWord = CreateObject("Word.Application")
temp.Quit
Set temp = Nothing
End If
Set objDoc = objWord.Documents.Add
("F:\Templates\StandardSubcontractTemplate.dot")
' Activates Word
objWord.Visible = True
' Make db equal the current database
Set db = CurrentDb()
' Get recordset for main data
Set rstMainData = db.OpenRecordset(strMainData)
' Attempt to create a recordset based on the SQL
string
Set rstPlans = db.OpenRecordset(strSQLPlans)
rstPlans.MoveFirst
strPlans = ""
' Begin reading recordset into a string variable
While Not rstPlans.EOF
' Separate the Name and Date fields with a space
and enter each new record on a new line so the
ConvertToTable method can be used next
strPlans = strPlans & rstPlans("Name") & vbTab &
rstPlans("Date") & vbCr
' Move to the next record in set until the end
rstPlans.MoveNext
Wend
intBookmark = intBookmark + 1
objDoc.Bookmarks(intBookmark).Select
' Insert the string into the bookmark
' Convert the string data to a table
Set objTable = objWord.Selection.ConvertToTable
(Separator:=vbTab, Format:=wdTableFormatNone)
' Format the table
With objTable
.Borders.Enable = False
.Rows.SetLeftIndent LeftIndent:=70,
RulerStyle:=wdAdjustNone
.Columns(1).Width = InchesToPoints(5.85)
.Columns(2).Width = InchesToPoints(0.7)
End With
Set objTable = Nothing
Set rstPlans = Nothing
' Move the cursor to the top of the document.
objWord.Selection.HomeKey Unit:=wdStory
' Turn hourglass off.
Screen.MousePointer = 0
' Trash the objects in memory
Set db = Nothing
Set objWord = Nothing
Set objDoc = Nothing
Set rstMainData = Nothing
End Sub
> On the first instance I send data to Word,
> the columns are formatted correctly, but if I do not keep
> the Word doc open that the first instance created, each
> new instance a new doc is opened and the code is ran the
> the tables' columns do not get formatted. Anybody know
> why????
>
I see a number of inconsistencies and oddities in the code
sample you posted. For instance, why on earth do you create
to Word.Application objects (temp and objWord)?
<<If TypeName(objWord) <> "Application" >>
It's generally better to use If objWord Is Nothing to test
whether an object has been assigned to an object variable
> ' Activates Word
> objWord.Visible = True
>
This doesn't activate it, only makes it visible. This can
cause "weirdness" when automating Word. Try objWord.Activate
before making it visible
> objDoc.Bookmarks(intBookmark).Select
> ' Insert the string into the bookmark
>
I'm missing the code that actually puts the data in?
> ' Trash the objects in memory
>
> Set db = Nothing
> Set objWord = Nothing
> Set objDoc = Nothing
> Set rstMainData = Nothing
>
You should *definitely* set objDoc to nothing BEFORE
setting objWord to Nothing.
Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jan
24 2003)
http://www.mvps.org/word
This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:-)