R
Rockey
I am using a template with fixed cell dimensions.
I am taking information from a vb.net form and using bookmarks a
placeholders for the form data.
I am not using fixed-width fonts.
If the data wraps in the cell due to length - I need to move all tex
from the 2nd line forward to the next cell.
The following code now works - but I would like any tips or comments o
code to make it more efficient.
Thanks in advance.
Matt
Private Sub cmdGenerate_Click(ByVal sender As System.Object, ByVa
e As System.EventArgs) Handles cmdGenerate.Click
' Declare the variables
Dim m_WordServer As Word.ApplicationClass
m_WordServer = New Word.Application()
Dim word_filename As String
Dim wdGoToBookmark As Word.Bookmark
Dim myrange As Word.Range
Dim oTable As Word.Table
Dim oCell As Word.Cell
Dim lngBegRange As Long
Dim lngEndRange As Long
'Get the discharge template path
word_filename = GetRegValue("Software\\Documentation"
"DischargeTemplatePath")
Try
With m_WordServer
' Open and acitvate the document
.Documents.Add(word_filename)
.Visible = True
.Activate()
.Application.ScreenUpdating = False
' Populate the bookmarks from our form fields
.ActiveDocument.Bookmarks().Item("Name").Select()
.Selection.TypeText(Me.[PatientName].Text)
.ActiveDocument.Bookmarks().Item("DateOfDischarge").Select()
.Selection.TypeText(Me.[DateOfDischarge].Text)
.ActiveDocument.Bookmarks().Item("TodaysDate").Select()
.Selection.TypeText(Me.[TodaysDate].Text)
.ActiveDocument.Bookmarks().Item("DISCHARGED_DUE_TO").Select()
.Selection.TypeText(Me.[DISCHARGED_DUE_TO].Text)
.ActiveDocument.Bookmarks().Item("PATIENT_DISCHARGED_TO").Select()
.Selection.TypeText(Me.[PATIENT_DISCHARGED_TO].Text)
.ActiveDocument.Bookmarks().Item("Level_Of_Function").Select()
.Selection.TypeText(Me.[Level_Of_Function].Text)
.ActiveDocument.Bookmarks().Item("STG").Select()
.Selection.TypeText(Me.[STG].Text)
.ActiveDocument.Bookmarks().Item("LTG").Select()
.Selection.TypeText(Me.[LTG].Text)
.ActiveDocument.Bookmarks().Item("HOME_INSTRUCTION").Select()
.Selection.TypeText(Me.[HOME_INSTRUCTION].Text)
.ActiveDocument.Bookmarks().Item("FURTHER_CARE").Select()
.Selection.TypeText(Me.[FURTHER_CARE].Text)
' We populated a bookmark in the footer so make sure i
is closed and that we are in PrintView
.ActiveDocument.ActiveWindow.ActivePane.Close()
.ActiveDocument.ActiveWindow.View.Type
Word.WdViewType.wdPrintView
.Application.ScreenUpdating = True
.Visible = True
' Code below loops through the cells in the table
' If a cell has multiple lines - all text after the first line i
moved to the next cell
For Each oTable In .ActiveDocument.Tables
For Each oCell In oTable.Range.Cells
myrange = oCell.Range
myrange.End = myrange.End - 1
lngBegRange
myrange.Information(Word.WdInformation.wdVerticalPositionRelativeToPage)
myrange.Collapse(0) 'wdCollapseEnd
lngEndRange
myrange.Information(Word.WdInformation.wdVerticalPositionRelativeToPage)
If (lngBegRange <> lngEndRange) Then
myrange = oCell.Range
myrange.End = myrange.End - 1
myrange.Select()
' Move the selection to the next line
.Selection.MoveStart(unit:=5, Count:=1)
wdLine = 5
' Select the line
.Selection.Range.Select()
' Cut the selection
.Selection.Range.Cut()
' Move down one cell
.Selection.MoveRight(unit:=12) ' wdCell
12
' Paste the selection
.Selection.Paste()
End If
Next oCell
Next oTable
End With
Catch exc As Exception
MsgBox("Word Error!" + vbCrLf + _
vbCrLf + "Exception: " + exc.Message)
m_WordServer.Quit()
m_WordServer = Nothing
Finally
'm_WordServer.Quit()
'm_WordServer = Nothing
End Try
End Su
I am taking information from a vb.net form and using bookmarks a
placeholders for the form data.
I am not using fixed-width fonts.
If the data wraps in the cell due to length - I need to move all tex
from the 2nd line forward to the next cell.
The following code now works - but I would like any tips or comments o
code to make it more efficient.
Thanks in advance.
Matt
Private Sub cmdGenerate_Click(ByVal sender As System.Object, ByVa
e As System.EventArgs) Handles cmdGenerate.Click
' Declare the variables
Dim m_WordServer As Word.ApplicationClass
m_WordServer = New Word.Application()
Dim word_filename As String
Dim wdGoToBookmark As Word.Bookmark
Dim myrange As Word.Range
Dim oTable As Word.Table
Dim oCell As Word.Cell
Dim lngBegRange As Long
Dim lngEndRange As Long
'Get the discharge template path
word_filename = GetRegValue("Software\\Documentation"
"DischargeTemplatePath")
Try
With m_WordServer
' Open and acitvate the document
.Documents.Add(word_filename)
.Visible = True
.Activate()
.Application.ScreenUpdating = False
' Populate the bookmarks from our form fields
.ActiveDocument.Bookmarks().Item("Name").Select()
.Selection.TypeText(Me.[PatientName].Text)
.ActiveDocument.Bookmarks().Item("DateOfDischarge").Select()
.Selection.TypeText(Me.[DateOfDischarge].Text)
.ActiveDocument.Bookmarks().Item("TodaysDate").Select()
.Selection.TypeText(Me.[TodaysDate].Text)
.ActiveDocument.Bookmarks().Item("DISCHARGED_DUE_TO").Select()
.Selection.TypeText(Me.[DISCHARGED_DUE_TO].Text)
.ActiveDocument.Bookmarks().Item("PATIENT_DISCHARGED_TO").Select()
.Selection.TypeText(Me.[PATIENT_DISCHARGED_TO].Text)
.ActiveDocument.Bookmarks().Item("Level_Of_Function").Select()
.Selection.TypeText(Me.[Level_Of_Function].Text)
.ActiveDocument.Bookmarks().Item("STG").Select()
.Selection.TypeText(Me.[STG].Text)
.ActiveDocument.Bookmarks().Item("LTG").Select()
.Selection.TypeText(Me.[LTG].Text)
.ActiveDocument.Bookmarks().Item("HOME_INSTRUCTION").Select()
.Selection.TypeText(Me.[HOME_INSTRUCTION].Text)
.ActiveDocument.Bookmarks().Item("FURTHER_CARE").Select()
.Selection.TypeText(Me.[FURTHER_CARE].Text)
' We populated a bookmark in the footer so make sure i
is closed and that we are in PrintView
.ActiveDocument.ActiveWindow.ActivePane.Close()
.ActiveDocument.ActiveWindow.View.Type
Word.WdViewType.wdPrintView
.Application.ScreenUpdating = True
.Visible = True
' Code below loops through the cells in the table
' If a cell has multiple lines - all text after the first line i
moved to the next cell
For Each oTable In .ActiveDocument.Tables
For Each oCell In oTable.Range.Cells
myrange = oCell.Range
myrange.End = myrange.End - 1
lngBegRange
myrange.Information(Word.WdInformation.wdVerticalPositionRelativeToPage)
myrange.Collapse(0) 'wdCollapseEnd
lngEndRange
myrange.Information(Word.WdInformation.wdVerticalPositionRelativeToPage)
If (lngBegRange <> lngEndRange) Then
myrange = oCell.Range
myrange.End = myrange.End - 1
myrange.Select()
' Move the selection to the next line
.Selection.MoveStart(unit:=5, Count:=1)
wdLine = 5
' Select the line
.Selection.Range.Select()
' Cut the selection
.Selection.Range.Cut()
' Move down one cell
.Selection.MoveRight(unit:=12) ' wdCell
12
' Paste the selection
.Selection.Paste()
End If
Next oCell
Next oTable
End With
Catch exc As Exception
MsgBox("Word Error!" + vbCrLf + _
vbCrLf + "Exception: " + exc.Message)
m_WordServer.Quit()
m_WordServer = Nothing
Finally
'm_WordServer.Quit()
'm_WordServer = Nothing
End Try
End Su