PDA

View Full Version : line breaks after a table



talytech
06-01-2012, 05:05 PM
I'm unsure if this is the correct forum I should be posting this question to so please forgive me in advance.

I am trying to create a word 2003 document from a command button on a MS access form. My reason for this is because the output method or the merge with MS Word method gives me issues with the memo fields truncating the data and messing up my reports.

So what I am attempting is a function that I will call from a command button.

So far I've been researching how to control Word using vba and I've made much progress; however I've been at a stumbling block for at least 9 hours trying to add a line break after my table. Here's what I have so far:

Function createWord()
Dim wordApp As Word.Application
Dim wordDoc As Word.Document
Dim wordTbl As Word.Table
Dim celTable As Cell
Dim intCount As Integer
Dim curDatabase As DAO.Database
Dim tbl As DAO.Recordset
Dim strAns As String
Dim strAns2 As String
Dim sql As String
sql = "SELECT tbl_Security_Event_2010.[Security Event Number], tbl_Security_Event_2010.[Security Event Type], tbl_Security_Event_2010.ComplainantType, tbl_Security_Event_2010.[Contact Phone] " _
& "FROM tbl_Security_Event_2010 WHERE (((tbl_Security_Event_2010.[Security Event Number])='dc-2012-1918'));"

Set curDatabase = CurrentDb
Set Recordset = curDatabase.OpenRecordset(sql, dbOpenSnapshot)

Set wordApp = New Word.Application
With wordApp
.Visible = True
Set wordDoc = .Documents.Add
End With

With wordDoc.PageSetup
.TopMargin = InchesToPoints(0.75)
.BottomMargin = InchesToPoints(0.75)
.LeftMargin = InchesToPoints(0.75)
.RightMargin = InchesToPoints(0.75)
'etc
End With


wordApp.Selection.Font.Name = "Tahoma"
wordApp.Selection.Font.Bold = True
wordApp.Selection.Font.Size = 14
wordApp.Selection.Font.Color = wdColorDarkBlue
wordApp.Selection.TypeText _
"Security Incident Report" & vbNewLine & vbNewLine
wordApp.Selection.Font.Bold = False
wordApp.Selection.Font.Size = 10
wordApp.Selection.Font.Color = False

wordApp.Selection.Font.Bold = True
wordApp.Selection.Font.Underline = wdUnderlineSingle
wordApp.Selection.TypeText "General Information" & vbNewLine & vbNewLine
wordApp.Selection.Font.Bold = False
wordApp.Selection.Font.Underline = False


wordApp.Selection.Font.Size = 8
Set wordTbl = wordDoc.Tables.Add( _
Range:=wordDoc.Range(Start:=Selection.Start, End:=Selection.End), NumRows:=5, _
NumColumns:=4)

wordTbl.Cell(1, 1).Range.InsertAfter ("Security Event Number:")
wordTbl.Cell(2, 1).Range.InsertAfter ("Security Event Type:")
wordTbl.Cell(3, 1).Range.InsertAfter ("Date/Time Event Began:")
wordTbl.Cell(4, 1).Range.InsertAfter ("Date/Time Event Reported:")
wordTbl.Cell(1, 3).Range.InsertAfter ("Reported method:")
wordTbl.Cell(2, 3).Range.InsertAfter ("Reported to:")
wordTbl.Cell(3, 3).Range.InsertAfter ("Reporting officer:")
wordTbl.Cell(4, 3).Range.InsertAfter ("SCC Monitor:")
wordTbl.Cell(5, 3).Range.InsertAfter ("Shift Supervisor on duty:")

'here's where I need a new line after my table

wordApp.Selection.TypeText "Next Information" & vbNewLine &



What I need to do is go to a new line after the table and insert text. I can't seem to figure that out.

Can someone help with this?

Frosty
06-02-2012, 09:49 AM
Without restructuring all of your code to not use the selection object (which will have its own issues, but if this is working so far, then so good).

Try recording macros in Word when you navigate. Such as recording a macro and hitting the End key to get to the end of the document... or CTRL + DOWN to move down one paragraph. You'll see things like

Selection.MoveDown wdCharacter, 1
and
Selection.EndKey wdStory
(that will take you to the end of the document)

There are a lot of ways to do this... you could also

dim rngWorking As Range
Set rngWorking = wordTbl.Range
rngWorking.Collapse wdCollapseEnd
rngWorking.InsertAfter = "Next Information" & vbNewLine

But that might be more than you're ready to tackle, since it starts working with ranges, and that is tougher to figure out.

All the Selection.Move (.MoveEnd, .MoveStart, .HomeKey and .EndKey) functions may help...