View Single Post
 
Old 12-23-2022, 10:55 AM
CSEShop CSEShop is offline Windows 10 Office 2013
Novice
 
Join Date: Dec 2022
Posts: 2
CSEShop is on a distinguished road
Default Using a dropdown box to fill out multiple text fields and insert a bb image

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.
Reply With Quote