Jeffrey Lebowski
Member
Hi,
I am using a trigger event to create a word document which pulls information from the target.row that initiated the trigger event. I can get most of this done, but it gets tricky when I want to put some of the variables I define into a Word table. To clarify, I want to create a new table in word that will be filled with my variables from excel. I have been striking out with this. I think its a pretty specific request because I cant seem to find help in any forums. Any help would really be appreciated.
Thanks.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
' unhide notice sheet if hidden
If Sheets("Notice").Visible = False Then Sheets("notice").Visible = True
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim wd As Object
Dim Data As Range, Message As String
Dim thisRow As Long
Dim wsNotice As Worksheet
Dim EmpName As String, Ttype As String, ExpDate As String
Dim Vname As String, ExpDesc As String, CtAtt As String
Dim MealCost As String, AppName As String
' constants
Const ksRange = "Data_List"
Const ksTrigger = "Warning"
' declarations
Dim rng As Range
Set wsNotice = Sheets("Notice")
' start
Set rng = Range(ksRange)
thisRow = Target.Row
If Application.Intersect(Target, rng) Is Nothing Then GoTo Worksheet_Change_Exit
If Target.Cells.Count > 1 Then GoTo Worksheet_Change_Exit
' process
With Target
If .Value = ksTrigger Then
' these are the variable which will be populated into the word doc
EmpName = Cells(thisRow, "E").Value
Ttype = Cells(thisRow, "I").Value
ExpDate = Cells(thisRow, "C").Value
Vname = Cells(thisRow, "M").Value
ExpDesc = Cells(thisRow, "J").Value
CtAtt = Cells(thisRow, "U").Value
MealCost = Cells(thisRow, "P").Value
AppName = Cells(thisRow, "G").Value
End If
End With
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Add
With WordDoc
.Font.Size = 12
' .Font.Bold = True
.ParagraphFormat.Alignment = 0
.TypeText Text:="Date:" & vbTab & vbTab
.TypeParagraph
.TypeText Text:="To:" & vbTab & vbTab & EmpName
.TypeParagraph
.TypeText Text:="From:" & vbTab & vbTab & "Bob Costas, CEO"
.TypeParagraph
.TypeText Text:="Cc:" & vbTab & vbTab & AppName
.TypeParagraph
.TypeParagraph
.ParagraphFormat.Alignment = 1
.Font.Bold = True
.TypeText Text:="Warning"
.Font.Bold = False
.TypeParagraph
.ParagraphFormat.Alignment = 0
.TypeText Text:="One whole paragraph of text here"
.TypeParagraph
End With
''''''''i want to add a table here
With WordDoc
.TypeParagraph
.TypeText Text:="two more paragraphs of text here"
.TypeParagraph
.TypeParagraph
.TypeText Text:="Thank you."
'
End With
End With
' end
Worksheet_Change_Exit:
Set rng = Nothing
If Sheets("Notice").Visible = True Then Sheets("Notice").Visible = False
Application.ScreenUpdating = True
End Sub
`
I am using a trigger event to create a word document which pulls information from the target.row that initiated the trigger event. I can get most of this done, but it gets tricky when I want to put some of the variables I define into a Word table. To clarify, I want to create a new table in word that will be filled with my variables from excel. I have been striking out with this. I think its a pretty specific request because I cant seem to find help in any forums. Any help would really be appreciated.
Thanks.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
' unhide notice sheet if hidden
If Sheets("Notice").Visible = False Then Sheets("notice").Visible = True
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim wd As Object
Dim Data As Range, Message As String
Dim thisRow As Long
Dim wsNotice As Worksheet
Dim EmpName As String, Ttype As String, ExpDate As String
Dim Vname As String, ExpDesc As String, CtAtt As String
Dim MealCost As String, AppName As String
' constants
Const ksRange = "Data_List"
Const ksTrigger = "Warning"
' declarations
Dim rng As Range
Set wsNotice = Sheets("Notice")
' start
Set rng = Range(ksRange)
thisRow = Target.Row
If Application.Intersect(Target, rng) Is Nothing Then GoTo Worksheet_Change_Exit
If Target.Cells.Count > 1 Then GoTo Worksheet_Change_Exit
' process
With Target
If .Value = ksTrigger Then
' these are the variable which will be populated into the word doc
EmpName = Cells(thisRow, "E").Value
Ttype = Cells(thisRow, "I").Value
ExpDate = Cells(thisRow, "C").Value
Vname = Cells(thisRow, "M").Value
ExpDesc = Cells(thisRow, "J").Value
CtAtt = Cells(thisRow, "U").Value
MealCost = Cells(thisRow, "P").Value
AppName = Cells(thisRow, "G").Value
End If
End With
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Add
With WordDoc
.Font.Size = 12
' .Font.Bold = True
.ParagraphFormat.Alignment = 0
.TypeText Text:="Date:" & vbTab & vbTab
.TypeParagraph
.TypeText Text:="To:" & vbTab & vbTab & EmpName
.TypeParagraph
.TypeText Text:="From:" & vbTab & vbTab & "Bob Costas, CEO"
.TypeParagraph
.TypeText Text:="Cc:" & vbTab & vbTab & AppName
.TypeParagraph
.TypeParagraph
.ParagraphFormat.Alignment = 1
.Font.Bold = True
.TypeText Text:="Warning"
.Font.Bold = False
.TypeParagraph
.ParagraphFormat.Alignment = 0
.TypeText Text:="One whole paragraph of text here"
.TypeParagraph
End With
''''''''i want to add a table here
With WordDoc
.TypeParagraph
.TypeText Text:="two more paragraphs of text here"
.TypeParagraph
.TypeParagraph
.TypeText Text:="Thank you."
'
End With
End With
' end
Worksheet_Change_Exit:
Set rng = Nothing
If Sheets("Notice").Visible = True Then Sheets("Notice").Visible = False
Application.ScreenUpdating = True
End Sub
`