nickirvine
05-13-2009, 05:25 AM
Hi,
I've had help from people on here in the past and you've been super. I'm trying to kind of adapt two codes now.
I have created a little form thing you type details into a form and it inserts them into bookmarks on letter on word. You press print it clears the bookmarks and the form then you can just do another one. Works super.
I know want the details you enter into the bookmarks to be saved onto an Excel spreadsheet and would really appreciate any help.
I've had a go by combining some code but i cant get it to work.
Its the command button1 sub i need changing. thats the print and do another button.
I have this, any pointers help really appreciated..
Option Explicit
Dim bname As String
Private Sub wb(bname, ByVal inhalt As String)
Dim r As Range
Set r = ActiveDocument.Bookmarks(bname).Range
r.Text = inhalt
ActiveDocument.Bookmarks.Add bname, r
End Sub
Private Sub ComboBox1_Change()
End Sub
Private Sub UserForm_Initialize()
With title
.AddItem "Mrs"
.AddItem "Mr"
.AddItem "Miss"
.AddItem "Ms"
.AddItem "Dr"
.AddItem "Rev"
End With
With chbenddate
.AddItem "1st June 2009"
.AddItem "7th September 2009"
End With
title.SetFocus
title.Text = ActiveDocument.Bookmarks("title").Range.Text
firstname.Text = ActiveDocument.Bookmarks("firstname").Range.Text
surname.Text = ActiveDocument.Bookmarks("surname").Range.Text
Address.Text = ActiveDocument.Bookmarks("Address").Range.Text
Processor.Text = ActiveDocument.Bookmarks("Processor").Range.Text
firstname.Text = ActiveDocument.Bookmarks("firstname").Range.Text
surname.Text = ActiveDocument.Bookmarks("surname").Range.Text
LastChild.Text = ActiveDocument.Bookmarks("LastChild").Range.Text
EntDate.Text = ActiveDocument.Bookmarks("EntDate").Range.Text
End Sub
Private Sub CommandButton3_Click()
Unload Me
Application.Move Left:=90, Top:=0
Application.Resize Width:=674, Height:=552
Application.Move Left:=51, Top:=0
ActiveDocument.Close SaveChanges:=False
End Sub
Private Sub CommandButton1_Click()
ActiveDocument.PrintOut Background:=False
Dim XLapp As Object, XLbook As Object, XLsheet As Object, Wbook As String, lastRow As Long, myRange As Range
On Error GoTo ErrorHandler
' Set application, workbook and worksheet objects
Wbook = "C:\my docs\worksheet.xls"
Set XLapp = GetObject(, "Excel.Application")
Set XLbook = XLapp.Workbooks.Open(Wbook)
Set XLsheet = XLbook.Worksheets("Sheet1")
With XLsheet
' Find last completed row on reporting log
.lastRow = .Cells(.Rows.Count, 1).Row + 1
' write information to reporting log
.Range("A" & lastRow + 1).Value = firstname
End With
' Close and save workbook and clear objects
Set XLsheet = Nothing
XLbook.Close SaveChanges:=True
Set XLbook = Nothing
Set XLapp = Nothing
' Error Handler
ErrorHandler:
If Err.Number = 429 Then
'Excel is not running, open Excel with CreateObject
Set XLapp = CreateObject("Excel.Application")
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
End If
title.Value = ""
Address.Value = ""
firstname.Value = ""
surname.Value = ""
EntDate.Value = ""
LastChild.Value = ""
Unload Me
chblettergen.Show
End Sub
Private Sub title_Change()
wb "title", title
wb "title2", title
End Sub
Private Sub Address_Change()
wb "Address", Address
End Sub
Private Sub Processor_Change()
wb "Processor", Processor
wb "Processor2", Processor
End Sub
Private Sub firstname_Change()
wb "firstname", firstname
wb "firstname2", firstname
wb "firstname3", firstname
wb "firstname4", firstname
End Sub
Private Sub surname_Change()
wb "surname", surname
wb "surname2", surname
wb "surname3", surname
wb "surname4", surname
wb "surname5", surname
End Sub
Private Sub LastChild_Change()
wb "LastChild", LastChild
End Sub
Private Sub EntDate_Change()
wb "EntDate", EntDate
End Sub
I've had help from people on here in the past and you've been super. I'm trying to kind of adapt two codes now.
I have created a little form thing you type details into a form and it inserts them into bookmarks on letter on word. You press print it clears the bookmarks and the form then you can just do another one. Works super.
I know want the details you enter into the bookmarks to be saved onto an Excel spreadsheet and would really appreciate any help.
I've had a go by combining some code but i cant get it to work.
Its the command button1 sub i need changing. thats the print and do another button.
I have this, any pointers help really appreciated..
Option Explicit
Dim bname As String
Private Sub wb(bname, ByVal inhalt As String)
Dim r As Range
Set r = ActiveDocument.Bookmarks(bname).Range
r.Text = inhalt
ActiveDocument.Bookmarks.Add bname, r
End Sub
Private Sub ComboBox1_Change()
End Sub
Private Sub UserForm_Initialize()
With title
.AddItem "Mrs"
.AddItem "Mr"
.AddItem "Miss"
.AddItem "Ms"
.AddItem "Dr"
.AddItem "Rev"
End With
With chbenddate
.AddItem "1st June 2009"
.AddItem "7th September 2009"
End With
title.SetFocus
title.Text = ActiveDocument.Bookmarks("title").Range.Text
firstname.Text = ActiveDocument.Bookmarks("firstname").Range.Text
surname.Text = ActiveDocument.Bookmarks("surname").Range.Text
Address.Text = ActiveDocument.Bookmarks("Address").Range.Text
Processor.Text = ActiveDocument.Bookmarks("Processor").Range.Text
firstname.Text = ActiveDocument.Bookmarks("firstname").Range.Text
surname.Text = ActiveDocument.Bookmarks("surname").Range.Text
LastChild.Text = ActiveDocument.Bookmarks("LastChild").Range.Text
EntDate.Text = ActiveDocument.Bookmarks("EntDate").Range.Text
End Sub
Private Sub CommandButton3_Click()
Unload Me
Application.Move Left:=90, Top:=0
Application.Resize Width:=674, Height:=552
Application.Move Left:=51, Top:=0
ActiveDocument.Close SaveChanges:=False
End Sub
Private Sub CommandButton1_Click()
ActiveDocument.PrintOut Background:=False
Dim XLapp As Object, XLbook As Object, XLsheet As Object, Wbook As String, lastRow As Long, myRange As Range
On Error GoTo ErrorHandler
' Set application, workbook and worksheet objects
Wbook = "C:\my docs\worksheet.xls"
Set XLapp = GetObject(, "Excel.Application")
Set XLbook = XLapp.Workbooks.Open(Wbook)
Set XLsheet = XLbook.Worksheets("Sheet1")
With XLsheet
' Find last completed row on reporting log
.lastRow = .Cells(.Rows.Count, 1).Row + 1
' write information to reporting log
.Range("A" & lastRow + 1).Value = firstname
End With
' Close and save workbook and clear objects
Set XLsheet = Nothing
XLbook.Close SaveChanges:=True
Set XLbook = Nothing
Set XLapp = Nothing
' Error Handler
ErrorHandler:
If Err.Number = 429 Then
'Excel is not running, open Excel with CreateObject
Set XLapp = CreateObject("Excel.Application")
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
End If
title.Value = ""
Address.Value = ""
firstname.Value = ""
surname.Value = ""
EntDate.Value = ""
LastChild.Value = ""
Unload Me
chblettergen.Show
End Sub
Private Sub title_Change()
wb "title", title
wb "title2", title
End Sub
Private Sub Address_Change()
wb "Address", Address
End Sub
Private Sub Processor_Change()
wb "Processor", Processor
wb "Processor2", Processor
End Sub
Private Sub firstname_Change()
wb "firstname", firstname
wb "firstname2", firstname
wb "firstname3", firstname
wb "firstname4", firstname
End Sub
Private Sub surname_Change()
wb "surname", surname
wb "surname2", surname
wb "surname3", surname
wb "surname4", surname
wb "surname5", surname
End Sub
Private Sub LastChild_Change()
wb "LastChild", LastChild
End Sub
Private Sub EntDate_Change()
wb "EntDate", EntDate
End Sub