N
Neha Gupta
hi,
We have created a macro function to add a row in the existing table
dynamically on the click of a button named 'Add Row'. This function works
when I open the dot template and click on 'Add Row' button. Strangly, when I
create a new document based on this dot template, this macro didn't get
copied into the target new document and works only for the first time. The
custom function will I have created is as shown below:
***************************************************
Private Sub AddRepeating_Click()
ActiveDocument.Unprotect Password:=""
Dim oWord As Word.Application
Dim oRange As Range
Selection.GoTo What:=wdGoToBookmark, Name:="\endofdoc"
ActiveDocument.Tables(3).Rows.Add
tcount = ActiveDocument.Tables(3).Rows.Count
Set oRange = ActiveDocument.Tables(3).Rows(tcount).Cells(1).Range
Set oRange = ActiveDocument.Tables(3).Rows(tcount).Cells(1).Range
oRange.Select
Selection.Style = ActiveDocument.Styles("Links")
Set oRange = ActiveDocument.Tables(3).Rows(tcount).Cells(2).Range
oRange.Select
ProcName = "CmdDeleteLink" & tcount
i = ProcedureExists(ProcName & "_Click", "ThisDocument")
If i = True Then
Do Until i = False
tcount = tcount + 1
ProcName = "CmdDeleteLink" & tcount
i = ProcedureExists(ProcName & "_Click", "ThisDocument")
Loop
End If
Dim shp As Word.InlineShape
Set doc = ThisDocument
Set shp =
doc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1",
Range:=oRange)
shp.OLEFormat.Object.Caption = "Delete"
shp.OLEFormat.Object.Name = ProcName
shp.OLEFormat.Object.BackColor = &HC0C0C0
shp.Height = 20
shp.Width = 44.8
Dim sCode As String
sCode = "Private Sub " & shp.OLEFormat.Object.Name & "_Click()" & vbCrLf
& _
"ActiveDocument.Unprotect Password:=""""" & vbCrLf & _
"ProcedureName =""" & shp.OLEFormat.Object.Name & "_Click""" & vbCrLf & _
"Dim oRange As Range" & vbCrLf & _
"Me." & shp.OLEFormat.Object.Name & ".Select" & vbCrLf & _
"Selection.Range.Rows.Delete" & vbCrLf & _
"DeleteCode ProcedureName" & vbCrLf & _
"End Sub"
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True
ActiveDocument.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString sCode
End Sub
Private Sub DeleteCode(ProcedureName)
Dim VBCodeMod As CodeModule
Dim StartLine As Long
Dim HowManyLines As Long
Set VBCodeMod =
ActiveDocument.VBProject.VBComponents("ThisDocument").CodeModule
With VBCodeMod
StartLine = .ProcStartLine(ProcedureName, vbext_pk_Proc)
HowManyLines = .ProcCountLines(ProcedureName, vbext_pk_Proc)
..DeleteLines StartLine, HowManyLines
End With
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True
End Sub
*****************************************************
Please suggest me how to resolve this issue. Thanks in Advance for your help.
Thanks,Neha
We have created a macro function to add a row in the existing table
dynamically on the click of a button named 'Add Row'. This function works
when I open the dot template and click on 'Add Row' button. Strangly, when I
create a new document based on this dot template, this macro didn't get
copied into the target new document and works only for the first time. The
custom function will I have created is as shown below:
***************************************************
Private Sub AddRepeating_Click()
ActiveDocument.Unprotect Password:=""
Dim oWord As Word.Application
Dim oRange As Range
Selection.GoTo What:=wdGoToBookmark, Name:="\endofdoc"
ActiveDocument.Tables(3).Rows.Add
tcount = ActiveDocument.Tables(3).Rows.Count
Set oRange = ActiveDocument.Tables(3).Rows(tcount).Cells(1).Range
Set oRange = ActiveDocument.Tables(3).Rows(tcount).Cells(1).Range
oRange.Select
Selection.Style = ActiveDocument.Styles("Links")
Set oRange = ActiveDocument.Tables(3).Rows(tcount).Cells(2).Range
oRange.Select
ProcName = "CmdDeleteLink" & tcount
i = ProcedureExists(ProcName & "_Click", "ThisDocument")
If i = True Then
Do Until i = False
tcount = tcount + 1
ProcName = "CmdDeleteLink" & tcount
i = ProcedureExists(ProcName & "_Click", "ThisDocument")
Loop
End If
Dim shp As Word.InlineShape
Set doc = ThisDocument
Set shp =
doc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1",
Range:=oRange)
shp.OLEFormat.Object.Caption = "Delete"
shp.OLEFormat.Object.Name = ProcName
shp.OLEFormat.Object.BackColor = &HC0C0C0
shp.Height = 20
shp.Width = 44.8
Dim sCode As String
sCode = "Private Sub " & shp.OLEFormat.Object.Name & "_Click()" & vbCrLf
& _
"ActiveDocument.Unprotect Password:=""""" & vbCrLf & _
"ProcedureName =""" & shp.OLEFormat.Object.Name & "_Click""" & vbCrLf & _
"Dim oRange As Range" & vbCrLf & _
"Me." & shp.OLEFormat.Object.Name & ".Select" & vbCrLf & _
"Selection.Range.Rows.Delete" & vbCrLf & _
"DeleteCode ProcedureName" & vbCrLf & _
"End Sub"
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True
ActiveDocument.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString sCode
End Sub
Private Sub DeleteCode(ProcedureName)
Dim VBCodeMod As CodeModule
Dim StartLine As Long
Dim HowManyLines As Long
Set VBCodeMod =
ActiveDocument.VBProject.VBComponents("ThisDocument").CodeModule
With VBCodeMod
StartLine = .ProcStartLine(ProcedureName, vbext_pk_Proc)
HowManyLines = .ProcCountLines(ProcedureName, vbext_pk_Proc)
..DeleteLines StartLine, HowManyLines
End With
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True
End Sub
*****************************************************
Please suggest me how to resolve this issue. Thanks in Advance for your help.
Thanks,Neha