Please review code

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
 
P

Peter Hewett

Hi Rockey

When updating bookmarked text you don't need to select them. Selecting them
has high overheads, especially when they're in a Header or Footer as you've
found out. Use a range object and Header/Footer panes are not opened so you
don't need to worry about closing them. Here's a VBA procedure you'll need
to adapt it slightly for VB.Net:

Public Sub UpdateBookmarkedText(ByVal Bookmark As String, _
ByVal Value As String)

With ActiveDocument.Bookmarks
If .Exists(Bookmark) Then
.Item(Bookmark).Range.Text = Value
End If
End With
End Sub

It does not recreate the bookmark after updating it as you didn't in your
code. It's defensive in that it checks for the bookmark before updating it.
You can always replace this with a Try/Catch block if you just want to do
the update and catch the error if the bookmark did not exist.

HTH + Cheers - Peter


I am using a template with fixed cell dimensions.
I am taking information from a vb.net form and using bookmarks as
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 text
from the 2nd line forward to the next cell.

The following code now works - but I would like any tips or comments on
code to make it more efficient.

Thanks in advance.
Matt




Private Sub cmdGenerate_Click(ByVal sender As System.Object, ByVal
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 it
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 is
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 Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top