![]() |
#1
|
|||
|
|||
![]()
Hi Forum,
I need your help. I have got a ComboBox with 2 entries “conforms” and “not tested” and die Values are Russian words. The goal is to select the words in English and get the Russian words as output. Code at the moment: Code:
PrivateSubDocument_ContentControlOnExit(ByValContentControl AsContentControl, Cancel AsBoolean) Dimrng AsRange Dimbm AsBookmark Dimbmt AsBookmark If(ContentControl.Type = wdContentControlComboBox) Then Setrng = ContentControl.Range Setbm = rng.Bookmarks(1) Setbmt = ActiveDocument.Bookmarks(bm & "tgt") ElseIf(ContentControl.Type = wdContentControlText) Then EndIf EndSub Bmt stands for the Combobox/Textfield – Textmark. Now I got 2 problems. On the one hand, I believe that I have to solve that problem by using a second ComboBox, cause I can’t work with Russian letters in VBA. On the other hand: How can I get the Indexchoice from the first ComboBox in a second one? Look at the image for more information. ![]() |
#2
|
|||
|
|||
![]()
If I select the first choice .. Test2 should automatically choose the same.
As you've probably seen, there is no built-in change event for content controls so "automatically" is really not possible. You have to select the first choice then "exit." There are several ways to work around this. But, why does the 2nd CC need to be a dropdown? If you select the second choice in test 1, all you really want is for Test2 to display "COOT" Correct? One method is to map the first CC to a CustomXMLPart and then use one of the built-in CustomXMLPart events: Here is how: In a new blank document. Add a standard code module and paste in this code: Code:
Option Explicit Sub AddCCsAndMap() Dim oCC As ContentControl Dim oRng As Word.Range With ActiveDocument .Range.Text = "Select English Term: " Set oRng = .Range oRng.Collapse wdCollapseEnd Set oCC = .ContentControls.Add(Type:=wdContentControlDropdownList, Range:=oRng) '.Range) 'Selection.Range) With oCC .Title = "Master" With .DropdownListEntries .Add "Chose an item.", "Nothing selected." .Add "A" .Add "B" End With End With oRng.Start = .Range.End - 1 oRng.InsertBefore vbCr oRng.Start = .Range.End - 1 oRng.Text = "Russian Equivalent: " oRng.Start = .Range.End - 1 Set oCC = .ContentControls.Add(wdContentControlText, oRng) With oCC .Title = "Slave" .LockContents = True .SetPlaceholderText , , "Nothing chosen." End With .Range.InsertAfter vbCr + vbCr & "There is an apparent ""bug"" or at least a design flaw in the NodeAfterReplace event." & vbCr _ & "If the Content Control XML mapping node value is null (i.e., Content control displaying normal Placeholder text) the event is not triggered when" _ & " the null value is replaced with a non-null value (i.e., a Content Control item is selected). Additionally the event is not triggered" _ & " when a non-null value is replaced with a null value. The event is only triggered when a non-null value is replaced with another non-null value." & vbCr & vbCr _ & "The work around is to use and store a non-null value in the XML node that mimics placeholder text. CC.Range" _ & " formatting attributes are used to make the imposter text appear as placeholder text in the content control." End With MapCCs AutoOpen End Sub Sub MapCCs() Dim oCC As ContentControl Dim strXML As String Dim oCustXMLPart As CustomXMLPart strXML = "<?xml version='1.0' encoding='utf-8'?><Root><Master> </Master></Root>" ClearXMLParts Set oCustXMLPart = ActiveDocument.CustomXMLParts.Add(strXML) Set oCC = ActiveDocument.SelectContentControlsByTitle("Master").Item(1) oCC.XMLMapping.SetMapping "/Root/Master[1]" Set oCustXMLPart = Nothing AutoOpen oCC.DropdownListEntries(2).Select oCC.DropdownListEntries(1).Select Set oCC = Nothing End Sub Sub AutoOpen() ThisDocument.InitializeMonitor End Sub Sub ClearXMLParts() Dim i As Long For i = ActiveDocument.CustomXMLParts.Count To 4 Step -1 ActiveDocument.CustomXMLParts(i).Delete Next i End Sub Code:
Option Explicit Dim WithEvents oMonitor As CustomXMLPart Sub InitializeMonitor() On Error Resume Next Set oMonitor = ThisDocument.CustomXMLParts(4) End Sub Private Sub oMonitor_NodeAfterReplace(ByVal OldNode As Office.CustomXMLNode, ByVal NewNode As Office.CustomXMLNode, ByVal InUndoRedo As Boolean) Select Case NewNode.ParentNode.BaseName Case "Master" ActiveDocument.SelectContentControlsByTitle("Master").Item(1).Range.Font.Color = wdColorAutomatic ActiveDocument.SelectContentControlsByTitle("Slave").Item(1).LockContents = False Select Case oMonitor.SelectSingleNode("/Root/Master[1]").Text Case "Nothing selected." ActiveDocument.SelectContentControlsByTitle("Master").Item(1).Range.Font.Color = wdColorGray45 With ActiveDocument.ContentControls(2).Range .Text = "Nothing chosen." .Font.Color = wdColorGray45 End With Case "A" With ActiveDocument.ContentControls(2).Range .Text = "Your ""A"" Russian term here." .Font.Color = wdColorGreen End With Case "B" With ActiveDocument.ContentControls(2).Range .Text = "Your ""B"" Russian term here." .Font.Color = wdColorBlue End With End Select ActiveDocument.SelectContentControlsByTitle("Slave").Item(1).LockContents = True End Select End Sub AddCCsAndMap |
#3
|
|||
|
|||
![]()
Thanks for your fast answer. Your solution looks nice, but the problem is, that i can't use russian letters in my VBA Makro. Therefore I can't display the whole word correctly.
![]() |
#4
|
|||
|
|||
![]()
You might replace:
.Text = "Your ""A"" Russian term here." With a function to go out and get that text from a table cell in a reference word document, or a spreadsheet. e.g., .Text = oRefDoc.Tables(1).Cell(1,3).Range.Text |
#5
|
|||
|
|||
![]()
As I always try not to use linked documents, I use my quick workaround for short terms:
![]() Type the needed term in Word, select it and then call the following function. Code:
Function GetUnicodeString(Optional UniCode As String) As String Dim oData As New DataObject 'Link to Microsoft Forms 2.0 Object Library Dim i% If UniCode = "" Then UniCode = Selection For i = 1 To Len(UniCode) GetUnicodeString = GetUnicodeString & "ChrW(" & (AscW(Mid(UniCode, i, i))) & ") & " Next GetUnicodeString = Left(GetUnicodeString, Len(GetUnicodeString) - 3) With oData .SetText GetUnicodeString .PutInClipboard End With MsgBox "String in clipboard for use in VBA ... ", vbOKOnly + vbInformation, "In Clipboard" End Function Only suitable for short strings in Userforms or ListBoxes, etc. NP |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
combobox issue | Deepa Shrestha | Word | 0 | 07-29-2013 09:47 PM |
Typing in Russian | shekala | Outlook | 0 | 11-12-2012 05:57 PM |
ComboBox ListIndex = -1 even though it does NOT = -1 | Joe Patrick | Word VBA | 0 | 08-03-2011 08:34 AM |
![]() |
ilkks | Word VBA | 7 | 05-25-2011 04:06 AM |
![]() |
vsempoux | Word VBA | 3 | 10-31-2009 08:58 AM |