PDA

View Full Version : Solved: Word VBA making Groups



island17
11-23-2004, 09:46 AM
Hello all

Groups may be misleading since I didn't know what else to call it. What I would like to do is define a type, or variable, and add members to it.

Example
I need to change en-dashes to em-dashes only if they are not of one of the headline sytles. I know in other languages I can do something like.

Dem Xstyles as styles

Xstyles = ("head1", "head2","head3)

if style <> Xstyles then
do this
endif

I hope I conveyed my idea clearly. I have looked for an answer in my nO'Reilly book, but can't find one. I know how to handle this by checking each paragraph, but thought this would be cooler if it could be done.

Thanks in advance

TonyJollans
11-23-2004, 06:06 PM
Hi Russ,

I don't think you can do it like that really, although it would be nice. You could do something if you set up your styles in a Collection, something like ..

Dim myStyles as new collection, dummy
mystyles.add Item:=True, Key:="Heading 1"
mystyles.add Item:=True, Key:="Heading 2"

On Error Resume Next

dummy = myStyles(Selection.Style)
If Err.Number = 0 then
' whatever
Else
' whatever else
End If

And you could always load your collection in a loop from an array so that it almost looked like what you ask.

island17
11-24-2004, 11:37 AM
Tony

Thanks, I started using the array approach just in case my idea was not possible. Thanks again.

Bilby
11-24-2004, 03:05 PM
Another option may be to use the instr function.
Loop through the paragraphs.
Put your target styles into a string,oPara (thats set as Activedocuments.Paragraphs) will return 0 if the style name can't be matched.

For Each oPara In oDoc.Paragraphs
' If valid Style then execute
If InStr(1, "Heading 1,Heading 2,Heading 3", oPara.Style) <> 0 Then
' replace an en(150) dash with an em dash
End If
Next oPara

island17
12-02-2004, 08:13 PM
Bilby

Thanks, that is sort of what I did. I created a text file with a list of our Headline styles. I then open that file from my macro and read it into an array. Then I check each paragraph for an en-dash using Instr(). if one exist I replace all the en-dash in that paragraph with an em-dash. I use a text file to hold the names of the styles so if they add any new Headline styles I can easily add it to the list with out disrupting the users. Sort of on the fly.

Thanks again