#1
|
|||
|
|||
Help with Case and Select case
Hey guys, I'm working on cases that are dropdown menu dependent and I'm stuck on the code. I have extracted some from an other macro, but am now over my head. The two cases below are controlled by the dropdown menu. Here's the code.
Code:
Sub ConditionalContentPublicRecords() ' ' ConditionalContent Macro Application.ScreenUpdating = False Dim Prot As Variant, BkMrkRng As Range, Rng As Range, FmFld As FormField Dim StrFmt As String, CCBmk As Boolean With ActiveDocument Prot = .ProtectionType Application.ScreenUpdating = False Select Case Selection.FormFields("RecordsDD").Result Case "U.S. Public Records Index": CCBmk = True: StrFmt = " vol." Case "United States Public Records 1970-2010": CCBmk = True: StrFmt = " database," Case Else: CCBmk = False: StrFmt = "" End Select If .ProtectionType <> wdNoProtection Then Prot = .ProtectionType .Unprotect 'Update the bookmark referenced via BmkPopn Set BkMrkRng = .Bookmarks(CCBmk).Range If (CCBmk = True) And (Len(BkMrkRng.Text) = 0) Or _ (CCBmk = False) And (Len(BkMrkRng.Text) <> 0) Then If (CCBmk = True) And (Len(BkMrkRng.Text) = 0) Then BkMrkRng.Text = ", " Else BkMrkRng.Delete End If .Bookmarks.Add CCBmk, BkMrkRng End If Select Case .FormFields("RecordsDD").Result Case "U.S. Public Records Index" If InStr(BkMrkRng.Text, ", vol. ") = 0 Then 'Text & Formfield for volume With Rng .Text = StrFmt & ", vol. " .Collapse wdCollapseEnd End With Set FmFld = .FormFields.Add(Range:=Rng, Type:=wdFieldFormTextInput) FmFld.TextInput.EditType wdRegularText, "X" With Rng .Collapse wdCollapseEnd .InsertAfter ", " .End = FmFld.Range.End .Collapse wdCollapseEnd ' Ancestry Text and Viewing Record Date .InsertAfter " Ancestry (http://www.ancestry.com : viewed " & _ Format(Now, "d MMMM yyyy") & ")," .Words(4).Font.Italic = True .Collapse wdCollapseEnd .InsertAfter ", entry for" 'Name Form Field End With Set FmFld = .FormFields.Add(Range:=Rng, Type:=wdFieldFormTextInput) FmFld.TextInput.EditType wdRegularText, "Name" With Rng .Collapse wdCollapseEnd .InsertAfter ", " .End = FmFld.Range.End .Collapse wdCollapseEnd Set FmFld = .FormFields.Add(Range:=Rng, Type:=wdFieldFormTextInput) FmFld.TextInput.EditType wdRegularText, "Place" 'Place Form Field With FmFld .Name = "Place" .EntryMacro = "" .ExitMacro = "UnlinkFields" .Enabled = True .TextInput.EditType Type:=wdRegularText, Default:="X", Format:="Title case" .TextInput.Width = 0 End With 'Additional Text With Rng .Collapse wdCollapseEnd .InsertAfter ", citing "" voter registration lists, public record filings, historical residential records, and other household database listings.""" .End = FmFld.Range.End .Collapse wdCollapseEnd Select Case .FormFields("RecordsDD").Result Case "United States Public Records 1970-2010" If (InStr(BkMrkRng.Text, "database") = 0) Then With Rng .Text = StrFmt & ", database, " .Collapse wdCollapseEnd .InsertAfter "FamilySearch.org (https://www.familysearch.org/search/collection/2199956 : viewed " & _ Format(Now, "d MMMM yyyy") & "), citing ""telephone directories, property tax assessments, credit applications, and other records available to the public,"" entry for """ .Words(4).Font.Italic = True .Collapse wdCollapseEnd End With 'Firstname Lastname Form Field Set FmFld = .FormFields.Add(Range:=Rng, Type:=wdFieldFormTextInput) With FmFld .Name = "FirstnameLastname" .EntryMacro = "" .ExitMacro = "UnlinkFields" .Enabled = True .TextInput.EditType Type:=wdRegularText, Default:="X", Format:="Title case" .TextInput.Width = 0 End With With Rng .End = FmFld.Range.End .InsertAfter "." .Collapse wdCollapseEnd End With 'Case Else: BkMrkRng.Text = "" End Select If Not FmFld Is Nothing Then BkMrkRng.End = FmFld.Range.End End If .Protect Type:=Prot, Password:=Pwd, NoReset:=True End If End With Set FmFld = Nothing: Set Rng = Nothing: Set BkMrkRng = Nothing Application.ScreenUpdating = True End Sub |
#2
|
||||
|
||||
To put it politely, your code is a dog's breakfast. You have:
• Application.ScreenUpdating = False used twice • a bookmark apparently named CCBmk but defined as a Boolean • bits of Case tests that are not part of a Select Case structure, and so on. I could mess around correcting all that, but I can't even tell from that mess what the code is supposed to do. Perhaps you'd care to explain?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
||||
|
||||
You might also explain what 'rng' refers to.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#4
|
|||
|
|||
Yeah, I know it's a mess, that's why I'm asking for help from people that have skills way beyond mine. I tried to take code from another one that you guys helped me with-didn't work. Here's what the code is supposed to do-
"U.S. Public Records Index, vol. X," Ancestry (http://www.ancestry.com : viewed 5 September 2015), entry for Name, Place, citing "voter registration lists, public record filings, historical residential records, and other household database listings." "United States Public Records 1970-2010," database, FamilySearch.org (https://familysearch.org/search/collection/2199956 : viewed 5 September 2015), citing "telephone directories, property tax assessments, credit applications, and other records available to the public, entry for Firstname Lastname. These two need to be combined, the dropdown menu being the text in quotes. Here's the code (such as it is) for the complete macro- Code:
Option Explicit Const BmkCCBmk As String = "CCBookmark" 'bookmark name Const Pwd As String = "" 'Filling in Forms password Sub PublicRecordsCombinedMacro() ' ' PublicRecordsCombinedMacro Macro ' ' Application.ScreenUpdating = False Dim Rng As Range, FmFld As FormField, i As Long Set Rng = Selection.Range With ActiveDocument Rng.Collapse wdCollapseStart 'Dropdown Menu for Record Collection Set FmFld = .FormFields.Add(Range:=Rng, Type:=wdFieldFormDropDown) With FmFld .Name = "RecordsDD" .EntryMacro = "" .ExitMacro = "ConditionalContentPublicRecords" .Enabled = True With .DropDown.ListEntries .Add Name:="Record Collection" .Add Name:="U.S. Public Records Index" .Add Name:="United States Public Records 1970-2010" End With End With With Rng .End = FmFld.Range.End .InsertBefore """" .InsertAfter "," .Collapse wdCollapseEnd End With .Bookmarks.Add Name:=BmkCCBmk, Range:=Rng With Rng .End = FmFld.Range.End .Collapse wdCollapseEnd End With .Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=Pwd End With Set FmFld = Nothing: Set Rng = Nothing Application.ScreenUpdating = True End Sub Sub ConditionalContentPublicRecords() ' ' ConditionalContent Macro Application.ScreenUpdating = False Dim Prot As Variant, BkMrkRng As Range, Rng As Range, FmFld As FormField Dim StrFmt As String, CCBmk As Boolean With ActiveDocument Prot = .ProtectionType Application.ScreenUpdating = False Select Case Selection.FormFields("RecordsDD").Result Case "U.S. Public Records Index": CCBmk = True: StrFmt = " vol." Case "United States Public Records 1970-2010": CCBmk = True: StrFmt = " database," Case Else: CCBmk = False: StrFmt = "" End Select If .ProtectionType <> wdNoProtection Then Prot = .ProtectionType .Unprotect 'Update the bookmark referenced via BmkPopn Set BkMrkRng = .Bookmarks(CCBmk).Range If (CCBmk = True) And (Len(BkMrkRng.Text) = 0) Or _ (CCBmk = False) And (Len(BkMrkRng.Text) <> 0) Then If (CCBmk = True) And (Len(BkMrkRng.Text) = 0) Then BkMrkRng.Text = ", " Else BkMrkRng.Delete End If .Bookmarks.Add CCBmk, BkMrkRng End If Select Case .FormFields("RecordsDD").Result Case "U.S. Public Records Index" If InStr(BkMrkRng.Text, ", vol. ") = 0 Then 'Text & Formfield for volume With Rng .Text = StrFmt & ", vol. " .Collapse wdCollapseEnd End With Set FmFld = .FormFields.Add(Range:=Rng, Type:=wdFieldFormTextInput) FmFld.TextInput.EditType wdRegularText, "X" With Rng .Collapse wdCollapseEnd .InsertAfter ", " .End = FmFld.Range.End .Collapse wdCollapseEnd ' Ancestry Text and Viewing Record Date .InsertAfter " Ancestry (http://www.ancestry.com : viewed " & _ Format(Now, "d MMMM yyyy") & ")," .Words(4).Font.Italic = True .Collapse wdCollapseEnd .InsertAfter ", entry for" 'Name Form Field End With Set FmFld = .FormFields.Add(Range:=Rng, Type:=wdFieldFormTextInput) FmFld.TextInput.EditType wdRegularText, "Name" With Rng .Collapse wdCollapseEnd .InsertAfter ", " .End = FmFld.Range.End .Collapse wdCollapseEnd Set FmFld = .FormFields.Add(Range:=Rng, Type:=wdFieldFormTextInput) FmFld.TextInput.EditType wdRegularText, "Place" 'Place Form Field With FmFld .Name = "Place" .EntryMacro = "" .ExitMacro = "UnlinkFields" .Enabled = True .TextInput.EditType Type:=wdRegularText, Default:="X", Format:="Title case" .TextInput.Width = 0 End With 'Additional Text With Rng .Collapse wdCollapseEnd .InsertAfter ", citing "" voter registration lists, public record filings, historical residential records, and other household database listings.""" .End = FmFld.Range.End .Collapse wdCollapseEnd Select Case .FormFields("RecordsDD").Result Case "United States Public Records 1970-2010" If (InStr(BkMrkRng.Text, "database") = 0) Then With Rng .Text = StrFmt & ", database, " .Collapse wdCollapseEnd .InsertAfter "FamilySearch.org (https://www.familysearch.org/search/collection/2199956 : viewed " & _ Format(Now, "d MMMM yyyy") & "), citing ""telephone directories, property tax assessments, credit applications, and other records available to the public,"" entry for """ .Words(4).Font.Italic = True .Collapse wdCollapseEnd End With 'Firstname Lastname Form Field Set FmFld = .FormFields.Add(Range:=Rng, Type:=wdFieldFormTextInput) With FmFld .Name = "FirstnameLastname" .EntryMacro = "" .ExitMacro = "UnlinkFields" .Enabled = True .TextInput.EditType Type:=wdRegularText, Default:="X", Format:="Title case" .TextInput.Width = 0 End With With Rng .End = FmFld.Range.End .InsertAfter "." .Collapse wdCollapseEnd End With 'Case Else: BkMrkRng.Text = "" End Select If Not FmFld Is Nothing Then BkMrkRng.End = FmFld.Range.End End If .Protect Type:=Prot, Password:=Pwd, NoReset:=True End If End With Set FmFld = Nothing: Set Rng = Nothing: Set BkMrkRng = Nothing Application.ScreenUpdating = True End Sub |
#5
|
||||
|
||||
Quote:
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
If the question you are asking is for the two new source citations to be integrated into the other post, the answer is no. These two are different source citations. What I tried to do with the other post is try and figure out what I could extract and use for the these two new ones. However, the other code in the other post is quite complicated and I could only figure out some things. I have several more of these citations that start out with a dropdown menu followed by text, form fields and of course the web site. Thanks for your help-
|
#7
|
||||
|
||||
Perhaps, then, you could attach a document to a post showing what it is you're trying to achieve.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Here's the doc-blue is the dropdown menu, yellow is the form fields(text), and the rest is conditional text-thanks for your help-
|
#9
|
||||
|
||||
See attached.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
Thanks Paul, I have many questions, I will try to limit them.
1) Where does the dropdown menu come from? 2) What is Chr(148)? 3) What are Private Sub Documents? Learning, lots more to learn- |
#11
|
|||
|
|||
I'm working on my Mac and instead of the " after the dropdown menu (which I think is Chr(148)), I get a " î". What's up with this?
|
#12
|
|||
|
|||
Got it, it's an ascII character and for the Mac it's 211-learning new things-
|
#13
|
||||
|
||||
Quote:
2: A closing double-quote character 3: There is no such code in the document I posted. Amongst other things, the 'Private' directive hides a Sub from the GUI
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
|||
|
|||
1) What I want to know is how you did #1
2) Why not just insert text? Preference? 3) I have no clue what a GUI is- |
#15
|
||||
|
||||
1: Developer>Controls>Legacy Controls>Drop-Down Form Field.
2. Without a dropdown how would you propose to choose the option that governs the text? 3: Graphical User Interface.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Replace & case | Jennifer Murphy | Word | 1 | 02-11-2013 03:26 AM |
Question about Case statement | Jennifer Murphy | Word VBA | 1 | 01-05-2013 02:30 PM |
Case Sensitive (contains) Selection | apolloman | Excel | 2 | 07-12-2011 04:50 AM |
From all UPPER CASE to Proper Case | davers | Word | 1 | 04-30-2009 12:41 PM |
Upper to lower case | jd | Excel | 1 | 04-28-2006 07:40 AM |