AWAISKAZMI
Registered User.
- Local time
- Today, 14:28
- Joined
- Oct 28, 2017
- Messages
- 12
Dear Experts;
So far I have created a data base that contains a table having 07 fields.
I have successfully export my data (extract from table) to word
Now i am in the need add 08 the field that is "OLE Object" each record have its separate OLE Object (Excel File)
now i want to print (export) table of excel in word..... tried a lot but failed
Kindly help me at which step my code is incorrect.
So far I have created a data base that contains a table having 07 fields.
I have successfully export my data (extract from table) to word
Now i am in the need add 08 the field that is "OLE Object" each record have its separate OLE Object (Excel File)
now i want to print (export) table of excel in word..... tried a lot but failed
Kindly help me at which step my code is incorrect.
Code:
Private Sub CMD_MY_DOC_Click()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
On Error GoTo errorhandler
Set cn = New ADODB.Connection
rs.ActiveConnection = CurrentProject.Connection
rs.Open ("select * from MySelectedObservations")
' Opening and Writing to Word Documnt
Dim objWord As Object
Dim pathgetter As String
Dim doc As Object
'Dim WordHeaderFooter As HeaderFooter
Dim filepath As String
Set objWord = CreateObject("Word.Application")
pathgetter = DLookup("Word_Path_Field", "WORD_PATH_TBL", "Serial = 1")
filepath = "" & pathgetter & "\Observations " & ".docx"
MsgBox "Please close Observation file first (if opened)"
With objWord
.Visible = True
Set doc = .Documents.Open(filepath)
End With
Dim DT_TM As String
DT_TM = "Observations up-to " & CStr(Now())
Dim d As Database
Dim rs1 As Recordset
Dim dept As Field
Dim head As Field
Dim obs As Field
Dim rimp As Field
Dim ratg As Field
Set dbs1 = CurrentDb()
Set rs1 = dbs1.OpenRecordset("MySelectedObservations")
Set dept = rs1.Fields("Department_Name")
Set head = rs1.Fields("Observation_Heading")
Set obs = rs1.Fields("Observation_Details")
Set tabl = rsl.Fields("Table")
Set rimp = rs1.Fields("Risk_Implication")
Set ratg = rs1.Fields("Risk_Category")
Dim dept_nm As String
dept_nm = "abcd"
With objWord.Selection
.Font.Name = "Times New Roman"
.Font.Size = 16
.Font.Bold = True
.Font.Underline = wdUnderlineSingle
.Font.Color = vbRed
.TypeText DT_TM
.Font.Color = vbBlack
.TypeParagraph
While rs1.EOF = False
If dept_nm <> dept.Value Then
.Font.Name = "Times New Roman"
.Font.Size = 10
.Font.Bold = True
.Font.Underline = wdUnderlineSingle
.TypeText dept.Value
.TypeText ":"
.TypeParagraph
dept_nm = dept.Value
End If
.Font.Name = "Times New Roman"
.Font.Size = 10
.Font.Bold = True
.Font.Underline = wdUnderlineSingle
.TypeText head.Value
.TypeText ":"
.TypeParagraph
.Font.Name = "Times New Roman"
.Font.Size = 10
.Font.Bold = False
.Font.Underline = wdUnderlineNone
.TypeText obs.Value
.TypeParagraph
'Copy Excel Table Range
Dim tbl As Excel.Range
Dim wordtable As Word.Table
Set tbl = tabl.OLEObject.worksheets(Sheet1.Name)
tbl.Copy
'Paste Table into MS Word
objWord.Paragraphs(1).Range.PasteExcelTable _
LinkedToExcel:=False, _
WordFormatting:=False, _
RTF:=False
'Autofit Table so it fits inside Word Document
Set wordtable = objWord.Tables(1)
wordtable.AutoFitBehavior (wdAutoFitWindow)
EndRoutine:
'Optimize Code
' Application.ScreenUpdating = True
' Application.EnableEvents = True
'Clear The Clipboard
' Application.CutCopyMode = False
.TypeParagraph
.Font.Name = "Times New Roman"
.Font.Size = 10
' .Font.TextColor = vbBlack
.Font.Bold = True
.Font.Underline = wdUnderlineNone
.TypeText "Risk Implication: "
.Font.Name = "Times New Roman"
.Font.Size = 10
' .Font.TextColor = vbBlack
.Font.Bold = False
.Font.Underline = wdUnderlineNone
.TypeText rimp.Value
.TypeParagraph
.Font.Name = "Times new Roman"
.Font.Size = 10
'.Font.TextColor = vbBlack
.Font.Bold = True
.Font.Underline = wdUnderlineNone
.TypeText "Risk Category: "
.Font.Name = "Times New Roman"
.Font.Size = 10
'.Font.TextColor = vbBlack
.Font.Bold = False
.Font.Underline = wdUnderlineNone
.TypeText ratg.Value
.TypeParagraph
.Font.Name = "Times New Roman"
.Font.Size = 10
'.Font.TextColor = vbBlack
.Font.Bold = True
.Font.Underline = wdUnderlineNone
.TypeText "Branch Remarks: "
.TypeParagraph
.TypeParagraph
rs1.MoveNext
Wend
' 'Add header and footer
'ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = "Audit & Inspection Division"
'ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = Now()
End With
doc.Save
doc.Activate
MsgBox "Your Observation File has been exported at your saved path"
errorhandler:
'MsgBox Err.Description
End Sub