PDA

View Full Version : [SOLVED:] Dynamically Creating Numbers.



Sisu
05-28-2015, 05:29 PM
Using VBA and Winforms I am trying to figure out how to input a number (ex. "42") into a box and have a list of 42 numbers (1,2,3....41,42) appear in my word doc WHILE STILL HAVING {DocVariables} and text enabled in each number generated. For example if I have the phrase "{DocVariable} where DocVariable = John in #1, "John" ran a stop sign, I want to try to have (i) each number be generated and then (ii) filled in with a {DocVariable} and some pre-arranged text.

I think I have to go crazy using if statements. Not even sure if Word can dynamically make things but this forum has godlike people. Thought I'd ask. Thanks.

SamT
05-28-2015, 07:14 PM
Using VBA and Winforms I am trying to figure out how to input a number (ex. "42") into a box and have a list of 42 numbers (1,2,3....41,42) appear in my word doc WHILE STILL HAVING {DocVariables} and text enabled in each number generated.

For example if I have the phrase "{DocVariable} where DocVariable = John in #1, "John" ran a stop sign, I want to try to have (i) each number be generated and then (ii) filled in with a {DocVariable} and some pre-arranged text.
There's a bunch of information missing between those two statements. Can you fill us in.

gmayor
05-28-2015, 09:54 PM
Like Sam I cannot see the logic of what you are trying to do here. Creating numbered docvariables is easy enough, but if you are creating a variable number of docvariables on the fly then how do you determine what the values of those variables are? The associated docvariable fields will have to be placed somewhere that cannot been predetermined, because the number is variable. It doesn't make any sense.

Sisu
05-29-2015, 01:15 AM
There's a bunch of information missing between those two statements. Can you fill us in.

Sure! Sorry if I jumped some logic there. Bad habit of mine. There should be a WinForms box with a number entry box of 2-digits [42] to generate the numbers, a textbox to say which number I am answering and a corresponding box to say the text input. When I click a button the output should be, numbered 1 to 42, "John" ___<Did Something Prearranged>___. Like:

Winforms:
[42] SUBMIT <42 numbers generated>
[1] [John] SAVE <Document Updated>
[2] [Timothy] SAVE <Document Updated>
.
.
[41] [John] SAVE “”
[42] [Mary] SAVE “”

Document:
1. John drank soda.
2. Timothy drank soda.
.
.
41. John drank soda.
42. Mary drank soda.

I can make the textboxes and figured out some additional stuff from gmayor so far. This is just the part that I'm not even sure VBA can do.

This is an outgrowth of everyone's lessons on here, especially gmayor's tutorials that got me restarted with VBA. Thanks for the fun of just reading the great code written here in any case.

gmaxey
05-29-2015, 05:37 AM
Maybe something like this:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim lngIndex As Long, lngCount As Long
Dim oRng As Word.Range
lngCount = InputBox("How many?")
For lngIndex = 1 To lngCount
Set oRng = ActiveDocument.Range
oRng.Collapse wdCollapseEnd
ActiveDocument.Fields.Add oRng, wdFieldSequence, "Num"
oRng.Start = ActiveDocument.Range.End
oRng.InsertAfter ". "
oRng.Start = ActiveDocument.Range.End
ActiveDocument.Fields.Add oRng, wdFieldDocVariable, "Name"
oRng.Start = ActiveDocument.Range.End
oRng.InsertAfter " drank soda." & vbCr
Next
lbl_Exit:
Exit Sub
End Sub

SamT
05-29-2015, 09:45 AM
@ Greg

Word! You the man.

Sisu
05-29-2015, 05:10 PM
Interesting is all I can say right now. I think maxey is on the right track. I can practice with this but may need private help. I'll see if we can keep in touch :thumb.

gmaxey
05-29-2015, 06:37 PM
SamT,

Nah just one of the boys ;-)

SamT
05-29-2015, 07:48 PM
@ Sisu,

Please try to keep the converstion public so our other guests can learn from your experience. Obviously if you need to keep it private with Greg, you are wlecome to do so, but do keep it to Needs Must.

Sisu
05-30-2015, 02:26 AM
13563

I was sort of educated as a systems analyst - now a law student. There's not much code yet except really this and GMayor's. Here's a wireframe of what I am doing :sleuth:. The idea here is to make some software that will create a large legal document that will be filled in by variables through this form. This will hopefully prevent most of the crazy-clicking, hilighting, deleting, and typing that I've been doing thereby saving time (time value of money). A couple of the documents have an unknown amount of questions to be asked by counsel. So earlier the question had to do with that - How could I generate the numbers and then answer the question using a variable like 'John' (plaintiff/defendant).

It's amazing to see VBA come this far. I am still old enough to remember the days when there wasn't enough RAM to handle the huge forms I would try to build, eventually giving up/waiting for hardware to catch up even on a top-level "gaming-pc".:yes And it's caught up for the masses!

SamT
05-30-2015, 08:07 AM
To: Sisu
CC: Greg

I'm just blowing smoke out my ears. Oh! Wait, that's a mixed allegory.

Well it's more office safe than it started out.

Anyway, integration and diversity might be better for you. Put your Form and its associated lists, et al, in Excel, then pass all the answers to your word Doc.

And Now, I shall unsubscribe to this thread and get my big nosy out-a-here.

Sisu
05-30-2015, 08:58 AM
To: Sisu
CC: Greg

I'm just blowing smoke out my ears. Oh! Wait, that's a mixed allegory.

Well it's more office safe than it started out.

Anyway, integration and diversity might be better for you. Put your Form and its associated lists, et al, in Excel, then pass all the answers to your word Doc.

And Now, I shall unsubscribe to this thread and get my big nosy out-a-here.

No worries! The documents are mostly just government forms - anyone can find the basic template on their state's website and do something similar. We the people...by the people...

I just don't have the analysis down first I suppose, least to say the logic. I hadn't thought about using excel to pass to word. I'll look at that, thanks!