#1
|
||||
|
||||
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 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 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? |
#2
|
||||
|
||||
The reason you are getting the style change is because of your autoformat as you type settings. With Word 2007 > Office Button > Word Options > Proofing > Autocorrect Options > AutoFormat as You type. Uncheck all those items that you don't require i.e. most of them!
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
||||
|
||||
Quote:
I decided to uncheck them one at a time to see which, if any, of them made a difference. I've attached a screen shot of my settings as they were before unchecking any. As I perused the list, I noticed that the only one that explicitly mentions tabs is the next to last one: "Set left- and first-indent with tabs and backspaces". So I unchecked that one and reran the test. The "bad tab" behavior did not occur. This appears to have fixed the problem. Can I conclude that it is this one setting that is causing the problem? But I am still puzzled by this. As far as I know, I have had this setting checked forever. I don't recall ever setting it either way (on or off). I have been using Word 2007 for about 10 years and I use Word all the time (thousands of documents). I am quite certain that this "bad tab" behavior never occurred until I wrote the MyOutlineLevel macro and then only after running the macro. Doesn't that suggest that it also has something to do with this macro? |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro to Expand Outline Heading in Outline View? | Clueless in Seattle | Word VBA | 1 | 05-04-2015 05:44 PM |
Creating a new document from a template screws up fields! | Cosmo | Word | 6 | 08-07-2014 05:16 AM |
Macro to toggle outline level | Jennifer Murphy | Word VBA | 3 | 01-22-2014 11:22 PM |
Macro to toggle outline level | Jennifer Murphy | Word VBA | 0 | 07-08-2013 08:20 AM |
How to link an action on one slide on a powerpoint with the same action on another | slevinmj | PowerPoint | 0 | 02-24-2011 05:38 AM |