Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-17-2020, 05:49 PM
Kindyr Kindyr is offline Naming a variable using userform fields Windows 10 Naming a variable using userform fields Office 2019
Novice
Naming a variable using userform fields
 
Join Date: Aug 2020
Posts: 12
Kindyr is on a distinguished road
Default Naming a variable using userform fields

Hello, I am wanting to create a persistent variable in a Word document that is based off of a userform. Basically, a user will a form, fill it out and at a later point, information from that userform will be used to print something else. So we will say they have two boxes to fill out with simple information. Those boxes will be EVParty (PartyTypeList) and EVNum (EvidenceID). I want to name a variable with the input from those two boxes. Here is what I have so far:



Dim EVParty As String
EVParty = PartyTypeList.Value
Dim EVNum As String
EVNum = EvidenceID.Value
Dim EXHIB As String
EXHIB = EVParty & EVNum


Now, I want to name a variable with the value that is EXHIB. So let's say the had a party named ...Bob and a number of 3, EXHIB = Bob3, and our new variable would be named Bob3.

The reason I need Bob3 is because I want to put more information into a specific item that is the unique item of Bob3 which would include the page number of Bob3 and other bits of information that might be added at other points of the document. So I'm wanting this to be a persistent variable that is the value of EXHIB. (Sorry if I am over explaining.)
Reply With Quote
  #2  
Old 08-18-2020, 11:11 AM
Kindyr Kindyr is offline Naming a variable using userform fields Windows 10 Naming a variable using userform fields Office 2019
Novice
Naming a variable using userform fields
 
Join Date: Aug 2020
Posts: 12
Kindyr is on a distinguished road
Default

I figured it out!! S

Private Sub PrintBlurb2_Click()
' Declare everything strings.
Dim EVParty, EVNum, arr() As String
' Pull the values from the Userform text boxes of PartyTypeList and EvidenceID.
EVParty = PartyTypeList.Value
EVNum = EvidenceID.Value

'Create a simple array of one item that pushed the two items together into one item.

arr = Split("EVParty&EVNum| EVPartyEVNum", "|")

'Use the array zero point to create a variable with an out put that is the same as the name of the variable, but with a space in it.
ActiveDocument.Variables(arr(0)) = EVParty & " " & EVNum

' Make a message box that does the check.
MsgBox ActiveDocument.Variables(arr(0))

Unload Me

End Sub
Reply With Quote
  #3  
Old 08-18-2020, 04:08 PM
Guessed's Avatar
Guessed Guessed is offline Naming a variable using userform fields Windows 10 Naming a variable using userform fields Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I'm not convinced you have this solved yet. Your current code actually populates the same variable (called "EVParty&EVNum") regardless of the actual values in your variables.

You have variables but are using strings of the variable names and expecting the macro to convert the string name of the variable into the variable value. Also, using a space as a separator is useless if either of the variable values contain a space - better to use something that won't appear in the values such as "|"

If you are intending to create a variable based on the values entered by the user in the userform, how are you going to make use of it in the document and/or when reopening the userform? You won't know the name of the variable to go looking for it.

I would recommend a unique location/name for this data location and if you need to store multiple values there then make them all part of a single string with a separator.

For example to write the form info to a variable
Code:
EVParty = PartyTypeList.Value
EVNum = EvidenceID.Value
ActiveDocument.Variables("FormInfo") = EVParty & "|" & EVNum & "|" & "Other stuff"
And to read these values when initialising the form
Code:
Dim sArr() as String
sArr = Split(ActiveDocument.Variables("FormInfo"),"|")
If UBound(sArr) > 1 then
  PartyTypeList.Value = sArr(0)
  EvidenceID.Value = sArr(1)
  MsgBox "Other stuff is: " & sArr(2)
End If
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 08-18-2020, 04:38 PM
Kindyr Kindyr is offline Naming a variable using userform fields Windows 10 Naming a variable using userform fields Office 2019
Novice
Naming a variable using userform fields
 
Join Date: Aug 2020
Posts: 12
Kindyr is on a distinguished road
Default

I need the name of the variable to be able to be different depending on what is put into a userform. So these are legal documents. The information is based off parties (Plaintifff, Defendant, etc) and their evidence numbers (1, 2,A, B, 2-D etc) but will not be the same on every single document. So on one document, you might have a document that is Plaintiff2 and DefendantA while on another you will have one that is Defendant2 and Plaintiff2. They change.

Later, I will have a function that will pull up every variable that starts with Plaintiff and every variable that starts with Plaintiff and list them. So the values of the varibles for DefendantA and Plaintifff2 will involve information such as their page numbers, which I will be using an array for. I have figured out most of everything except for naming the variables to pull the information from.

I have an entirely different way to do the same thing, but it involves a different method of using comments in a document that I wasn't wanting to use and wanted to see if there was going to be a way to just name variables on teh document.
Reply With Quote
  #5  
Old 08-18-2020, 06:21 PM
Guessed's Avatar
Guessed Guessed is offline Naming a variable using userform fields Windows 10 Naming a variable using userform fields Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I'm not seeing how you plan to make use of the document variables in the document itself. I assume your other code is looping through the variables that exist and then places fields into the document at relevant positions. You are going to have broken fields unless you also have macros to clean up content when variables don't exist.

If you need further help on this, post a sample document showing how your userform is coded and a couple of examples of where variables are used in the body of the document.

