Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-14-2019, 11:55 PM
jhansrod jhansrod is offline If condition not working. Windows 10 If condition not working. Office 2016
Novice
If condition not working.
 
Join Date: Jun 2019
Posts: 16
jhansrod is on a distinguished road
Default If condition not working.

Hi



In the code below, why would OFFICEHEAD always be set to "JUST HEADER".

Code:
With ActiveDocument.SelectContentControlsByTitle("A-OFFICE")(1)
        .LockContents = False
        .Range.Text = Split(StrDetails, "|")(0)
        .LockContents = False
   Select Case .Range.Text
     Case "OFFICE01"
       With ActiveDocument.SelectContentControlsByTitle("OFFICEHEAD")(1)
         .LockContents = False
         .Range.Text = "OFFICE01 HEADER"
         .LockContents = True
       End With
     Case "OFFICE02"
       With ActiveDocument.SelectContentControlsByTitle("OFFICEHEAD")(1)
         .LockContents = False
         .Range.Text = "OFFICE02 HEADER"
         .LockContents = True
       End With
      Case Else
        With ActiveDocument.SelectContentControlsByTitle("OFFICEHEAD")(1)
          .LockContents = False
          .Range.Text = "JUST HEADER"
          .LockContents = True
        End With
   End Select
End With


Thank you
J
Reply With Quote
  #2  
Old 06-15-2019, 12:13 AM
macropod's Avatar
macropod macropod is online now If condition not working. Windows 7 64bit If condition not working. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Presumably because whatever Split(StrDetails, "|")(0) contains - and populates "A-OFFICE" with - is neither "OFFICE01" nor "OFFICE02".

Based on what you've posted, your code would be better expressed as:
Code:
With ActiveDocument.SelectContentControlsByTitle("A-OFFICE")(1)
    .LockContents = False
    .Range.Text = Split(StrDetails, "|")(0)
    .LockContents = False
End With
With ActiveDocument.SelectContentControlsByTitle("OFFICEHEAD")(1)
  .LockContents = False
  Select Case Split(StrDetails, "|")(0)
    Case "OFFICE01": .Range.Text = "OFFICE01 HEADER"
    Case "OFFICE02": .Range.Text = "OFFICE02 HEADER"
    Case Else: .Range.Text = "JUST HEADER"
  End Select
  .LockContents = True
End With
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 06-15-2019, 12:38 AM
jhansrod jhansrod is offline If condition not working. Windows 10 If condition not working. Office 2016
Novice
If condition not working.
 
Join Date: Jun 2019
Posts: 16
jhansrod is on a distinguished road
Default


I was using the value from Display Name not Value of the DropDown Content Control.

For simplicity, how would it change if I wanted to use the Display Name?

Thank you.
Reply With Quote
  #4  
Old 06-15-2019, 01:25 AM
macropod's Avatar
macropod macropod is online now If condition not working. Windows 7 64bit If condition not working. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

So how is the displayed name, which is equivalent to .Range.Text, any different from Split(StrDetails, "|")(0)? Where does StrDetails come from?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 06-15-2019, 01:43 AM
jhansrod jhansrod is offline If condition not working. Windows 10 If condition not working. Office 2016
Novice
If condition not working.
 
Join Date: Jun 2019
Posts: 16
jhansrod is on a distinguished road
Default

The dropdown list for the ContentControl has the following :
Code:
Display Name   Value
OFFICE01       OFFICENAME1|Address 1L1;Address 1L2;Address 1L3
OFFICE02       OFFICENAME2|Address 2L1;Address 2L2;Address 2L3
The DropDownList will have the relevant office code whilst Split(StrDetails, "|")(0) will have the full name.
Reply With Quote
  #6  
Old 06-15-2019, 02:51 AM
macropod's Avatar
macropod macropod is online now If condition not working. Windows 7 64bit If condition not working. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

What is the full code? So far, your descriptions and snippet lack context.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 06-15-2019, 03:22 AM
jhansrod jhansrod is offline If condition not working. Windows 10 If condition not working. Office 2016
Novice
If condition not working.
 
Join Date: Jun 2019
Posts: 16
jhansrod is on a distinguished road
Default

