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

faq703-760, Trouble with reports in word when word is open

Status
Not open for further replies.

NightZEN

Programmer
Apr 29, 2003
142
US
I have modified the code from faq703-760 to what you see below for my database (in Access 97). It works well, as long as MSWord is closed when you run the code. if it's open, I get the following message:

Word cannot open the existing "[square symbol here]

If I click OK, It creates the document as usual only in a new instance of Word, but when I close Word, it says that I have changed the Normal.dot template and do I want to save my changes!? If I wait at the message and do nothing, I get this error:

Run-time error '429'
ActiveX component can't create object

What I want is to be able to create a new document regardless of Word's state. Thanks for any insights! here's the code:

From a button on my Form:

Private Sub CbtnPrint_Click()
Dim db As DAO.Database
Dim recSubmittal As DAO.Recordset
Dim recSubmittal2 As DAO.Recordset
Dim strSQL As String
Dim strSQL2 As String


' Capture the field whose value will narrow your recordset down
strFCRNum = Me.[TbxFCRNum]



strSQL = "SELECT * FROM QryFCRReport WHERE [FCRNum]= " & strFCRNum & ";"
Set db = CurrentDb()
Set recSubmittal = db.OpenRecordset(strSQL)

strSQL2 = "SELECT * FROM QryFCRReportAttendSub WHERE [FCRNum]= " & strFCRNum & ";"
Set db = CurrentDb()
Set recSubmittal2 = db.OpenRecordset(strSQL2)



' This CreateSubmittal sub is created in the module
CreateSubmittal recSubmittal, recSubmittal2

End Sub





In a module:

Option Compare Database 'Use database order for string comparisons

Option Explicit

' location of the template for ECN BOARD REPORT Email -
Public Const m_strDIR As String = "S:\Transfer\Keith W\Databases Offline\Applications\"
Public Const m_strTEMPLATE As String = "FCRReview.dot"

' sets up objects for use and Public variables to be shared in database -
Private m_objWord As Word.Application
Private m_objDoc As Word.Document
Public strFCRNum As Variant


Public Sub CreateSubmittal(recSubmit As DAO.Recordset, recSubmit2 As DAO.Recordset)

Set m_objWord = New Word.Application
Set m_objDoc = m_objWord.Documents.Add(m_strDIR & m_strTEMPLATE)

m_objWord.Visible = True

InsertTextAtBookmark "BMCustName", recSubmit("CustName")
InsertTextAtBookmark "BMReviewDate", recSubmit("ReviewDate")
InsertTextAtBookmark "BMCustPlant", recSubmit("CustPlant")
InsertTextAtBookmark "BMCustPart", recSubmit("CustPart#")
InsertTextAtBookmark "BMReviewLocation", recSubmit("ReviewLocation")
InsertTextAtBookmark "BMNumberStations", recSubmit("NumberStations")
InsertTextAtBookmark "BMBlankLoadTableHeight", recSubmit("BlankLoadTableHeight")
InsertTextAtBookmark "BMExitConveyorHeight", recSubmit("ExitConveyorHeight")
InsertTextAtBookmark "BMMatlThickness", recSubmit("MatlThickness")
InsertTextAtBookmark "BMClampStroke", recSubmit("ClampStroke")
InsertTextAtBookmark "BMLiftStroke", recSubmit("LiftStroke")
InsertTextAtBookmark "BMJob", recSubmit("Job#")
InsertTextAtBookmark "BMPress", recSubmit("Press")
InsertTextAtBookmark "BMPitch", recSubmit("Pitch")
InsertTextAtBookmark "BMRailDistance", recSubmit("RailDistance")
InsertTextAtBookmark "BMSetsTooling", recSubmit("#SetsTooling")


'Generate the table data
InsertSummaryTable recSubmit2


Set m_objDoc = Nothing
Set m_objWord = Nothing


End Sub

Private Sub InsertTextAtBookmark(strBkmk As String, varText As Variant)
' This finds the bookmarks in the Word template to place the data.
m_objDoc.Bookmarks(strBkmk).Select
m_objWord.Selection.Text = varText & ""

End Sub

Private Sub InsertSummaryTable(recR As DAO.Recordset)
' This pulls in the data for a table then highlights the data
' and creates a table in the Word document at a bookmark location
' for each field you want in the column of the table, have tabs
' surround it. Items in quotes are field names from the query/recordset
' If you need to have a blank column, just place vbTab in twice
''On Error GoTo No_Record_Err
Dim strTable As String
Dim objTable As Word.Table

recR.MoveFirst
strTable = ""
While Not recR.EOF
strTable = strTable & recR("Attendee") & vbTab & recR("Company") & vbTab & recR("Signature") & vbCr
recR.MoveNext
Wend

InsertTextAtBookmark "BMTableAttendees", strTable
Set objTable = m_objWord.Selection.ConvertToTable(Separator:=vbTab)

objTable.Select
objTable.Columns(1).Width = InchesToPoints(2.5)
objTable.Columns(2).Width = InchesToPoints(2.5)
objTable.Columns(3).Width = InchesToPoints(0.5)

Set objTable = Nothing
Time_To_Go:
Exit Sub

No_Record_Err:
MsgBox "No Record Error Dude"
GoTo Time_To_Go

End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top