![]() |
#1
|
|||
|
|||
![]()
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. |
#2
|
||||
|
||||
![]()
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 |
#3
|
|||
|
|||
![]()
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:
|
#4
|
||||
|
||||
![]()
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 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
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
![]()
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 |
#6
|
||||
|
||||
![]()
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 |
#7
|
|||
|
|||
![]()
Thank you guys! Figured out the document property part.
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
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 |