My apology. Here is the full code. Ideally, the use of the Office Code instead of Name is what I would like to achieve.

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 = "OFFICE" 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("A-OFFICE")(1)
      .LockContents = False
      .Range.Text = Split(StrDetails, "|")(0)
      .LockContents = False
     End With
     
     With ActiveDocument.SelectContentControlsByTitle("OFFICEHEAD")(1)
      .LockContents = False
      Select Case Split(StrDetails, "|")(0)
       Case "OFFICENAME1": .Range.Text = "OFFICENAME1 HEADER"
       Case "OFFICENAME2": .Range.Text = "OFFICENAME2 HEADER"
       Case Else: .Range.Text = ""
      End Select
      .LockContents = True
     End With
        
     With ActiveDocument.SelectContentControlsByTitle("A-FOOTER1")(1)
      .LockContents = False
      Select Case Split(StrDetails, "|")(0)
       Case "OFFICENAME1": .Range.Text = "OFFICENAME1 FOOTER1"
       Case "OFFICENAME2": .Range.Text = "OFFICENAME2 FOOTER1"
       Case Else: .Range.Text = ""
      End Select
      .LockContents = True
     End With
     
     With ActiveDocument.SelectContentControlsByTitle("A-FOOTER2")(1)
      .LockContents = False
      Select Case Split(StrDetails, "|")(0)
       Case "OFFICENAME1": .Range.Text = "OFFICENAME1 FOOTER2"
       Case "OFFICENAME2": .Range.Text = "OFFICENAME2 FOOTER2"
       Case Else: .Range.Text = ""
      End Select
      .LockContents = True
     End With
     
     With ActiveDocument.SelectContentControlsByTitle("A-FOOTER3")(1)
      .LockContents = False
      Select Case Split(StrDetails, "|")(0)
       Case "OFFICENAME1": .Range.Text = "OFFICENAME1 FOOTER3"
       Case "OFFICENAME2": .Range.Text = "OFFICENAME2 FOOTER3"
       Case Else: .Range.Text = ""
      End Select
      .LockContents = True
     End With
     
     With ActiveDocument.SelectContentControlsByTitle("A-OFFICE")(2)
      .LockContents = False
      .Range.Text = Split(StrDetails, "|")(0)
      .LockContents = True
     End With
     
     With ActiveDocument.SelectContentControlsByTitle("A-OFFICE")(3)
      .LockContents = False
      .Range.Text = Split(StrDetails, "|")(0)
      .LockContents = True
     End With
                
     With ActiveDocument.SelectContentControlsByTitle("OFFICEADDRESS")(1)
      .LockContents = False
      .Range.Text = Replace(Split(StrDetails, "|")(1), ";", Chr(11))
      .LockContents = True
     End With
       
  End If
 End With
 Application.ScreenUpdating = True
End Sub
Reply With Quote
  #8  
Old 06-15-2019, 03:45 AM
macropod's Avatar
macropod macropod is online now If condition not working. Windows 7 64bit If condition not working. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

A better way (IMHO) would be:
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
 Application.ScreenUpdating = False
 Dim i As Long, StrDetails As String, StrOffice As String, StrHdFt As String
 With CCtrl
  If .Title = "OFFICE" 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 = "||"
    StrOffice = Split(StrDetails, "|")(0)
     With ActiveDocument.SelectContentControlsByTitle("A-OFFICE")(1)
      .LockContents = False
      .Range.Text = StrOffice
      .LockContents = False
     End With
    With ActiveDocument.SelectContentControlsByTitle("A-OFFICE")(2)
      .LockContents = False
      .Range.Text = StrOffice
      .LockContents = True
    End With
    With ActiveDocument.SelectContentControlsByTitle("A-OFFICE")(3)
      .LockContents = False
      .Range.Text = StrOffice
      .LockContents = True
    End With
    With ActiveDocument.SelectContentControlsByTitle("OFFICEADDRESS")(1)
      .LockContents = False
      .Range.Text = Replace(Split(StrDetails, "|")(1), ";", Chr(11))
      .LockContents = True
    End With
    Select Case StrOffice
      Case "OFFICENAME1": StrHdFt = "OFFICENAME1 HEADER|OFFICENAME1 FOOTER1|OFFICENAME1 FOOTER2|OFFICENAME1 FOOTER3"
      Case "OFFICENAME2": StrHdFt = "OFFICENAME1 HEADER|OFFICENAME1 FOOTER1|OFFICENAME1 FOOTER2|OFFICENAME1 FOOTER3"
      Case Else: StrHdFt = "|||"
    End Select
    With ActiveDocument.SelectContentControlsByTitle("OFFICEHEAD")(1)
      .LockContents = False
      .Range.Text = Split(StrHdFt, "|")(0)
      .LockContents = True
    End With
    With ActiveDocument.SelectContentControlsByTitle("A-FOOTER1")(1)
      .LockContents = False
      .Range.Text = Split(StrHdFt, "|")(1)
      .LockContents = True
    End With
    With ActiveDocument.SelectContentControlsByTitle("A-FOOTER2")(1)
      .LockContents = False
      .Range.Text = Split(StrHdFt, "|")(2)
      .LockContents = True
    End With
    With ActiveDocument.SelectContentControlsByTitle("A-FOOTER3")(1)
      .LockContents = False
      .Range.Text = Split(StrHdFt, "|")(3)
      .LockContents = True
    End With
  End If
 End With
 Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 06-15-2019, 05:59 AM
jhansrod jhansrod is offline If condition not working. Windows 10 If condition not working. Office 2016
Novice
If condition not working.
 
Join Date: Jun 2019
Posts: 16
jhansrod is on a distinguished road
Default

This is fantastic, Thank you.

I am slowly reaching the end of my objective. Once done, I will be sharing the final template as I believe it will be useful to a lot of people.
Reply With Quote
Reply

Tags
if then else;case

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
If condition related query... LearnerExcel Excel 1 03-12-2018 01:37 AM
If condition not working. How to use if condition to for this ... LearnerExcel Excel 1 12-19-2016 02:14 PM
If condition not working. Use Two (concatenate) Fields If Condition Is Met (If... Then... Else) SoonerLater Mail Merge 1 09-30-2015 05:40 PM
If condition not working. Condition formating for date? yabbah Excel 2 05-31-2012 12:38 PM
If condition not working. check with condition karti Word 2 03-15-2011 06:06 AM

Other Forums: Access Forums

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


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