Results 1 to 2 of 2
2014-01-09, 03:24 #1
- Join Date
- Jan 2014
- Thanked 0 Times in 0 Posts
how to insert data in Word Template using userform
hi to all
am new to VBA in word, i had create user-form in word for data inserting. when i submit the data, the data is not overwriting , due to this my word alignment changed. how to over come this?
Here is my code
Private Sub CommandButton1_Click() Dim Name As Range Set Name = ActiveDocument.Bookmarks("GreetName").Range Name.Text = Me.Gname.Value Dim Address As Range Set Address = ActiveDocument.Bookmarks("ToAdd").Range Address.Text = Me.Taddress.Value Dim dt As Range Set dt = ActiveDocument.Bookmarks("Date").Range dt.Text = Me.TDate.Value Dim Ref As Range Set Ref = ActiveDocument.Bookmarks("Ref").Range Ref.Text = Me.TRef.Value Dim Posi As Range Set Posi = ActiveDocument.Bookmarks("Position").Range Posi.Text = Me.CPosition.Value Dim CTCF As Range Set CTCF = ActiveDocument.Bookmarks("CTCfig").Range CTCF.Text = Me.CTCFig.Value Dim CTCW As Range Set CTCW = ActiveDocument.Bookmarks("CTCText").Range CTCW.Text = Me.CTCWord.Value End Sub
Last edited by RetiredGeek; 2014-01-09 at 04:47.
2014-01-09, 05:17 #2
- Join Date
- Mar 2004
- Manning, South Carolina
- Thanked 1,454 Times in 1,323 Posts
Welcome to the Lounge as a new poster.
You can control the Overtype/Insert option via:
Options.Overtype = True (Turn On - Overtype) or False (Turn Off - Insert)
In your code I would advise:
Private Sub CommandButton1_Click() Dim bCurrentOTMode as Boolean bCurrentOTMode = Options.Overtype Options.Overtype = False 'Turn Insert On '*** Note: I took the liberty of simplifying the code here *** With ActiveDocument .Bookmarks("GreetName").Range.Text = Me.Gname.Value .Bookmarks("ToAdd").Range.Text = Me.Taddress.Value .Bookmarks("Date").RangeText = Me.TDate.Value .Bookmarks("Ref").RangeText = Me.TRef.Value .Bookmarks("Position").Rangei.Text = Me.CPosition.Value .Bookmarks("CTCfig").Range.Text = Me.CTCFig.Value .Bookmarks("CTCText").Range.Text = Me.CTCWord.Value End With 'ActiveDocument Options.Overtype = bCurrentOTMode 'Return to original mode End Sub