Part of the concept of using classes is to encapsulate the logic. As such,
only the class need to know how to check for valid data and generate the
report.
I would guess you are likely to have various report of different
format/requirements, so each could have it's own class.
So in the VBE, select InsertClass Module
'Declarations
Dim appWD As Word.Application
Private WS as Worksheet
Private WordFile As String
Private Sub Class_Initialize()
Set appWD = New Word.Application
End Sub
Private Sub Class_Terminate()
appWD.Quit
Set appWD=nothing
End Sub
'Set a reference to WS to use as source
Public Property Let SourceWS (vData as Excel.Worksheet)
Set WS=vData
End Property
'Tell the class which Word file to use
Public Property Let WordTemplateName (vData as String)
WordFile=vData
End Property
Public Function Generate (Optional LogResults As Boolean=True) As String
Dim ErrStr As String
On Error GoTo Handler
With appWD.Open WordFile
'write all the formatted bookmark data
'...& log data
'...& close/save
End With
Generate=""
Exit Function
Handler:
Select Case Err.Number
Case xx 'WS invalid
ErrStr="WS not set"
Case yy 'Word file invalid
ErrStr="Word file not valid"
Case zz 'data mising
ErrStr="Missing data"
Case Else
ErrStr=Err.description
End Select
Generate = ErrStr
'Clean up
End Function
Then in your WS all you need to do is you can do the whole thing in a couple
of lines:
Dim MyReport as cLOAProjectReport
Dim Retstr As String
With MyReport
Set .SourceWS = Me 'Or Worksheet("NewData") or whatever
.WordFile="\\lgwsvr011\<fullpath\Templates\LOA_Te mplate.doc"
RetStr=.Generate(False)
If RetStr<"" Then
'Error so decide what to do
End If
End With
NickHK
"ChrisMattock"
wrote in message
news:ChrisMattock.2aew7c_1152001202.9742@excelforu m-nospam.com...
Yes that all worked brilliantly in terms of Error Handling, any chance
you could explain this a little more? Not sure I understand :S
Given that you have a lot of related info, I would probably make a class
out
of this to handle its own reading from XL WS, validation and writing
to
Word.
e.g. <cLOAProjectReport
Public Property Let SourceWS (vData as Excel.Worksheet)
Public Property Let WordTemplateName (vData as String)
Public Function Generate (Optional LogResults As Boolean=True) As
String
'Return the address of the first cell missing data
--
ChrisMattock
------------------------------------------------------------------------
ChrisMattock's Profile:
http://www.excelforum.com/member.php...o&userid=33912
View this thread: http://www.excelforum.com/showthread...hreadid=557935