Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-08-2018, 11:41 PM
Zhibek Zhibek is offline drop-out the table items using the vba in word Windows 10 drop-out the table items using the vba in word Office 2013
Advanced Beginner
drop-out the table items using the vba in word
 
Join Date: Aug 2018
Location: Almaty, Kazakhstan
Posts: 30
Zhibek is on a distinguished road
Default drop-out the table items using the vba in word

When user will be click to check box near the "Spouse of employee" (I attached the screen of table) the list of rows with items must appear, without user's clicking it must be closed.



How can I run such macros?
Attached Images
File Type: png A.png (54.2 KB, 33 views)
Reply With Quote
  #2  
Old 08-09-2018, 03:06 AM
gmayor's Avatar
gmayor gmayor is offline drop-out the table items using the vba in word Windows 10 drop-out the table items using the vba in word Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Frankly I wouldn't do it like this personally - better to create a userform to make the selections and create the document based on the selections, but you can do it using content controls - see attached.


See also https://www.msofficeforums.com/word-...-checkbox.html
Attached Files
File Type: docm Employee.docm (47.2 KB, 19 views)
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 08-09-2018, 10:09 PM
Zhibek Zhibek is offline drop-out the table items using the vba in word Windows 10 drop-out the table items using the vba in word Office 2013
Advanced Beginner
drop-out the table items using the vba in word
 
Join Date: Aug 2018
Location: Almaty, Kazakhstan
Posts: 30
Zhibek is on a distinguished road
Default

Thank you for answer
Reply With Quote
  #4  
Old 08-09-2018, 11:41 PM
Zhibek Zhibek is offline drop-out the table items using the vba in word Windows 10 drop-out the table items using the vba in word Office 2013
Advanced Beginner
drop-out the table items using the vba in word
 
Join Date: Aug 2018
Location: Almaty, Kazakhstan
Posts: 30
Zhibek is on a distinguished road
Default

Where in this macros I need change the line or add new function that list items must be different in each case?
Because now it copies list items from first table(employee)
Code:
Option Explicit
Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
Dim oTable As Table
Dim oRng As Range
Dim occ As ContentControl
Dim i As Integer
    'Spouse
    Select Case ContentControl.Title
        Case "Spouse"
            Set oRng = ContentControl.Range.Tables(1).Range
            For i = 1 To ActiveDocument.Tables.Count
                If oRng.InRange(ActiveDocument.Tables(i).Range) Then
                    Exit For
                End If
            Next i
            If ContentControl.Checked = True Then
                MsgBox "Выбрать супруга(-у)"
                If Not ActiveDocument.Tables(i + 1).Rows.Count > 1 Then
                    Set oRng = ActiveDocument.Tables(i).Range
                    oRng.Collapse 0
                    oRng.Text = vbCr
                    oRng.Collapse 0
                    oRng.FormattedText = ActiveDocument.Tables(2).Range.FormattedText
                End If
            Else
                MsgBox "Удалить супруга(-у)"
                Set oTable = ActiveDocument.Tables(i + 1)
                If oTable.Rows.Count > 1 Then
                    For Each occ In oTable.Range.ContentControls
                        occ.LockContentControl = False
                    Next occ
                    Set oRng = oTable.Range
                    oRng.Start = oRng.Start - 1
                    oRng.End = oRng.Paragraphs(1).Range.End
                    oTable.Delete
                    oRng.Delete
                End If
            End If
            Selection.HomeKey wdStory
'Child 1
        Case "Child1"
            Set oRng = ContentControl.Range.Tables(1).Range
            For i = 1 To ActiveDocument.Tables.Count
                If oRng.InRange(ActiveDocument.Tables(i).Range) Then
                    Exit For
                End If
            Next i
            If ContentControl.Checked = True Then
                MsgBox "Adding child section"
                If oRng.InRange(ActiveDocument.Tables(ActiveDocument.Tables.Count).Range) Then
                    Set oRng = ActiveDocument.Tables(i).Range
                    oRng.Collapse 0
                    oRng.Text = vbCr
                    oRng.Collapse 0
                    oRng.FormattedText = ActiveDocument.Tables(2).Range.FormattedText
                End If
            Else
                If Not oRng.InRange(ActiveDocument.Tables(ActiveDocument.Tables.Count).Range) Then
                    MsgBox "Removing child section"
                    Set oTable = ActiveDocument.Tables(i + 1)
                    If oTable.Rows.Count > 1 Then
                        For Each occ In oTable.Range.ContentControls
                            occ.LockContentControl = False
                        Next occ
                        Set oRng = oTable.Range
                        oRng.Start = oRng.Start - 1
                        oRng.End = oRng.Paragraphs(1).Range.End
                        oTable.Delete
                        oRng.Delete
                    End If
                End If
            End If
            Selection.HomeKey wdStory
'Child 2
Case "Child2"
            Set oRng = ContentControl.Range.Tables(1).Range
            For i = 1 To ActiveDocument.Tables.Count
                If oRng.InRange(ActiveDocument.Tables(i).Range) Then
                    Exit For
                End If
            Next i
            If ContentControl.Checked = True Then
                MsgBox "Adding child section"
                If oRng.InRange(ActiveDocument.Tables(ActiveDocument.Tables.Count).Range) Then
                    Set oRng = ActiveDocument.Tables(i).Range
                    oRng.Collapse 0
                    oRng.Text = vbCr
                    oRng.Collapse 0
                    oRng.FormattedText = ActiveDocument.Tables(2).Range.FormattedText
                End If
            Else
                If Not oRng.InRange(ActiveDocument.Tables(ActiveDocument.Tables.Count).Range) Then
                    MsgBox "Removing child section"
                    Set oTable = ActiveDocument.Tables(i + 1)
                    If oTable.Rows.Count > 1 Then
                        For Each occ In oTable.Range.ContentControls
                            occ.LockContentControl = False
                        Next occ
                        Set oRng = oTable.Range
                        oRng.Start = oRng.Start - 1
                        oRng.End = oRng.Paragraphs(1).Range.End
                        oTable.Delete
                        oRng.Delete
                    End If
                End If
            End If
            Selection.HomeKey wdStory
'Child 3
Case "Child1"
            Set oRng = ContentControl.Range.Tables(1).Range
            For i = 1 To ActiveDocument.Tables.Count
                If oRng.InRange(ActiveDocument.Tables(i).Range) Then
                    Exit For
                End If
            Next i
            If ContentControl.Checked = True Then
                MsgBox "Adding child section"
                If oRng.InRange(ActiveDocument.Tables(ActiveDocument.Tables.Count).Range) Then
                    Set oRng = ActiveDocument.Tables(i).Range
                    oRng.Collapse 0
                    oRng.Text = vbCr
                    oRng.Collapse 0
                    oRng.FormattedText = ActiveDocument.Tables(2).Range.FormattedText
                End If
            Else
                If Not oRng.InRange(ActiveDocument.Tables(ActiveDocument.Tables.Count).Range) Then
                    MsgBox "Removing child section"
                    Set oTable = ActiveDocument.Tables(i + 1)
                    If oTable.Rows.Count > 1 Then
                        For Each occ In oTable.Range.ContentControls
                            occ.LockContentControl = False
                        Next occ
                        Set oRng = oTable.Range
                        oRng.Start = oRng.Start - 1
                        oRng.End = oRng.Paragraphs(1).Range.End
                        oTable.Delete
                        oRng.Delete
                    End If
                End If
            End If
            Selection.HomeKey wdStory
'Child 4
Case "Child1"
            Set oRng = ContentControl.Range.Tables(1).Range
            For i = 1 To ActiveDocument.Tables.Count
                If oRng.InRange(ActiveDocument.Tables(i).Range) Then
                    Exit For
                End If
            Next i
            If ContentControl.Checked = True Then
                MsgBox "Adding child section"
                If oRng.InRange(ActiveDocument.Tables(ActiveDocument.Tables.Count).Range) Then
                    Set oRng = ActiveDocument.Tables(i).Range
                    oRng.Collapse 0
                    oRng.Text = vbCr
                    oRng.Collapse 0
                    oRng.FormattedText = ActiveDocument.Tables(2).Range.FormattedText
                End If
            Else
                If Not oRng.InRange(ActiveDocument.Tables(ActiveDocument.Tables.Count).Range) Then
                    MsgBox "Removing child section"
                    Set oTable = ActiveDocument.Tables(i + 1)
                    If oTable.Rows.Count > 1 Then
                        For Each occ In oTable.Range.ContentControls
                            occ.LockContentControl = False
                        Next occ
                        Set oRng = oTable.Range
                        oRng.Start = oRng.Start - 1
                        oRng.End = oRng.Paragraphs(1).Range.End
                        oTable.Delete
                        oRng.Delete
                    End If
                End If
            End If
            Selection.HomeKey wdStory
    End Select
End Sub
Also, that message which appears in MsgBox I need it in Russian language,
but I changed it to Russian letters and finally in MsgBox shows smth not readable,

What should I do?)

Thank you!
Reply With Quote
  #5  
Old 08-12-2018, 08:02 PM
Zhibek Zhibek is offline drop-out the table items using the vba in word Windows 10 drop-out the table items using the vba in word Office 2013
Advanced Beginner
drop-out the table items using the vba in word
 
Join Date: Aug 2018
Location: Almaty, Kazakhstan
Posts: 30
Zhibek is on a distinguished road
Default Drop Down function with help of check box. Need different value of list items

In this code when I click check box button it shows list of items. For example in "Spouse of employee" table it shows( First Name; Last Name; Phone number and etc). And even if I want to add different items for each tables after updating page all items take a value of first table "Employee". But I do not need it.