FWIW, I have never used Document Variables as I didn't like having stuff I couldn't also see and edit with the GUI. When I had similar requirements I used Document Built-in and Custom properties. Nowdays I make use of XML and linked Content Controls for these types of tasks. Linked CCs allow contents of these variables to be populated directly from the document, don't require field updates to refresh and are more forgiving when backend fields are missing.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 08-19-2020, 06:41 AM
Kindyr Kindyr is offline Naming a variable using userform fields Windows 10 Naming a variable using userform fields Office 2019
Novice
Naming a variable using userform fields
 
Join Date: Aug 2020
Posts: 12
Kindyr is on a distinguished road
Default

I'll look into content controls. I'm still SUPER new to VBA. I started out with a project to do one thing and it morphed and morphed from old 20 year old projects into this one. I have various things I want the different variables saved on each document to do. One of the reasons I wanted to be able to name a variable with another variable was to have a naming convention FOR the variables. So in the example Plaintiff's Exhibit 3, the variable saved to the document would be Plaintiff3, and the macro used to create this variable will "page stamp" the variable, showing the macro was used on the specific page because that is when something happened to Plaintiff's Exhibit 3. So the variable would be built like: Plaintiff3 = Admitted|T/F|Page|Marked|T/F|Page. So the T/F will be a true false call, the page will be the page it was addmited or the page it was marked.

But as I've said, the same parties and exhibit numbers will not always be on the same document. In different places in the document, the information will need to be pulled up.

So later in the document, when I need to write down when X happened to Plaintiff's Exhibit 3, I can pull from the variable of Plaintiff3 and have it express that it was Admitted on page 15 ad Marked on page 10 and so forth.

This is all legal document work. This is just one of the things I'm working to automate in a different way than I already have them automated. RIght now, I have a macro that creates a comment that holds the information and later something that calls the informaiton from all macros, but I was looking for an easier way to compare the data. So that if Plaintiff's 3 is seen on page page 5 and page 4, which would be two different comments, I wouldn't have two output lines, I would just get 1. Since I'm so new to VBA, my brain was not getting the logic of loop I would need to create to search all comments, compare all comments, combine them, then output them in the format I wanted them to do. SO, I came up with the idea of just creating an object on the document that would be named based on the exhibit itself, the function would later search all variables that coinside with the party.

The whole template I am creating has to be able to be simple use by anyone who has Word. They can't have to download different programs, or know how to install add-ons or anything like that. If it goes beyond checking a template box in Developer tab, it's too complicated for the end user. It also has to be a single document, so like, I can't have it call from other programs or online processes that would probably make life a lot easier.


As I've said, this is only ONE part of an entire template. Tracking, indexing, and using exhibits in a hearing. It will be used in written transcripts of hearings, so the typist has a slightly easier time of typing up their work.
Reply With Quote
  #7  
Old 08-19-2020, 03:25 PM
Guessed's Avatar
Guessed Guessed is offline Naming a variable using userform fields Windows 10 Naming a variable using userform fields Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

If you are open to looking at linked content controls and repeating data I would recommend you have a look at the resources available on Greg Maxey's site. This page for instance should give you an idea on how you could record exhibits and then make use of those throughout the document
Add, Map and Employ as Repeating Section Content Control (RSCC) in Word 2013
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 08-19-2020, 03:39 PM
Kindyr Kindyr is offline Naming a variable using userform fields Windows 10 Naming a variable using userform fields Office 2019
Novice
Naming a variable using userform fields
 
Join Date: Aug 2020
Posts: 12
Kindyr is on a distinguished road
Default

This is almost exactly perfect! With this, my end users won't have to do anything special or fiddle with things other than just use the forms already created? I had initially been thinking back to the days of Access and wanted to create behind the scenes databases that would build themselves then not be an issue once the document was printed. Then someone pointed me at VBA and it does a lot of the things I want it to. XML looks interesting and fun, I just know nothing of it.

Will my end users have to do anything other than use my template?
Reply With Quote
  #9  
Old 08-19-2020, 04:05 PM
Guessed's Avatar
Guessed Guessed is offline Naming a variable using userform fields Windows 10 Naming a variable using userform fields Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Implementing templates for users is a bit of an artform but there is no reason why your users would have to interact with anything other than your template.

I would be delivering the .dotm template and allowing the users to create .docx files from that template. That way the code sits in the 'attached template' and the document content includes the full transcript and background xml data.

If you read up on Greg's site and also have a look at his CC tool template I think you may even be able to roll out a no macro template but the user interactions would be easier with macros and ribbon buttons.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #10  
Old 08-19-2020, 04:13 PM
Kindyr Kindyr is offline Naming a variable using userform fields Windows 10 Naming a variable using userform fields Office 2019
Novice
Naming a variable using userform fields
 
Join Date: Aug 2020
Posts: 12
Kindyr is on a distinguished road
Default

The point of the template I'm working on IS macros, creating shortcuts for a typist to make a long complicated, formatted document using macros and forms. A lot of the work is done by text expanders, but creating some of the formatting can't be done with the simple expanders available.

Thank you so much for this help. I'm going to look through this and see if I can wrap my little peapicking brain around XML.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Show userForm from variable Cosmo Word VBA 5 01-31-2018 12:59 PM
Naming a variable using userform fields Assigning a string variable to a userform label caption Larry_1 Excel Programming 3 12-18-2017 06:59 AM
Project - variable data fields pmarc Word 7 04-04-2013 05:07 PM
30+ days Variable Day Date Calculations via Fields ztag Word 2 01-06-2012 11:12 AM
Variable fields? Emalee77 PowerPoint 0 01-30-2011 05:58 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:13 PM.


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