View Single Post
 
Old 08-12-2015, 09:51 PM
Jennifer Murphy's Avatar
Jennifer Murphy Jennifer Murphy is offline Windows XP Office 2007
Competent Performer
 
Join Date: Aug 2011
Location: Silicon Valley
Posts: 234
Jennifer Murphy is on a distinguished road
Default Outline macro screws up tab action

OK, I am clearly not smart enough to write VBA macros. I hope someone can help.

Sometime in the past few weeks, I noticed that the tab key was behaving oddly. For no apparent reason, it would change a Normal paragraph into a Heading style (2, 3, etc.). It didn't matter where the cursor was in the paragraph. It would not insert a tab character. This behavior was intermittent. I usually ended up copying a tab character from another document and pasting it where it needed it.

It happened again today and now I think I know what is causing it, but I cannot figure out why. Some time ago, I wrote a macro to handle what I had been doing manually. I write a lot of documents with several levels of headings. I often need to move sections around. I may want to move a Heading 1 up or a Heading 2 up within a Heading 1 section, etc.

The macro is supposed to check current view and the outline level of the line where the cursor is located. If it is in outline view, it will switch to Normal view and change the view setting to show all headings. If it is in Page or Normal view and the cursor is on a heading (not body text), it will switch to outline view and show heading to whatever level that line is. If the cursor is on a body text paragraph, it will do nothing.

Here's the code. I added line numbers for reference.

Code:
  01 Sub MyOutlineLevel()
  02
  03 'Call MsgBox("ActiveWindow.View = " & ActiveWindow.View)
  04
  05 Dim DocLayout   'The document layout (view)
  06 Dim ParOutline  'The paragraph outline level (1-9 + 10 (=Bodytext))
  07
  08 'DocLayout = ActiveWindow.View.Type
  09 DocLayout = ActiveWindow.View
  10 ParOutline = Selection.Paragraphs(1).OutlineLevel
  11
  12 Select Case DocLayout                 'Check the layout (view)
  13   Case wdNormalView, wdPrintView        'If Normal or Page (print) view,
  14     If ParOutline = 10 Then               'If BodyText paragraph,
  15       Exit Sub                              'Do nothing
  16     Else                                  'Else, must be a heading
  17       ActiveWindow.View.ShowHeading ParOutline  'Show headings only up to that level
  18     End If
  19     ActiveWindow.View.Type = wdOutlineView      'And switch to Outline view
  20   Case wdOutlineView                    'If Outline view,
  21     ActiveWindow.View.ShowAllHeadings     'Show everything
  22     ActiveWindow.View.Type = wdNormalView 'And switch back to Normal view
  23   Case Else                             'For any other view,
  24     Exit Sub                              'NOP
  25 End Select
I have discovered that the tab problem is being caused by this macro, but I cannot for the life of me figure out how. To test it, I created this simple document. Again, line numbers added for reference. (It just occurred to me that it would be very handy if the CODE tag offered the option of adding line numbers automatically.)

Code:
  01 heading 1
  02 body text
  03 body text
  04 body text
  05 heading 2
  06 body text
  07 body text
  08 body text
  09 heading 3
  10 body text
  11 body text
  12 body text
If I place the cursor on line 02 and press the tab key, the line is indented, as it should be,

If I place the cursor on line 01 and run the macro (I have it assigned to alt-o), the document is changed to Outline view with only the Heading 1 styles visible. If I run the macro again (alt-o), the document is changed back to Normal view with all heading levels visible.

If I then place the cursor on 02, the tab key will change 02 to a Heading 2 style. A second tab key will change it to Heading 3, and so on. If I place the cursor on any of the lines 06-08, after the Heading 2, they will get converted to a Heading 3. The tab key will convert any Body Text line to the next higher heading level of the last heading line above it.

But what is even more bewildering (to me) is what happens if I run the macro from a Body Text line, such as 02. This corrects the tab character so that it behaves as a tab key. If I trace the macro, it does not execute any commands other than the variable assignments.

Can someone please explain what is going on here?
Reply With Quote