Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with redundant code

Status
Not open for further replies.

Pegasus0990

Technical User
Nov 16, 2006
27
0
0
US
1) when you see the following
' first part ..... then 'second part
i will have to use this code over 100+ times. is there a good way to decrease the total amount of code? Note, I am using unique bookmarks in each one for code insertion. and if the instance is "yes" then it deletes all the code for that part. if "no" then it inserts the data.

2) if i do "Wordcount" it gives me a certain amount of characters that exist in my document. but if i tell my code to delete that many characters as in
Selection.Delete Unit:=wdCharacter, Count:=1996
even if my document says that there are 1996 characters, it acutally always fall short. why?

[VBCODE]
Public Sub CommandButton1_Click()
HideListBoxes

Dim oDoc As Word.Document

Set oDoc = Application.Documents("C:\Documents and Settings\word.doc")
Dim oExcel As Excel.Application, myWB As Excel.Workbook

Set oExcel = New Excel.Application
Set myWB = oExcel.Workbooks.Open("C:\Documents and Settings\excel.xls")

h = 2
i = 4
j = 6
l = 7

If Worksheets("Calculations").Cells(1, 1) = "0" Then
Selection.GoTo What:=wdGoToBookmark, Name:="part1"

Selection.Delete Unit:=wdCharacter, Count:=1996
Else
If Worksheets("Calculations").Cells(2, 1) = "0" Then
Selection.GoTo What:=wdGoToBookmark, Name:="one"
With Selection
.EndOf Unit:=wdParagraph, Extend:=wdExtend
.TypeBackspace
End With
Selection.Delete Unit:=wdCharacter, Count:=985
Else
' first time
a = 8
If Worksheets("Facility").Cells(a, i) = "No" Then
oDoc.Bookmarks("a11").Range.Text = myWB.Sheets("Facility").Cells(a, j)
oDoc.Bookmarks("a12").Range.Text = myWB.Sheets("Facility").Cells(a, l)
Else
Selection.GoTo What:=wdGoToBookmark, Name:="a1"
With Selection
.EndOf Unit:=wdParagraph, Extend:=wdExtend
.TypeBackspace
End With
Selection.Delete Unit:=wdCharacter, Count:=42
oDoc.Bookmarks("a1").Delete
End If
'second time
a = a + 1
If Worksheets("Facility").Cells(a, i) = "No" Then
oDoc.Bookmarks("a21").Range.Text = myWB.Sheets("Facility").Cells(a, j)
oDoc.Bookmarks("a22").Range.Text = myWB.Sheets("Facility").Cells(a, l)
Else
Selection.GoTo What:=wdGoToBookmark, Name:="a2"
With Selection
.EndOf Unit:=wdParagraph, Extend:=wdExtend
.TypeBackspace
End With
Selection.Delete Unit:=wdCharacter, Count:=42
oDoc.Bookmarks("a2").Delete
End If

[/VBCODE]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top