![]() |
|
![]() |
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
![]()
Hi, I am trying to create a form which has a single dropdown box that fills out multiple text fields and inserts an image all from 1 drop down box.
I am very unfamiliar with VBA, but I have been reading a lot of posts here and I've found two chunks of code that work in two separate documents how I would like them to (one fills out the text, the other inserts an image from a building block), but I cannot seem to figure out how to combine them in one document without breaking everything. The first method I would like to use for adding the text fields is from this post and works exactly how I would perfer.: https://www.msofficeforums.com/word-...html#post46903 Code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Application.ScreenUpdating = False Dim i As Long, StrDetails As String With ContentControl If .Title = "Client" Then For i = 1 To .DropdownListEntries.Count If .DropdownListEntries(i).Text = .Range.Text Then StrDetails = .DropdownListEntries(i).Value Exit For End If Next If StrDetails = "" Then StrDetails = "||" With ActiveDocument.SelectContentControlsByTitle("Title")(1) .LockContents = False .Range.Text = Split(StrDetails, "|")(0) .LockContents = True End With With ActiveDocument.SelectContentControlsByTitle("Corporation of Firm Nme")(1) .LockContents = False .Range.Text = Split(StrDetails, "|")(1) .LockContents = True End With With ActiveDocument.SelectContentControlsByTitle("Address")(1) .LockContents = False .Range.Text = Split(StrDetails, "|")(2) .LockContents = True End With End If End With Application.ScreenUpdating = True End Sub The second part I want to combine to work with the first chunk of code is from this post, I know that this post it talking about text building blocks, but I played with it and can reference image building blocks and it seems to work fine : https://www.msofficeforums.com/word-...dent-drop.html I'd also like to change the cc name from conditional content to signature, but that seems to break it for some reason Code:
Option Explicit Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Select Case ContentControl.Title Case "Client" Select Case ContentControl.Range.Text Case "A": InsertBB_atRTCC_Range "Conditional Content", "Signature1" Case "B": InsertBB_atRTCC_Range "Conditional Content", "Signature2" '...so on. Case Else: InsertBB_atRTCC_Range "Conditional Content": End Select End Select lbl_Exit: Exit Sub End Sub Sub InsertBB_atRTCC_Range(CCTitle As String, Optional BBName As String = vbNullString) Dim oTmp As Template Dim oRng As Range Dim oTbl As Table Set oTmp = ThisDocument.AttachedTemplate Set oRng = ActiveDocument.SelectContentControlsByTitle(CCTitle).Item(1).Range 'Tables in range and be problamatic so delete them. If oRng.Tables.Count > 0 Then For Each oTbl In oRng.Tables oTbl.Delete Next oTbl Set oRng = ActiveDocument.SelectContentControlsByTitle(CCTitle).Item(1).Range End If If Not BBName = vbNullString Then oTmp.BuildingBlockEntries(BBName).Insert oRng, True Else oRng.Text = vbNullString End If lbl_Exit: Exit Sub End Sub Sub InsertBB_atBookmarkRange_Range(BMName As String, Optional BBName As String) Dim oTmp As Template Dim oRng As Range Dim oTbl As Table Set oTmp = ThisDocument.AttachedTemplate Set oRng = ActiveDocument.Bookmarks(BMName).Range 'Tables in range and be problamatic so delete them. If oRng.Tables.Count > 0 Then For Each oTbl In oRng.Tables oTbl.Delete Next oTbl End If If Not BBName = vbNullString Then Set oRng = oTmp.BuildingBlockEntries(BBName).Insert(oRng, True) Else oRng.Text = vbNullString End If ActiveDocument.Bookmarks.Add BMName, oRng lbl_Exit: Exit Sub End Sub So I would like to have 1 dropdown box called client that when a client is selected it inputs text in the same way that the first chunk of code does, and ALSO inserts an image (signature) from a building block like the second chunk does. I can't figure out how to do both at once without breaking things and would really appreciate some help. |
#2
|
||||
|
||||
![]()
The attached should work, however I would suggest reading the signatures from disc rather than from building blocks as it makes them much easier to maintain. Put all the files in the same folder and create a new document from the template. The list is easily maintained using Insert Content Control Add-In and while this example could follow your proposal and have numbered signatures that reflect the list position of the selected name, it would be better if you named the signatures to match the listed names as in the attached.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
![]()
Thank you so much! This is exactly what I was hoping to achieve, and you are right, storing the signatures like that is a much easier and better solution. I just tested this out and it works exactly as I was hoping to be able to do it. I was struggling quite a bit with this before posting and this was a great solution.
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
strodden | Word | 2 | 08-28-2017 07:01 AM |
![]() |
vera | Word VBA | 1 | 10-07-2016 07:41 PM |
![]() |
Nukedaddy | Word | 3 | 09-16-2016 06:50 AM |
![]() |
Blades2002 | Word | 2 | 08-31-2015 11:20 AM |
Using Fill In Fields to Autopopulate Multiple Areas in the Document | rogelinepaula | Word | 1 | 08-13-2015 11:15 PM |