Need help to resolve - Error appears "Object Required" (1 Viewer)

AWAISKAZMI

Registered User.
Local time
Today, 12:40
Joined
Oct 28, 2017
Messages
12
When i use this code i get error "Object Required"


Private Sub Command12_Click()
Dim cn As ADODB.Connection
Set rs = New ADODB.Recordset
On Error GoTo errorhandler
Set cn = New ADODB.Connection


Set rs = New ADODB.Recordset
rs.ActiveConnection = CurrentProject.Connection
rs.Open ("select * from MySelectedObservations")


Dim objWord As Word.Application
Dim pathgetter As String
Dim doc As Word.Document
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 " & Format(Now(), "dd-mm-yyyy") & ".docx"


With objWord
.Visible = True


Set doc = .Documents.Open(filepath)
End With


With objWord.Selection



.Font.Name = "Times New Roman"
.Font.Size = 10
.Font.TextColor = vbBlack
.Font.Bold = True
.Font.Underline = wdUnderlineSingle
'.Font.Bold = True


.TypeText Observation_Heading.Value
.TypeText ":"
.TypeParagraph


.Font.Name = "Times New Roman"
.Font.Size = 10
.Font.TextColor = vbBlack
.Font.Bold = False
.Font.Underline = wdUnderlineNone
.TypeText Observation_Details.Value
.TypeParagraph
.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 Risk_Implication.Value
.TypeParagraph

.Font.Name = "Times new Roman"
.Font.Size = 10
.Font.TextColor = vbBlack
.Font.Bold = True
.Font.Underline = wdUnderlineNone


.TypeText "Risk Category: "
.TypeParagraph

.Font.Name = "Times New Roman"
.Font.Size = 10
.Font.TextColor = vbBlack
.Font.Bold = False
.Font.Underline = wdUnderlineNone

.TypeText Risk_Category.Value
.TypeParagraph

.Font.Name = "Times New Roman"
.Font.Size = 10
.Font.TextColor = vbBlack
.Font.Bold = True
.Font.Underline = wdUnderlineNone


.TypeText "Branch Remarks: "
.TypeParagraph
.TypeParagraph


End With


doc.Save
doc.Activate





errorhandler:
MsgBox Err.Description


End Sub



Can anybody help me
 

AWAISKAZMI

Registered User.
Local time
Today, 12:40
Joined
Oct 28, 2017
Messages
12
now code is CORRECTED......... BUT still appearing same error

i have table "MySelectedObservations"
fields are:
Department_Name
Observation_Heading
Observation_Details
Risk_Implication
Risk_Category

i want to export these data in the table into word file with name saved in table "WORD_PATH_TBL"

Please help me to correct my code

Private Sub Command12_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")

Dim objWord As Word.Application
Dim pathgetter As String
Dim doc As Word.Document
Dim filepath As String
Set objWord = CreateObject("Word.Application")

pathgetter = DLookup("Word_Path_Field", "WORD_PATH_TBL", "Serial = 1")
filepath = "" & pathgetter & "\Observations " & Format(Now(), "dd-mm-yyyy") & ".docx"

With objWord
.Visible = True

Set doc = .Documents.Open(filepath)
End With

With objWord.Selection

.Font.Name = "Times New Roman"
.Font.Size = 10
.Font.TextColor = vbBlack
.Font.Bold = True
.Font.Underline = wdUnderlineSingle
'.Font.Bold = True

.TypeText Observation_Heading.Value
.TypeText ":"
.TypeParagraph

.Font.Name = "Times New Roman"
.Font.Size = 10
.Font.TextColor = vbBlack
.Font.Bold = False
.Font.Underline = wdUnderlineNone
.TypeText Observation_Details.Value
.TypeParagraph
.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 Risk_Implication.Value
.TypeParagraph

.Font.Name = "Times new Roman"
.Font.Size = 10
.Font.TextColor = vbBlack
.Font.Bold = True
.Font.Underline = wdUnderlineNone

.TypeText "Risk Category: "
.TypeParagraph

.Font.Name = "Times New Roman"
.Font.Size = 10
.Font.TextColor = vbBlack
.Font.Bold = False
.Font.Underline = wdUnderlineNone

.TypeText Risk_Category.Value
.TypeParagraph

.Font.Name = "Times New Roman"
.Font.Size = 10
.Font.TextColor = vbBlack
.Font.Bold = True
.Font.Underline = wdUnderlineNone

.TypeText "Branch Remarks: "
.TypeParagraph
.TypeParagraph

End With

doc.Save
doc.Activate

errorhandler:
MsgBox Err.Description

End Sub
 

JHB

Have been here a while
Local time
Today, 10:40
Joined
Jun 17, 2012
Messages
7,732
In which code line do you get the error?
 

Minty

AWF VIP
Local time
Today, 09:40
Joined
Jul 26, 2013
Messages
10,367
And please use the # symbol in the Reply box to surround your code in a formatted box.
 

Users who are viewing this thread

Top Bottom