Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-17-2017, 12:39 AM
darkfaith darkfaith is offline Combining dropdown list VBA and data repeat VBA Windows 8 Combining dropdown list VBA and data repeat VBA Office 2013
Novice
Combining dropdown list VBA and data repeat VBA
 
Join Date: Jul 2016
Posts: 4
darkfaith is on a distinguished road
Default Combining dropdown list VBA and data repeat VBA

Hi, I have very limited knowledge on VBA and I got these codes from somewhere online. I can get these codes to work but only in separate word documents. Basically I just need to combine these two codes in 1 word document (1 page).
I'm using Word 2013 if that helps.
Attached Files
File Type: docm Content Controls - Data Repeat.docm (37.7 KB, 15 views)
File Type: docm Content Controls - Dropdown Dependent Text.docm (30.4 KB, 14 views)
Reply With Quote
  #2  
Old 03-17-2017, 01:19 AM
gmayor's Avatar
gmayor gmayor is offline Combining dropdown list VBA and data repeat VBA Windows 10 Combining dropdown list VBA and data repeat VBA Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
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 ofgmayor has much to be proud of
Default

That's fairly straightforward as you have done all the work

Code:
Option Explicit

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim i As Integer
Dim strDetails As String
    With ActiveDocument
        'Use the Select Case only if employing  Custom Document Properties
        Select Case ContentControl.Title
            Case "Sbjct": .CustomDocumentProperties("Sbjct").Value = ContentControl.Range.Text
            Case "Stmnt": .CustomDocumentProperties("Stmnt").Value = ContentControl.Range.Text
            Case "Dsclr": .CustomDocumentProperties("Dsclr").Value = ContentControl.Range.Text
            Case "MjTpc": .CustomDocumentProperties("MjTpc").Value = ContentControl.Range.Text
            Case "ClientA"
                For i = 1 To ContentControl.DropdownListEntries.Count
                    If ContentControl.DropdownListEntries(i).Text = ContentControl.Range.Text Then
                        strDetails = Replace(ContentControl.DropdownListEntries(i).Value, "|", Chr(11))
                        Exit For
                    End If
                Next
                With ActiveDocument.SelectContentControlsByTitle("ClientDetailsA")(1)
                    .LockContents = False
                    .Range.Text = strDetails
                    .LockContents = True
                End With
            Case "ClientB"
                For i = 1 To ContentControl.DropdownListEntries.Count
                    If ContentControl.DropdownListEntries(i).Text = ContentControl.Range.Text Then
                        strDetails = Replace(ContentControl.DropdownListEntries(i).Value, "|", Chr(11))
                        Exit For
                    End If
                Next
                With ActiveDocument.SelectContentControlsByTitle("ClientDetailsB")(1)
                    .LockContents = False
                    .Range.Text = strDetails
                    .LockContents = True
                End With
        End Select
        .Fields.Update
    End With
End Sub
__________________
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 03-17-2017, 01:43 AM
darkfaith darkfaith is offline Combining dropdown list VBA and data repeat VBA Windows 8 Combining dropdown list VBA and data repeat VBA Office 2013
Novice
Combining dropdown list VBA and data repeat VBA
 
Join Date: Jul 2016
Posts: 4
darkfaith is on a distinguished road
Default

Thanks, Graham.
Like i said, i have very little clue about vba/scripts.
But anyway, i have copied the code you wrote but it's giving me "run-time error", Invalid procedure call or argument.
When i click debug, this was highlighted:

Quote:
Case "Sbjct": .CustomDocumentProperties("Sbjct").Value = ContentControl.Range.Text
Reply With Quote
  #4  
Old 03-17-2017, 03:40 AM
gmayor's Avatar
gmayor gmayor is offline Combining dropdown list VBA and data repeat VBA Windows 10 Combining dropdown list VBA and data repeat VBA Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
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 ofgmayor has much to be proud of
Default

The line in question writes the text from the content control to a customdocument property called "Sbjct". If that document property doesn't exist you will get an error (hence the comment two lines before this one).

Alternatively you could change the document property references to document variables (and use docvariable fields to display the variable content) e.g.
Code:
Case "Sbjct": ActiveDocument.Variables("Sbjct").Value = ContentControl.Range.Text
This doesn't require the variable to pre-exist but it will crash if the content control is empty so you need to trap that (and the default text) e.g.
Code:
Case "Sbjct"
                If Not ContentControl.Range.Text = "" _
                   And Not ContentControl.Range.Text = ContentControl.PlaceholderText Then
                    ActiveDocument.Variables("Sbjct").Value = ContentControl.Range.Text
                End If
You will of course have to treat all those documentproperty entries similarly
__________________
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
  #5  
Old 03-17-2017, 05:33 AM
gmaxey gmaxey is offline Combining dropdown list VBA and data repeat VBA Windows 7 32bit Combining dropdown list VBA and data repeat VBA Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

May I ask what is your purpose of writing data that you can see and edit directly in the form to a custom document property? If you want to repeat data you can do that with mapped CCs and not have to have code in your document at all.

http://gregmaxey.com/word_tip_pages/...rol_tools.html
Attached Files
File Type: docm Content Controls - Data Repeat II.docm (45.2 KB, 12 views)
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 03-17-2017, 08:04 AM
gmayor's Avatar
gmayor gmayor is offline Combining dropdown list VBA and data repeat VBA Windows 10 Combining dropdown list VBA and data repeat VBA Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
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 ofgmayor has much to be proud of
Default

Good point but it won't help improve his VBA skills
__________________
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
  #7  
Old 03-20-2017, 11:14 PM
darkfaith darkfaith is offline Combining dropdown list VBA and data repeat VBA Windows 8 Combining dropdown list VBA and data repeat VBA Office 2013
Novice
Combining dropdown list VBA and data repeat VBA
 
Join Date: Jul 2016
Posts: 4
darkfaith is on a distinguished road
Default

Thank you guys! Figured out the document property part.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Combining dropdown list VBA and data repeat VBA How do I make a checkbox or dropdown item reference data in that specific section of the data array? dhare Excel Programming 2 02-24-2016 12:36 PM
Help!! Dropdown List christo16 Word 1 06-29-2015 05:18 AM
combining 2 or more columns of numerical data or text data heartdoc Excel 0 12-03-2014 10:57 PM
Populate dropdown list with data from Access table spw4000 Office 0 02-24-2012 05:22 AM
Using fields to repeat data throughout a document Brasada Word 0 07-20-2010 02:37 PM

Other Forums: Access Forums

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


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