Тема: Исправить запись данных из формы
Доброго времени, уважаемые! Извините, если тема создана не в том разделе.
Есть форма для ввода нескольких значений. По нажатию кнопки эти значения записываются на листах документа - выбор необходимого листа указывается в форме (1,2, и т.д.).
Листы в документе не одинаковые, и нужно правильно определить, в какую именно строку нужно записать данные из текст.формы.
Вопрос в том, как определить необходимую строку в листе, чтобы выполнить запись один раз (чтобы избежать дублей)?
В данный момент запись дублируется в двух строках. Листинг кода и скриншоты прилагаю.
Private Sub CommandButton14_Click()
Const y = mm * 276
Const dh1 = mm * 5 'изменения по высоте y
Const dh2 = mm * 7 'изменения по высоте y
Const dv1 = mm * 7 'изменения по длине x
Const dv2 = mm * 10 'изменения по длине x
Const dv3 = mm * 23 'изменения по длине x
Const dv4 = mm * 15
Dim Shp As shape, izmshp As shape
Dim PageRng As Range
Dim SectionNmb As Integer, PageNmb As Integer
If TextBox25.text = "" Then ActiveDocument.Variables("Izm").Value = " " Else ActiveDocument.Variables("Izm").Value = TextBox25.text
If TextBox26.text = "" Then ActiveDocument.Variables("NomIzv").Value = " " Else ActiveDocument.Variables("NomIzv").Value = TextBox26.text
If CheckBox1 Then IzmNovZam = "ИЗМ"
If CheckBox2 Then IzmNovZam = "НОВ"
If CheckBox3 Then IzmNovZam = "ЗАМ"
' If Optlaser.Value Then kb1 = mm * 3.65
Cansel1 = True
Unload Me
Set rng = Selection.Range
pgs = TextBox27.text
Do
If pgs <> "" Then
pgz = InStr(pgs, ",")
If pgz <> 0 Then
pg0 = Left(pgs, pgz - 1)
pgs = Right(pgs, Len(pgs) - pgz)
Else
pg0 = pgs
pgs = ""
End If
pgd = InStr(pg0, "-")
If pgd <> 0 Then
pg1 = Left(pg0, pgd - 1)
pg2 = Right(pg0, Len(pg0) - pgd)
Else
pg1 = pg0
pg2 = pg0
End If
Else
End If
For i = Val(pg1) To (pg2)
If TextBox27.text <> "" Then
Selection.GoTo What:=wdGoToPage, name:=i
Set PageRng = Selection.Range
Else
Set PageRng = rng
End If
SectionNmb = PageRng.Information(wdActiveEndSectionNumber)
For Each Shp In ActiveDocument.Sections(SectionNmb).Headers(wdHeaderFooterPrimary).Shapes
If Shp.Type = msoTextBox Then
If Left(Shp.TextFrame.TextRange.text, 3) = "Изм" Then
If Shp.TextFrame.TextRange.Information(wdActiveEndSectionNumber) = SectionNmb Then
Set izmshp = Shp
' izmshp.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
' izmshp.RelativeVerticalPosition = wdRelativeVerticalPositionPage
Select Case izmshp.TextFrame.Orientation
Case msoTextOrientationHorizontal
If izmshp.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage Then
dx1 = izmshp.Left
ElseIf izmshp.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn Then
dx1 = izmshp.Left + ActiveDocument.Sections(SectionNmb).PageSetup.LeftMargin
Else
rng.Select
Exit Sub
End If
If izmshp.RelativeVerticalPosition = wdRelativeVerticalPositionPage Then
dy1 = izmshp.Top - izmshp.Height
ElseIf izmshp.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph Then
dy1 = izmshp.Top + izmshp.Height + ActiveDocument.Sections(SectionNmb).PageSetup.PageHeight - _
ActiveDocument.Sections(SectionNmb).PageSetup.TopMargin + ActiveDocument.Sections(SectionNmb).PageSetup.HeaderDistance - _
ActiveDocument.Sections(SectionNmb).PageSetup.FooterDistance
Else
rng.Select
Exit Sub
End If
ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, dx1, dy1, dv1, dh1, PageRng).Select
Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionPage
Selection.ShapeRange.Left = dx1
Selection.ShapeRange.Top = dy1
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.ShapeRange.TextFrame.MarginTop = 0
Selection.Font.size = 12
Selection.TypeText text:=""
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, text:="DOCVARIABLE ""Izm""", PreserveFormatting:=True
dx1 = dx1 + dv1
ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, dx1, dy1, dv2, dh1, PageRng).Select
Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionPage
Selection.ShapeRange.Left = dx1
Selection.ShapeRange.Top = dy1
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.ShapeRange.TextFrame.MarginTop = 0
Selection.Font.size = 12
Selection.TypeText text:=IzmNovZam
'Selection.TypeText Text:=""
'Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="DOCVARIABLE ""izm""", PreserveFormatting:=True
dx1 = dx1 + dv2
ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, dx1, dy1, dv3, dh1, PageRng).Select
Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionPage
Selection.ShapeRange.Left = dx1
Selection.ShapeRange.Top = dy1
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.ShapeRange.TextFrame.MarginTop = 0
Selection.Font.size = 12
Selection.TypeText text:=""
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, text:="DOCVARIABLE ""NomIzv""", PreserveFormatting:=True
Case msoTextOrientationDownward
If izmshp.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage Then
dx1 = izmshp.Left + izmshp.Width
ElseIf izmshp.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn Then
dx1 = izmshp.Left + izmshp.Width + ActiveDocument.Sections(SectionNmb).PageSetup.LeftMargin
Else
rng.Select
Exit Sub
End If
If izmshp.RelativeVerticalPosition = wdRelativeVerticalPositionPage Then
dy1 = izmshp.Top
ElseIf izmshp.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph Then
dy1 = izmshp.Top + ActiveDocument.Sections(SectionNmb).PageSetup.HeaderDistance
Else
rng.Select
Exit Sub
End If
ActiveDocument.Shapes.AddTextbox(msoTextOrientationDownward, dx1, dy1, dh1, dv1, PageRng).Select
Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionPage
Selection.ShapeRange.Left = dx1
Selection.ShapeRange.Top = dy1
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.ShapeRange.TextFrame.MarginTop = 0
Selection.Font.size = 12
Selection.TypeText text:=""
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, text:="DOCVARIABLE ""Izm""", PreserveFormatting:=True
dy1 = dy1 + dv1
ActiveDocument.Shapes.AddTextbox(msoTextOrientationDownward, dx1, dy1, dh1, dv2, PageRng).Select
Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionPage
Selection.ShapeRange.Left = dx1
Selection.ShapeRange.Top = dy1
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.ShapeRange.TextFrame.MarginTop = 0
Selection.Font.size = 12
Selection.TypeText text:=IzmNovZam
'Selection.TypeText Text:=""
'Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="DOCVARIABLE ""izm""", PreserveFormatting:=True
dy1 = dy1 + dv2
ActiveDocument.Shapes.AddTextbox(msoTextOrientationDownward, dx1, dy1, dh1, dv3, PageRng).Select
Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionPage
Selection.ShapeRange.Left = dx1
Selection.ShapeRange.Top = dy1
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.ShapeRange.TextFrame.MarginTop = 0
Selection.Font.size = 12
Selection.TypeText text:=""
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, text:="DOCVARIABLE ""NomIzv""", PreserveFormatting:=True
Case Else
rng.Select
Exit Sub
End Select
End If 'shp.TextFrame.TextRange.Information(wdActiveEndSectionNumber) = SectionNmb
End If 'Left(shp.TextFrame.TextRange.Text, 7) = "Изм"
End If 'shp.Type = msoTextBox
Next Shp
Next i
rng.Select
Loop While pgs <> ""
End Sub