Thursday, June 27, 2013

Obsession

I am obsessed with writing a macro. I've dealt with macros for years. I am not a programmer, though sometimes I wish I was just so I would know how to do what I want.

What I want is to write a macro to run in a doc that has all of our user guides inserted into it. For each heading, I want the macro to create a Word doc that only has the text that is under that section up to the point of the next heading. I’ve added a code to each paragraph to identify the client so I don’t get issues with files being named the same. Ultimately, using a Heading called “Registering Apples” as an example, I want a directory of docs like this:

Registering Apples – Client A.docx

Registering Apples – Client B.docx

Registering Apples – Client C.docx

After I get to that point, the next step is to analyze the content in each of those docs to end up with a single version of “Registering Apples” that can then be shared or referenced or “something” so that there’s not multiple versions floating around. That’s to be determined – may use Confluence or some other tool – don’t know. The analysis of the content I describe below has to be done regardless of the tool.

What we have now is in “Registering Apples – Client A.docx” the text may be this:
  1. Go here > there.
  2. Click that.
  3. Enter those things.
  4. Click Save.
In “Registering Apples – Client B.docx” the text may be this:
  1. Go here > there
  2. Click that
  3. Enter those things and click Save.
In “Registering Apples – Client C.docx” the text may be this:
  1. Go here
  2. Go there <=splits up the navigation that is only step 1 in Client A and Client B into two steps)
  3. Click that
  4. Enter those and click Save.
So, ultimately, I consolidate those and have:

Registering Apples
  1. Go here > there.
  2. Click that.
  3. Enter those things.
  4. Click Save.
And I write it once. And I use it in Client A, B, and C. And if there’s a change, I update it once and the update then goes to Client A, B, and C.

We have a section in one of our UGs that is included in 10 user guides. If there is an update to it, the update to the section is written once, then it gets copied / pasted into the 10 user guides. If there’s another change, that process is repeated. To paraphrase someone we both know “There must be a better way.” Doing what I want to do is impossible today, but if it could be done, it would radically change the way my job is performed. I had a 1:1 meeting with my manager and got the green light to make this happen, which is way cool!

Here’s the macro I’m running:

|

Sub Howard()

    Dim i As Integer, nSplits As Integer, nParas As Integer

    Dim BlockStarts() As Long  ' stores character positions where doc is to be split

    Dim CurOutputDoc As Document

    Dim CurBlock As Range

    Dim DocNames() As String     ' <---

    Dim Para As Paragraph

    Dim FilePath        As String

    Dim FileName()      As String

    With ActiveDocument

        nParas = .Paragraphs.Count

        ReDim BlockStarts(nParas)

        ReDim DocNames(nParas)   ' <---

        ' <--- Omit line here
        BlockStarts(0) = 0  ' Set lower boundary of first text block


        ' Set another split point at the start of each para with

        ' a Heading style (H1, H2, ..., H5)

        nSplits = 0

        For Each Para In .Paragraphs

            For i = 1 To 5

                If Para.Range.Start > 0 Then   ' Prevents re-processing first para

                    If Para.Style = "Heading " & i Then

                        nSplits = nSplits + 1

                        BlockStarts(nSplits) = Para.Range.Start

                        DocNames(nSplits) = Left(Para.Range.Text, Len(Para.Range.Text) - 1)   '<---

                        Exit For

                    End If

                End If

            Next i

        Next

  


        If nSplits = 0 Then

            MsgBox "Document contains no headings of the specified type"

       Else

            For i = 1 To nSplits

                ' Copy the text block between successive split points

                Set CurBlock = .Range(BlockStarts(i - 1), BlockStarts(i))

                CurBlock.Copy
                ' Paste into new doc, save & close
                Set CurOutputDoc = Documents.Add

                With CurOutputDoc

                    .Range.Paste

                    .SaveAs DocNames(i) & ".docx"   ' <---

                    .Close

                End With

            Next

        End If

    End With

End Sub

BUT it's not doing what I want!! I'm obsessed with figuring it out.

No comments: