Results 1 to 2 of 2
  1. #1
    New Lounger
    Join Date
    Jan 2014
    Posts
    1
    Thanks
    0
    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
    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
    Thanks for advance
    Last edited by RetiredGeek; 2014-01-09 at 05:47.

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,378
    Thanks
    207
    Thanked 829 Times in 762 Posts
    Sateesh,

    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:
    Code:
    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
    HTH
    Last edited by RetiredGeek; 2014-01-09 at 06:19.
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •