I have created a template, and a query in Access to create documents, however I must be missing something, as access says doc created, butt when I look at the created doc, all of my control fields are still there and no data has been added.
I will list the code below that creates the doc, and in the document template I have a table with $1, $2 etc for where the data should go. I have seen this done so I know it SHOULD work, but I guess something needs to be in the template to link it??
Code from Access:
Dim app As Word.Application
Dim doc As Word.Document
Dim i As Long
Set rst = CurrentDb.OpenRecordset("SELECT HWReplacedItems.StockLinkID, HWItems.Description, HWReplacedItems.DateIn, HWUsers.UserName AS Engineer, HWReplacedItems.SAP, HWReplacedItems.Batch, HWReplacedItems.ReplaceReason, HWReplacedItems.ItemIsRMA, HWReplacedItems.SourceSerial, HWReplacedItems.SourceCName, HWReplacedItems.SourceCTekref, HWStock.DateIn AS NIDateIn, HWItems_1.Description AS NIDescription, HWUsers_1.UserName AS NIEngineer, HWSources.SourceName AS NISource, HWStock.SAP AS NISAP, HWStock.Batch AS NIBatch, HWUsageReasons.Reason, HWUsed.SaleOrder, HWStock.PONumber, HWStock.RMAStockNumber AS NIRMAStockNumber, HWStock.RMAItemCode AS NIRMAItemCode, HWReplacedItems.RMAStockNumber, HWReplacedItems.RMAItemCode " & _
"FROM (((((((HWReplacedItems LEFT JOIN HWItems ON HWReplacedItems.ItemID = HWItems.ID) LEFT JOIN HWStock ON HWReplacedItems.StockLinkID = HWStock.ID) LEFT JOIN HWSources ON HWStock.SourceID = HWSources.ID) LEFT JOIN HWUsers ON HWReplacedItems.UserID = HWUsers.ID) LEFT JOIN HWItems AS HWItems_1 ON HWStock.ItemID = HWItems_1.ID) LEFT JOIN HWUsers AS HWUsers_1 ON HWStock.UserID = HWUsers_1.ID) LEFT JOIN HWUsed ON HWReplacedItems.StockLinkID = HWUsed.StockLinkID) LEFT JOIN HWUsageReasons ON HWUsed.UsageReasonID = HWUsageReasons.ID " & _
"WHERE (((HWReplacedItems.StatusID) = 2)) " & _
"ORDER BY HWReplacedItems.StockLinkID; ")
If Not rst.EOF Then
rst.MoveLast
rst.MoveFirst
i = 0
Do: iold = i: i = InStr(i + 1, CurrentDb.Name, "\"): Loop While i <> 0
If iold = 0 Then
MsgBox "Cannot extract Database Path. Exiting", vbOKOnly + vbCritical
Exit Sub
End If
Set app = New Word.Application
Do While Not rst.EOF
Set doc = app.Documents.Add(Left$(CurrentDb.Name, iold) & "faultform.dot")
For y = 1 To doc.Tables(1).Rows.Count
For x = 1 To 4
S = ""
On Local Error Resume Next
S = doc.Tables(1).Cell(y, x).Range.Text
On Local Error GoTo 0
i = 0
Do
i = InStr(i + 1, S, "$")
If i > 0 Then
ist = i
i = InStr(i + 1, S, "$")
If i > 0 Then
ien = i
tS = Mid(S, ist + 1, ien - (ist + 1))
For j = 1 To Len(tS)
If Mid$(tS, j, 1) < "0" Or Mid$(tS, j, 1) > "9" Then Exit For
Next j
If j = Len(tS) + 1 Then
ttS = ""
On Local Error Resume Next
ttS = rst.Fields(Val(tS) - 1).Value
On Local Error GoTo 0
S = Left$(S, ist - 1) & ttS & Right$(S, Len(S) - ien)
If Right$(S, 2) = Chr$(&HD) & Chr$(&H7) Then
S = Left$(S, Len(S) - 2) & Chr$(&H7)
End If
doc.Tables(1).Cell(y, x).Range.Text = S
End If
End If
End If
Loop While i > 0
Next x
Next y
' app.Visible = True
' app.Activate
DoEvents
' Stop
doc.SaveAs Left$(CurrentDb.Name, iold) & "RIDocs\Item-" & rst.Fields(0).Value & "-.doc"
doc.Close
MsgBox "'Item-" & rst.Fields(0).Value & "-.doc' was saved"
CurrentDb.Execute "Update HWReplacedItems Set StatusID=3 Where StockLinkID=" & rst.Fields(0).Value
Set doc = Nothing
rst.MoveNext
Loop
app.Quit
Set app = Nothing
End If
--------
The Set rst line is on 4 lines,
line 2 starting "FROM
Line 3 starting "WHERE
Line 4 starting "ORDER
The $1, $2 fields in the word template relate to the Set rst code to populate the relevant details, and this is the bit that does not work.
Any ideas greatly received!
I will list the code below that creates the doc, and in the document template I have a table with $1, $2 etc for where the data should go. I have seen this done so I know it SHOULD work, but I guess something needs to be in the template to link it??
Code from Access:
Dim app As Word.Application
Dim doc As Word.Document
Dim i As Long
Set rst = CurrentDb.OpenRecordset("SELECT HWReplacedItems.StockLinkID, HWItems.Description, HWReplacedItems.DateIn, HWUsers.UserName AS Engineer, HWReplacedItems.SAP, HWReplacedItems.Batch, HWReplacedItems.ReplaceReason, HWReplacedItems.ItemIsRMA, HWReplacedItems.SourceSerial, HWReplacedItems.SourceCName, HWReplacedItems.SourceCTekref, HWStock.DateIn AS NIDateIn, HWItems_1.Description AS NIDescription, HWUsers_1.UserName AS NIEngineer, HWSources.SourceName AS NISource, HWStock.SAP AS NISAP, HWStock.Batch AS NIBatch, HWUsageReasons.Reason, HWUsed.SaleOrder, HWStock.PONumber, HWStock.RMAStockNumber AS NIRMAStockNumber, HWStock.RMAItemCode AS NIRMAItemCode, HWReplacedItems.RMAStockNumber, HWReplacedItems.RMAItemCode " & _
"FROM (((((((HWReplacedItems LEFT JOIN HWItems ON HWReplacedItems.ItemID = HWItems.ID) LEFT JOIN HWStock ON HWReplacedItems.StockLinkID = HWStock.ID) LEFT JOIN HWSources ON HWStock.SourceID = HWSources.ID) LEFT JOIN HWUsers ON HWReplacedItems.UserID = HWUsers.ID) LEFT JOIN HWItems AS HWItems_1 ON HWStock.ItemID = HWItems_1.ID) LEFT JOIN HWUsers AS HWUsers_1 ON HWStock.UserID = HWUsers_1.ID) LEFT JOIN HWUsed ON HWReplacedItems.StockLinkID = HWUsed.StockLinkID) LEFT JOIN HWUsageReasons ON HWUsed.UsageReasonID = HWUsageReasons.ID " & _
"WHERE (((HWReplacedItems.StatusID) = 2)) " & _
"ORDER BY HWReplacedItems.StockLinkID; ")
If Not rst.EOF Then
rst.MoveLast
rst.MoveFirst
i = 0
Do: iold = i: i = InStr(i + 1, CurrentDb.Name, "\"): Loop While i <> 0
If iold = 0 Then
MsgBox "Cannot extract Database Path. Exiting", vbOKOnly + vbCritical
Exit Sub
End If
Set app = New Word.Application
Do While Not rst.EOF
Set doc = app.Documents.Add(Left$(CurrentDb.Name, iold) & "faultform.dot")
For y = 1 To doc.Tables(1).Rows.Count
For x = 1 To 4
S = ""
On Local Error Resume Next
S = doc.Tables(1).Cell(y, x).Range.Text
On Local Error GoTo 0
i = 0
Do
i = InStr(i + 1, S, "$")
If i > 0 Then
ist = i
i = InStr(i + 1, S, "$")
If i > 0 Then
ien = i
tS = Mid(S, ist + 1, ien - (ist + 1))
For j = 1 To Len(tS)
If Mid$(tS, j, 1) < "0" Or Mid$(tS, j, 1) > "9" Then Exit For
Next j
If j = Len(tS) + 1 Then
ttS = ""
On Local Error Resume Next
ttS = rst.Fields(Val(tS) - 1).Value
On Local Error GoTo 0
S = Left$(S, ist - 1) & ttS & Right$(S, Len(S) - ien)
If Right$(S, 2) = Chr$(&HD) & Chr$(&H7) Then
S = Left$(S, Len(S) - 2) & Chr$(&H7)
End If
doc.Tables(1).Cell(y, x).Range.Text = S
End If
End If
End If
Loop While i > 0
Next x
Next y
' app.Visible = True
' app.Activate
DoEvents
' Stop
doc.SaveAs Left$(CurrentDb.Name, iold) & "RIDocs\Item-" & rst.Fields(0).Value & "-.doc"
doc.Close
MsgBox "'Item-" & rst.Fields(0).Value & "-.doc' was saved"
CurrentDb.Execute "Update HWReplacedItems Set StatusID=3 Where StockLinkID=" & rst.Fields(0).Value
Set doc = Nothing
rst.MoveNext
Loop
app.Quit
Set app = Nothing
End If
--------
The Set rst line is on 4 lines,
line 2 starting "FROM
Line 3 starting "WHERE
Line 4 starting "ORDER
The $1, $2 fields in the word template relate to the Set rst code to populate the relevant details, and this is the bit that does not work.
Any ideas greatly received!