• Passing Arrays between Templates WordXP/97/VBA

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Passing Arrays between Templates WordXP/97/VBA

    Author
    Topic
    #369123

    Greetings all,

    We have docs developed with different styles (lotsa both). I have a module that compares the docs existing styles to the updated/new styles, replaces them and deletes the old styles.

    Unfortunately I have to use this module in every template becouse I can’t work out how to pass the two arrays from the attached template to a global template.
    Can anyone tell me whether you can pass arrays as parameters between templates?

    Viewing 2 reply threads
    Author
    Replies
    • #580396

      I’m not sure I grasp your question. You can pass an array to a subprocedure in a different project just as you would any other variable. For example:

      Sub PassArray()
      'This module lives in a document
      Dim strArray(3) As String
      strArray(0) = "Alpha"
      strArray(1) = "Beta"
      strArray(2) = "Gamma"
      strArray(3) = "Delta"
      Application.Run "arraydemo", strArray()
      End Sub
      Sub ArrayDemo(strArray() As String)
      'This procedure lives in a global template
      Dim intCounter As Integer
      For intCounter = 0 To UBound(strArray)
          Debug.Print strArray(intCounter)
      Next
      End Sub

      If you are trying to pass the Styles collection, I don’t think you need to pass it so much as just reference it, although I’m not sure how you reference the Styles of a Template, as opposed to the styles in a document.

    • #580438

      Did you know that you can do this by opening the document, selecting Tools/Templates & Addins, then check the box “automatically update document styles”. Programmatically –

      Sub UpdatesMyStyles()
      ActiveDocument.UpdateStylesOnOpen = True
      End Sub

      You can’t pass arrays from one template to another, but you could copy styles using the following code (which uses “Organisizer”:

      Sub CopyMyStyles()
      Application.OrganizerCopy Source:=”C:My DocumentsDocument1.doc”, _
      Destination:=”C:My TemplatesTemplatename.dot”, Name:=”samplestyle”, _
      Object:=wdOrganizerObjectStyles
      End Sub

      Hope this helps
      Cathy

    • #581055

      Thanks for your comments everyone but to clarify my situation…
      I have heaps of templates being used throughout Australia to deliver technical documents.
      My front end uses a global template to control execuation , and VBA code in the ‘child’ templates.
      Now the documents produced do have their own styles but are continually being updated and modified.
      With several different style sets, using the ‘latest’ version results in the newly attached macros failing becouse the document
      and the new template don’t share a common style set.

      My solution is to create a 2 dimensional array that controls substitution of style sets, updates the document and then deletes the old style set. This must be in the new template.

      I would like the actual VBA procedures to be global and located only in the global template. But becouse I
      can’t pass an array from a documents attached template to the master global one I have to duplicate the same code in each of many many many templates.

      There are literally thousands of docs out there, a global module is a lot easier to keep track of than an
      entire suite of induvudualized templates.

      Thanks again for your responses.

      • #581082

        > …I can’t pass an array from a documents attached template to the master global one…

        I don’t understand the problem here. The sample code I posted works with a two-dimensional array also, building the array and calling the global procedure. Does this approach not work for you?

        Sub PassArray()
        'This module lives in a document template
        Dim strArray(3, 1) As String
        strArray(0, 0) = "Alpha"
        strArray(0, 1) = "Waves"
        strArray(1, 0) = "Beta"
        strArray(1, 1) = "Test"
        strArray(2, 0) = "Gamma"
        strArray(2, 1) = "Rays"
        strArray(3, 0) = "Delta"
        strArray(3, 1) = "Airlines"
        Application.Run "arraydemo", strArray()
        End Sub
         
        Sub ArrayDemo(strArray() As String)
        'This procedure lives in a global template
        Dim intCounter As Integer
        For intCounter = 0 To UBound(strArray)
            Debug.Print strArray(intCounter, 0)
            Debug.Print strArray(intCounter, 1)
        Next
        End Sub

        If you need to invoke the global procedure first and then retrieve an array from the document template, you might be able to do it with a Class Module and Property Get procedure in each document template. However, that probably would pass a collection rather than an array, which is much less convenient for multi-dimensional data. I’ve never coded a Property Get procedure, so I will leave that issue to the experts.

        • #581085

          Just as an addendum to Jefferson’s note:

          If you set a reference (Tools > References) to the global template, from the document template, then you should be able to call functions (and pass parameters) that are in the global template, just the same as though they were in the document template.

          So:

          ArrayDemo strArray()

          should also work in that case.

          Gary

        • #581232

          Unfortunately I don’t know how to write class modules yet but between your answer and Chris Greaves I’ve got a viable solution.

          Looking at your example I can’t figure out why mine wouldn’t work. Maybe after I’ve got a working string parsing solution going I’ll be able to re-visit a ‘neater’ array solution.

          many thanks
          Frank

      • #581138

        > can’t pass an array

        You’re in luck! I just downloaded a dictionary of Australian Slang (grin!).

        OK. In the past when I’ve been faced with a problem like this, I’ve said the heck with it. If I can’t pass an array (which is really just an internally structured string of items), I’ll pass a string.

        In the simplest case – how to pass a string array – I decompose the rows and columns into strings of characters, catenate them, and pass that string.

        Brooke knows I’ve decomposed a WAN and a LAN into strings of computers which are strings of drives which are strings of paths which are strings of folders which are strings of files (TXT, BAS, XLS, DOT etc) which are strings of Modules which are strings of Procedures (SUBs and FUNCTIONs) which are strings of lines which are strings of characters.

        It sounds exhausting, but it’s not really that bad. You need to write a few utility routines or you can use mine (try looking for strSplitStringAt in VBA forum).

        If you can reduce a style to a string, then an array of styles is an array of strings. Piece of cake.

        I think that style can be defined completely in a chunk of VBA code (see Macro (recorded hastily as an experiment) below), so there ought not to be a difficulty there at all.

        Sub Macro1()
        '
        ' Macro1 Macro
        ' Macro recorded 04/08/02 by Christopher Greaves
        '
            ActiveDocument.Styles.Add Name:="Style1", Type:=wdStyleTypeParagraph
            With ActiveDocument.Styles("Style1")
                .AutomaticallyUpdate = False
                .BaseStyle = "Normal"
                .NextParagraphStyle = "Style1"
            End With
            With ActiveDocument.Styles("Style1").Font
                .Name = "Times New Roman"
                .size = 12
                .Bold = False
                .Italic = False
                .Underline = wdUnderlineNone
                .StrikeThrough = False
                .DoubleStrikeThrough = False
                .Outline = False
                .Emboss = False
                .Shadow = False
                .Hidden = False
                .SmallCaps = False
                .AllCaps = False
                .ColorIndex = wdRed
                .Engrave = False
                .Superscript = False
                .Subscript = False
                .Scaling = 100
                .Kerning = 0
                .Animation = wdAnimationNone
            End With
            With ActiveDocument.Styles("Style1").ParagraphFormat
                .LeftIndent = CentimetersToPoints(0)
                .RightIndent = CentimetersToPoints(0)
                .SpaceBefore = 3
                .SpaceAfter = 3
                .LineSpacingRule = wdLineSpaceSingle
                .Alignment = wdAlignParagraphCenter
                .WidowControl = True
                .KeepWithNext = False
                .KeepTogether = True
                .PageBreakBefore = False
                .NoLineNumber = False
                .Hyphenation = True
                .FirstLineIndent = CentimetersToPoints(0)
                .OutlineLevel = wdOutlineLevelBodyText
            End With
            ActiveDocument.Styles("Style1").ParagraphFormat.TabStops.ClearAll
            With ActiveDocument.Styles("Style1").ParagraphFormat
                With .Shading
                    .Texture = wdTextureNone
                    .ForegroundPatternColorIndex = wdAuto
                    .BackgroundPatternColorIndex = wdAuto
                End With
                .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
                .Borders(wdBorderRight).LineStyle = wdLineStyleNone
                .Borders(wdBorderTop).LineStyle = wdLineStyleNone
                .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
                With .Borders
                    .DistanceFromTop = 1
                    .DistanceFromLeft = 4
                    .DistanceFromBottom = 1
                    .DistanceFromRight = 4
                    .Shadow = False
                End With
            End With
            ActiveDocument.Styles("Style1").LanguageID = wdEnglishUS
            Application.OrganizerCopy Source:=ActiveDocument.FullName, Destination:= _
                ActiveDocument.AttachedTemplate.FullName, Name:="", Object:= _
                wdOrganizerObjectStyles
        End Sub
        
        
        • #581230

          bow bow bow bow bow bow bow bow bow bow bow bow bow bow bow

          Passing style name arrays was what I needed. Your answer now puts me on the right track and the code will follow.
          Incidently you’ve also solved a couple of other questions that I hadn’t got around to looking at.

          One of the annoying things about programming is the inability to see a bug/solution thats stareing you right in the face.

          BTW you made a spelling mistake near the end, the correct line reads;

          ActiveDocument.Styles(“Style1”).LanguageID = wdEnglishAUS

          Many thanks
          Frank

          • #581246

            > solved a couple of other questions that I hadn’t got around to looking at.

            Which were/are?

            > Passing style name arrays was what I needed

            Oh no, we can do a lot better than that. You don’t need to restrict yourself to the style name. You can build a device (it’s easy, honest!) which will allow you to compare definitions.

            Didn’t you have one definition of the style on a local machine and one being handed down by The Gods?

            Think of a style as a collection of things with names like “Font”, ‘ParagraphFormat”, “LanguageId” and the like (see my quickly-recorded example above). If you look at each of those things, you’ll see, for example, that the “Font” thing can be broken down into things called “Name”, “Size”, “Bold” etc., and that each of those things can be treated like a string.

            Now imagine that each of those lexical atom things (Name, Size, Bold etc) is encoded as a string, and you catenate those little strings together to make a “Font” string; and likewise you catenate the “LeftIndent”, “RightIndent”, “SpaceBefore” strings to make a “ParagraphFormat” string.

            Now you catenate your “Font” string and your “ParagraphFormat” string and your “LanguageId” srtings to make a string called “Style1” (from my example).

            Each style definition can be encoded as a string, and then you catenate all your style strings to make a single string called, oh I dunno, “AllStyles”.

            Now imagine that you compare the Gods’ version of “AllStyles” to the local version of “AllStyles”. If both strings are equal, you don’t have a problem. But if the strings are unequal, you use strSplitStringAt to partition the top level string into a series of style strings, and compare them one by one to determine which style(s) differ. Then you continue to partition and compare to isolate specific differences. You can even build a nice dynamic tool that asks the user (but see “mean” below) “I see that you have changed the Font Size. Would you like me to set it to its default value?”

            I can see a very keen sleuthing tool here, snooping around the LAN seeing what gets changed as it gets changed, building a picture of what characteristics of Styles your users change. You may then get to ask yourself “How come 95% of the changes are to FontSize?” and other interesting questions.

            In particular, if you are building a device to detect changed styles and implement current versions, you will probably also be managing other aspects of your users environment, perhaps toolbars or macros or autotext entries.

            The mechanism you develop for catenating/decomposing styles will work for Toolbars, Autotext entries etc, will it not?

            I wouldn’t stop at Style Names. I’d go the whole hog.

            I’m starting to see a mechanism for graceful roll-back of a series of style definitions, maybe a dynamic archive of all style changes across the LAN. Why not?

            Mean:

            Or the message could do a Dilbert and read “I see that you have changed your style defintion again. I’m going to fire off an email to The Gods anyway, but before I do would you like to enter your comments in the text box marked ‘mitigating circumstances’ ?”

            Heh heh!

          • #581256

            Here you go (attached).

            “I want a full report on my desk by 9 a.m. tomorrow; give diagrams wherever possible” (VBG)

    Viewing 2 reply threads
    Reply To: Passing Arrays between Templates WordXP/97/VBA

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: