PDA

View Full Version : Plotting 'Dots' on Word Doc Pages



ytwater
05-10-2006, 05:04 AM
I am teaching myself VBA and trying to write a Word macro at the same time. My goal, in part, is to develop a Word doc that contains a series of 'connect the dots' pages. In other words, a specified number (Cnt) of random points would be printed on a specified number of pages (Plts).

My subroutine to date generates random number arrays for row and column position for each page. These arrays are then sorted by row number and then column position for duplicated row numbers. :clap: My problem is that I cannot figure out how to 'plot' these points (coordinates) onto pages of the opened document.:banghead:

Any thoughts, suggestions and/or direction will be greatly appreciated.:friends:

The code that I have written is as follows:


Sub Dots

Dim rows(100), cols(100) As Integer
Dim KNT, I, Loop1, Loop2 As Integer

Cnt = InputBox("Enter the number of points to plot")
Plts = InputBox("Enter number of pages desired")



For KNT = 1 To Plts
For I = 1 To Cnt
Randomize
cols(I) = Int((131 * Rnd) + 1)
rows(I) = Int((66 * Rnd) + 1)


Next I
'Sort arrays by row number


For Loop1 = 0 To Cnt
For Loop2 = Loop1 To Cnt
If rows(Loop2) < rows(Loop1) Then
rowstr1 = rows(Loop1)
rowstr2 = rows(Loop2)
rows(Loop1) = rowstr2
rows(Loop2) = rowstr1
colstr1 = cols(Loop1)
colstr2 = cols(Loop2)
cols(Loop1) = colstr2
cols(Loop2) = colstr1


End If
'Sort arrays by column number


If rows(Loop2) = rows(Loop1) Then
If cols(Loop2) < cols(Loop1) Then
colstr1 = cols(Loop1)
colstr2 = cols(Loop2)
cols(Loop1) = colstr2
cols(Loop2) = colstr1
End If


End If


Next Loop2


'Debug.Print rows(Loop1), cols(Loop1)
Next Loop1


Next KNT
End Sub


Edited 11-May-06 by GeekGirlau. Reason: insert vba tags

geekgirlau
05-10-2006, 08:05 PM
Hi ytwater,

Welcome to the Board! :wavey: I've inserted vba tags around your code to make it easier to read - when you are posting code, paste (or type) the code, select it, and click on the "VBA" button.

fumei
05-10-2006, 11:00 PM
My subroutine to date generates random number arrays for row and column position for each page. That is a good start.
My problem is that I cannot figure out how to 'plot' these points (coordinates) onto pages of the opened document.And....WHAM!...yup, you do have a problem.

You will not be able to do so. Word does not HAVE a coordinate system. It is a word processor. It is not a graphics application. It does not HAVE a row/column format inherently. Sure, you can make tables. Do you really want to put single dots into single cells of a very very ugly table?

I think not.

Word is a word processor. That is what it is for.

ytwater
05-11-2006, 04:30 AM
Thank you geekgirlau!

Gerry,
I knew that Word didn't have an x-y coordinate system. I was hoping to find a way to step through my row array for the number of lines within a Word page (I think that is 66). As I proceed through the array, a blank line would be 'printed' if the array element did not match the row number. If it did, then space over the number of columns associated with the column array element and 'print' a dot. If any other sucssesive row array elements equal the current row number, then space the number of columns equal to the difference between the current and previous column elements and then print another dot.

Thoughts?

fumei
05-11-2006, 07:03 AM
Number one:

My title page for documents has....2 lines. That is because the styles used for that page are BIG. Point? If there are no paragraphs on a "page" there ARE no lines. Lines are calculated. They have no existence on their own. They are completely derived from the paragraphs on the page.

Number two:
If it did, then space over the number of columns associated with the column array element and 'print' a dot. Space over what?????? For the sake of argument, say you are doing your thing and you get to "Row" ("Line") 14. OK. Now you have the need to put your dot, oh, 17 "spaces" in from the left margin. Unless there IS something there, there is nothing to space over. So, are you talking about having each page full of spaces?

Mind you.......hmmmmmmm, in theory.........hmmmmm, interesting......hmmmmmm.

Know what? If you did that, and the style (ie. font size) is known....hmmmmmmm.

Could be done. Ugly as sin, but possible. You could replace the spaces with dots AS IF they are a coordinate system.

However, I can not see any other way of doing it.

fumei
05-11-2006, 07:25 AM
Well I'll be darned. I have never done this before. I just made a new document, put my finger down on the spacebar and held it. For some reason I was expecting that when the Selection point got to the right margin it would wrap. It did not. It merrily went along. I stopped with a Col count of over 700. It never stops.

OK. I have (using default New document Arial 12 pt) 200 "points", or spaces per row. This is with minimum margins. I figured you would want to get as many coordinates as possible. Note of course if you DO change font size there are still the same number of points, the rest are simply invisible off in never-never land. Odd that.

Anyway, here are two possibilities:ActiveDocument.Paragraphs(14).Range.Select
With Selection
.Collapse Direction:=wdCollapseStart
.MoveStart Unit:=wdCharacter, Count:=123
.MoveEnd Unit:=wdCharacter, Count:=1
.Text = "."
' collapse just so I can see it
.Collapse Direction:=wdCollapseEnd
End WithThis pretends that paragraph 14 is "row" 14, and then changes the 124th character from a space to a period.

OR......based on a 200 character/space per "line" (ie paragraph), you can explicitly use the character count.With Selection
.Range.Start = (14 * 200) + 124
.Extend
.Text = "."
End With
Don't know if this helps.

ytwater
05-11-2006, 08:17 AM
Gerry,
As a newbie to VB, the code you provided looks very daunting. But, it will give me a start.

Here is a little background on my thought process that was used to develop this program idea. My programming experience is FORTRAN and BASIC. I learned these back in the days of line printers <I showing my age!>. So using space, line feed, form feed and carriage return ASCII codes are very familiar to me.

Is it possible to create a lengthy print string in VBA (Word) of my goal and have the combined commands interpreted in a way to create a .TXT file instead of writing to the currently opened Word doc?

Steve

fumei
05-11-2006, 09:42 PM
Hate to tell you this, but I am probably older. I keep a 14 inch hard drive platter on my wall as a memento.

Actually the code is not very difficult at all. Let's see if I can help. In the first chunklet, the code selects the 14th paragraph - or 14th "row". Each "row" is a paragraph of spaces the full width of the page. The code selects the 14th paragraph. "Collapses" - that is, it shrinks the selected text down to a point. Moves the point 123 spaces, and then MAKES the selection a dot. This replaces the space with a dot.

It is vital you understand the significance of paragraphs in Word. Also, if you are going to manipulate Word, ummm, numerically, you must grasp that everything in a Word document is located by the character count from the start of the document.

So if you want a dot on the 14th "row" (paragraph), and 100 spaces "in", then the location count is 13 "rows" (each 200 spaces in my document) - so 13 x 200 characters PLUS 100 more "IN". The location of anything is the actual number of characters counted from the start.

OK. So now...you want to do this as a long string into a text file? yes, I suppose it is possible. No, it is possible.

But I gotta ask....why the hell are you doing this again??????