I need only drop down function with help of check box, it runs perfect. But the value of each tables must be different, I will type value.( for example, in "Spouse of Employee" list of items must be: First name of Spouse; Last Name of Spouse and etc. and it the value must be different, now it just copy value from 'Employee table")

Where in this code do I need change function and which Function?
Thank you in advance.!
Attached Files
File Type: docm Employee.docm (47.2 KB, 12 views)
Reply With Quote
  #6  
Old 08-12-2018, 10:44 PM
Zhibek Zhibek is offline drop-out the table items using the vba in word Windows 10 drop-out the table items using the vba in word Office 2013
Advanced Beginner
drop-out the table items using the vba in word
 
Join Date: Aug 2018
Location: Almaty, Kazakhstan
Posts: 30
Zhibek is on a distinguished road
Default

Can Somebody help me please??
Gmayor?
Reply With Quote
  #7  
Old 08-12-2018, 10:49 PM
macropod's Avatar
macropod macropod is offline drop-out the table items using the vba in word Windows 7 64bit drop-out the table items using the vba in word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Dim Tbl As Table, Rng As Range, i As Long
Dim oCCtrl As ContentControl
With CCtrl
  i = ActiveDocument.Range(0, .Range.Tables(1).Range.End).Tables.Count
  Select Case .Title
    Case "Spouse"
      If .Checked = True Then
        MsgBox "Adding spouse section"
        Set Rng = .Range.Tables(1).Range
        Call AddGroup(Rng, i)
      Else
        MsgBox "Removing spouse section"
        Set Rng = ActiveDocument.Tables(i + 1).Range
        Call RemoveGroup(Rng)
      End If
    Case "Child1"
      If .Checked = True Then
        MsgBox "Adding child section"
        Set Rng = .Range.Tables(1).Range
        Call AddGroup(Rng, i)
      Else
        MsgBox "Removing child section"
        Set Rng = ActiveDocument.Tables(i + 1).Range
        Call RemoveGroup(Rng)
      End If
    End Select
End With
End Sub

Sub AddGroup(Rng As Range, i As Long)
Dim CCtrl As ContentControl, t As Long, bAdd As Boolean
bAdd = False
If (i = ActiveDocument.Tables.Count) Then bAdd = True
If bAdd = False Then
  If ActiveDocument.Tables(i + 1).Rows.Count = 1 Then bAdd = True
End If
If bAdd = True Then
  With Rng
    .Collapse 0
    .Text = vbCr
    .Collapse 0
    .FormattedText = ActiveDocument.Tables(2).Range.FormattedText
    For Each CCtrl In .ContentControls
      With CCtrl
        t = .Type
        .Type = wdContentControlText
        .Range.Text = ""
        .Type = t
      End With
    Next
  End With
End If
End Sub

Sub RemoveGroup(Rng As Range)
Dim CCtrl As ContentControl
With Rng
  If .Tables(1).Rows.Count > 1 Then
    For Each CCtrl In .ContentControls
      CCtrl.LockContentControl = False
    Next
    .End = .End + 1
    .Delete
  End If
End With
End Sub
Note that I've employed a ContentControlOnExit macro - which makes more sense IMHO.

I'll leave it to you to figure out what to do for those with more than one spouse and/or child...

PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 08-12-2018, 10:50 PM
macropod's Avatar
macropod macropod is offline drop-out the table items using the vba in word Windows 7 64bit drop-out the table items using the vba in word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Zhibek View Post
Can Somebody help me please??
How about some patience on your part... You're not paying anyone here to do your coding!
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 08-12-2018, 11:53 PM
Zhibek Zhibek is offline drop-out the table items using the vba in word Windows 10 drop-out the table items using the vba in word Office 2013
Advanced Beginner
drop-out the table items using the vba in word
 
Join Date: Aug 2018
Location: Almaty, Kazakhstan
Posts: 30
Zhibek is on a distinguished road
Default

Thank you for answer.
How can I run it?
I replaced my old code for this one. but it doesn't work
Reply With Quote
  #10  
Old 08-12-2018, 11:55 PM
Zhibek Zhibek is offline drop-out the table items using the vba in word Windows 10 drop-out the table items using the vba in word Office 2013
Advanced Beginner
drop-out the table items using the vba in word
 
Join Date: Aug 2018
Location: Almaty, Kazakhstan
Posts: 30
Zhibek is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
How about some patience on your part... You're not paying anyone here to do your coding!
What's wrong with my patience ?
I just forget to write it one my first message and add it separately.
Reply With Quote
  #11  
Old 08-13-2018, 01:01 AM
macropod's Avatar
macropod macropod is offline drop-out the table items using the vba in word Windows 7 64bit drop-out the table items using the vba in word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Zhibek View Post
I replaced my old code for this one. but it doesn't work
The code works just fine - I actually developed and tested it in your document...

Quote:
Originally Posted by Zhibek View Post
What's wrong with my patience ?
What's wrong??? You ask a question, then less than two hours later bump the post with a "Can Somebody help me please??". That's what's wrong. Keep up the behaviour you've exhibited so far and you'll find no-one here willing to help you.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
drop-out the table items using the vba in word Counting items in the table digiart Word VBA 1 08-25-2017 10:17 PM
Lock items in a table village Word 2 06-22-2017 07:33 AM
Select multiple items from a drop down list ConfuddledOne Excel Programming 0 01-30-2017 08:13 PM
drop-out the table items using the vba in word How can I style individual items of a drop down or combobox? supremegrandruler Word 1 12-09-2014 03:17 PM
Drag and Drop Outlook Items to Word Mike23 Outlook 0 06-14-2010 03:51 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:48 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft