I have a macro that inserts the filename into a footer. If I keep the macro code in the normal template, it works perfectly. However, if I put the code into a separate .DOTM file so that I can circulate it for others to use, I always get a debug error at ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument. Why? Does this command only work if run from normal.dotm? Although it may be too much, I’ve included my entire macro code below. The error occurs after the ReturnToDocEditing label. I create a bookmark called ReturnHere when the macro first runs. Then after the macro runs, I return to the document and return to bookmark ReturnHere. If I take out the SeekMainDoc code in a separate .DOTM, the macro runs but it leaves the footer editing window open, which I do not want. I thought SeekMainDoc would return me to the doc I was editing in my original normal view. Thoughts? Thanks!
-Rich Belthoff
Sub eDocsFooterNew() ‘ ‘ eDocs Filename Footer ‘ 2013-09-09; Corrected 2013-09-12; Modified 2015-10-29 ‘ ‘ Dim vFileNameFull As String Dim vTrackChanges Dim vFileNamePartial As String Dim vNum As Integer Dim vWhiteSpace Dim vChar ‘Checks for Track Changes (resets Track Changes at end of macro) If ActiveDocument.TrackRevisions = True Then ActiveDocument.TrackRevisions = False vTrackChanges = 1 Else vTrackChanges = 0 End If ‘Checks for display of white space If ActiveWindow.View.DisplayPageBoundaries = True Then vWhiteSpace = 1 Else vWhiteSpace = 0 End If ‘Ensures that viewing doc in page view If ActiveWindow.View.SplitSpecial wdPaneNone Then ActiveWindow.Panes(2).Close End If If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _ ActivePane.View.Type = wdOutlineView Or ActiveWindow.ActivePane.View.Type _ = wdMasterView Then ActiveWindow.ActivePane.View.Type = wdPageView End If ‘Set bookmark for return location after macro completes With ActiveDocument.Bookmarks .Add Range:=Selection.Range, Name:=”ReturnHere” .DefaultSorting = wdSortByName .ShowHidden = False End With ‘Moves to top of Doc, Inserts Hard Return & Sets Font Selection.HomeKey Unit:=wdStory Selection.TypeParagraph Selection.MoveUp Unit:=wdLine, Count:=1 Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft Selection.Style = ActiveDocument.Styles(“Normal”) ‘Inserts full eDocs filename Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _ “FILENAME * Upper “, PreserveFormatting:=True ‘ Changed to upper 2013-10-18 so that sub-versions use upper case letters ‘Selects filename (from right to left) (modified 2013-10-18) Selection.HomeKey Unit:=wdLine, Extend:=wdExtend Selection.Font.Name = “Arial” Selection.Font.Size = 8 vFileNameFull = Selection.Text ‘Assigns filename to variable Selection.TypeText Text:=vFileNameFull ‘Inserts filename as plain text Selection.HomeKey Unit:=wdLine ‘Move IP to beginning of line Selection.Delete Unit:=wdCharacter, Count:=6 ‘Deletes “edocs ” Selection.TypeText Text:=”eDocs ” ‘Inserts “eDocs ” instead Selection.MoveLeft Unit:=wdWord, Count:=1 ‘Moves cursor before eDocs Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend ‘Selects eDocs Selection.NoProofing = True ‘Turns off spellcheck for eDocs (won’t save in custom .DIC as eDocs) Selection.MoveRight Unit:=wdCharacter, Count:=1 ‘Moves cursor right to turn off selection If Selection.Type = wdSelectionIP Then ‘Selects text to next hyphen (hyphen before version) vFileNamePartial = Selection.MoveEndUntil(Cset:=”-“) Selection.MoveRight Unit:=wdCharacter, Count:=1 ‘ Moves IP to right (just before hyphen) End If Selection.Delete Unit:=wdCharacter, Count:=1 ‘ Deletes hypen before “v” (version) Selection.TypeText Text:=”v” ‘Enters a lower case “v” Modified 2013-10-18 Selection.Delete Unit:=wdCharacter, Count:=1 ‘ Deletes the upper case “V” If Selection.Type = wdSelectionIP Then ‘ Selects text to hyphen after version) vFileNamePartial = Selection.MoveEndUntil(Cset:=”-“) Selection.MoveRight Unit:=wdCharacter, Count:=1 ‘ Moves IP to last hyphen before filename End If If Selection.Type = wdSelectionIP Then ‘Selects text to right of IP & up to Tab vChar = Selection.MoveEndUntil(Cset:=vbCr, Count:=100) ‘ Extend selection to Tab End If Selection.Delete Unit:=wdCharacter, Count:=1 ‘ Deletes the remainder of the filename ‘Selects remaining filename, adds Bookmark, deletes filename & blank line at top of doc Selection.HomeKey Unit:=wdLine, Extend:=wdExtend vFileNamePartial = Selection.Text ‘Assigns filename to variable Selection.Cut Selection.Delete Unit:=wdCharacter, Count:=1 If ActiveDocument.Bookmarks.Exists(“eDocsName”) Then ActiveDocument.Bookmarks(“eDocsName”).Select Selection.Delete Unit:=wdCharacter, Count:=1 Selection.InsertAfter vFileNamePartial ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:=”eDocsName” Selection.Font.Name = “Arial” Selection.Font.Size = 8 GoTo ReturnToDocEditing Else GoTo NoBookmark End If NoBookmark: ‘ Need to determine what to do if there is no tab, which messes up some docs. ‘ Also, could use called functions because inserting the filename ‘ is the same code. ‘Moves insertion point to footer ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader If Selection.HeaderFooter.IsHeader = True Then ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter Else ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader End If ‘Selects text to first tab (including the WF date stamp if it exists) If Selection.Type = wdSelectionIP Then ‘Selects text to right of IP & up to Tab vChar = Selection.MoveEndUntil(Cset:=vbTab, Count:=100) ‘ Modified 2013-10-03; extend selection to Tab ‘Selection.MoveEndWhile Cset:=”abcdefghijklmnopqrstuvwxyWBFD0123456789~`!@#$%^&*()-_+{}|[];:,./? “, Count:=wdForward End If ‘Deletes the selected text Selection.TypeBackspace Selection.PasteAndFormat (wdFormatOriginalFormatting) ‘Pastes short filename Selection.HomeKey Unit:=wdLine, Extend:=wdExtend ‘Selects filename With ActiveDocument.Bookmarks ‘Assigns bookmark name .Add Range:=Selection.Range, Name:=”eDocsName” .DefaultSorting = wdSortByName .ShowHidden = False End With ReturnToDocEditing: ‘Returns insertion point to main doc ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument [COLOR=#ff0000]’PROBLEM OCCURS HERE![/COLOR] ‘Returns insertion point to last edited location bookmark Selection.GoTo What:=wdGoToBookmark, Name:=”ReturnHere” ActiveDocument.Bookmarks(“ReturnHere”).Delete With ActiveDocument.Bookmarks .DefaultSorting = wdSortByName .ShowHidden = False End With ‘Turns track changes back on if applicable If vTrackChanges = 1 Then ActiveDocument.TrackRevisions = True Else ActiveDocument.TrackRevisions = False End If ‘Turns white space display on or off as applicable If vWhiteSpace = 1 Then ActiveWindow.View.DisplayPageBoundaries = True Else ActiveWindow.View.DisplayPageBoundaries = False End If End Sub