Conflicting macros

L

LEU

I have 2 macros that are conflicting with each other. The first one deletes
the last page of the template if it’s not required. Then I just added the
second macro in the ThisDocument that opens up a form if the user tries to
edit the header. Now that I have done this the first macro no longer workers
because part of the macro is to delete the header from the last page. Is
there a way to work around this? My macros are as follows:

Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
'quit if active doc isn't attached to this template
If ActiveDocument.AttachedTemplate <> ThisDocument Then Exit Sub
'get out of the header if we're in it
Select Case Sel.StoryType
Case wdEvenPagesHeaderStory, wdFirstPageHeaderStory, wdPrimaryHeaderStory
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
frmTable.Show
Exit Sub

Case Else
If Sel.Range.InRange(ActiveDocument.Bookmarks("bktable").Range) Then
ActiveDocument.Bookmarks("bkbeforetbl").Select
frmTable.Show
End If
End Select
End Sub

Sub DeleteLastPage()
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Are you sure you want to delete Templates last page?"
Style = vbYesNo + vbCritical + vbDefaultButton2
Title = "Deletion Confirmation"
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then

Selection.EndKey unit:=wdStory
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
LinkToPrevious
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
ActiveWindow.ActivePane.SmallScroll Down:=-18
Selection.MoveUp unit:=wdScreen, Count:=1
ActiveWindow.ActivePane.VerticalPercentScrolled = 63
Selection.Delete unit:=wdCharacter, Count:=1
Selection.Delete unit:=wdCharacter, Count:=1
With Selection.PageSetup
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(0.75)
.BottomMargin = InchesToPoints(0.75)
.LeftMargin = InchesToPoints(0.8)
.RightMargin = InchesToPoints(0.8)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(8.5)
.PageHeight = InchesToPoints(11)
End With
Selection.Delete unit:=wdCharacter, Count:=6
Else
MyString = "No"
End If
End Sub

LEU
 
R

Russ

LEU,
Here's a reply I just gave to Robert who was trying to close a document
after invoking a userform from that document. Normally a userform opens by
default as modal and won't allow anything to be done in the background, so
to speak. Maybe you are running into the same problem.

(BigSnip)
Are you invoking the userform from the form that is open via the template?
That situation would explain why the form can't be closed until the userform
it invoked is closed.


When juggling multiple documents (possibly from multiple applications) it is
best to assign each document to a variable for reference, instead of relying
on ActiveDocument.

Dim objDocument As Document
Set objDocument = Documents.Add ...
....

objDocument.Close

If your users are not using Word97 or MacWord, you have the option to open a
userform as modal(vbModal)(the default) or modeless(vbModeless).

(snip)
Modal vs. Modeless:
The definition of a modal form is that it must be closed (hidden or
unloaded) before you can continue working with the rest of the application
(hence it is also shown on top; but this is rather a "side effect" so to
speak). For example, a dialog box is modal if it requires you to click OK or
Cancel before you can switch to another form or dialog box.

Modeless dialog boxes let you shift the focus between the dialog box and
another form without having to close the dialog box. You can continue to
work elsewhere in the current application while the dialog box is displayed.
Modeless dialog boxes are rare. From the Edit menu, the Find dialog box in
Visual Basic is an example of a modeless dialog box.
(snipped taken from the MSDN library)

Caution:a modeless userform allows the user to change non-userform windows
while the form is showing.

(snip)
In VBA, you can show a form as vbModal or vbModeless. In Visual Basic .NET,
the ShowDialog method is used to display a form modally; the Show method is
used to display a form non-modally. Note, however, that the form will
display non-modally, but when you click in the Word or Excel document, the
form moves to the background, which can be confusing to your users.
(/snip)

See also:
<http://support.microsoft.com/kb/171978/EN-US/>

So if you invoking the userform from the *document* that is open via the
template, use, i.e.:
MyUserForm.Show vbModeLess
Then that connection to the invoking document may be separated enough to
allow closing of that document while the userform is still in memory.

Since I don't have access to anything but Word97 or MacWord, I can't test if
you can change a form from modal to modeless, with a simple .hide in
between, or if once a form is in memory, only Windows API calls can change
the form window attributes.

But since a userform can't unload itself and reload itself from/to memory,
template code or a third party code, would have to do that, to change a
modal userform to a modeless userform just before you want to close the
invoking document and back to modal after closing the document.
(/BigSnip)
 
L

LEU

Hi Russ,

I think it's just how I am deleting my last page, because if I run the
following for any other page it gets deleted. If I try to use this macro for
the last page it gives my the following error: The requested member of the
collection does not exist.

Selection.Bookmarks("\Page").Range.Delete

LEU
 
H

Helmut Weber

Hi LEU,

try this:

Sub DeleteLastPage()
Dim lngPgs As Long
With ActiveDocument
lngPgs = .BuiltInDocumentProperties(wdPropertyPages)
If lngPgs > 1 Then
Selection.GoTo _
what:=wdGoToPage, _
which:=lngPgs
Selection.Bookmarks("\Page").Range.Delete
While .Characters.Last = Chr(13) And _
.Characters.Last.Previous = Chr(13)
.Characters.Last.Delete
Wend
End If
End With
Application.ScreenRefresh
' as the display gets confused otherwise here and now
End Sub

Note: The last paragraph mark in a doc is something special.
It is the end of doc mark.
In former times (DOS-Word) it was a diamond.
Nowerdays it is not displayed as something special anymore.
But it is something special.
It can not be deleted, unless it is preceeded
by another paragraph mark.

Whether that is real deletion, is debatable.

I like this kind of philosophical thoughts.
You cannot cut off the end of a rope without
creating a new end. Things get more complicated,
if the end equals the start. That's why you can't
delete all the contents of a doc.
There will always be a paragraph mark left,
or newly created on the spot. (?)

If you could, there would be no doc anymore.

HTH
 
H

Helmut Weber

....

by the way, this could become pretty complicated
if there is a table spanning
from a previous page to the last page.

So far, the code does not cover that case.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
L

LEU

Hi Helmut,

There are no tables that span from the previous page. I tried your macro but
it gave me the following error: Bad parameter. When I debug its at the
following lines:

Selection.GoTo _
what:=wdGoToPage, _
which:=lngPgs

My last page in the template is setup in landscape mode so drawings or
pictures can be insterted. I was trying to set this up so when a new document
is created and I do not need the landscape page I could just delete it. Would
it be easer to setup a seperate document called 'landscape page' and if
needed insert that into the document if needed? I tried doing this using the
insertfile, but that just inserts the text and none of the formating.

LEU
 
R

Russ

LEU,
I came up with this workaround.
Dim aDoc As Word.Document
Set aDoc = ActiveDocument

aDoc.Characters.Last.Previous.Select
Selection.Bookmarks("\Page").Range.Delete
 
R

Russ

LEU,
Did you use all four lines?
They work for me in a document full of text to delete the last page and
don't give a "error: The requested member of the collection does not exist."

If you don't want to create a reference with the first two lines, then this
should work as well.
ActiveDocument.Characters.Last.Previous.Select
Selection.Bookmarks("\Page").Range.Delete

Are you still getting the "error: The requested member of the collection
does not exist."?
 
H

Helmut Weber

Hi LEU

is has indeed become complicated,
and it is all about sections again,
(we've discussed this before in detail),
as a different orienation of the last page
requires a section break.

So you got at least two sections.
Getting rid of the contents of the last section,
which represents the last page,
it not a problem.
Just delete the last section in the doc.

Which leaves you with a section break
and an empty last page. Deleting this section break
is not a problem either, but then all the previous
pages will inherit the orientation of
the previously last section, and more,
will inherit all properties of pagesetup
of the previously last section.

So you got to remember all properties of the
second last section, which you want to preserve.

According to my present level of knowledge (as always).

In this sample, I just wanted to remember page orientation.

Sub DeleteLastPageX()
With ActiveDocument.Sections
.Last.PageSetup.Orientation = _
.First.PageSetup.Orientation
.Last.Range.Delete
.Parent.Characters.Last.Previous.Delete
End With
End Sub

Happy thinking.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
H

Helmut Weber

hmm...,

beware of running the macro a second time,
in this case, if there were only two sections before,
the last section is now equal to the first section,
which would delete all of the doc,
except the end of doc mark,
and then stop with an error, as there
would not be a .Parent.Characters.Last.Previous anymore.

But some error-handling using something like sections.count
would not be a problem, I think.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
L

LEU

Helmut & Russ,

Helmut, after reading your reply about section again the fix was easy. All I
had to do in ThisDocument was change one line in my macro and it work fine
now.

From:
Select Case Sel.StoryType
Case wdPrimaryHeaderStory, wdFirstPageHeaderStory, wdEvenPagesHeaderStory
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
frmTable.Show
Exit Sub

TO:
Select Case Sel.StoryType
Case wdPrimaryHeaderStory
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
frmTable.Show
Exit Sub

The last page header is not linked to the primary header and so making my
Case look only at the primary header, it lets me delete the last page header
without showing my form or giving me an error.

Thanks again to the both of you for all your support.

LEU
 
R

Russ

LEU,
I thought you were trying to delete the last page:
But you were trying to delete the last page header not the last page. That's
why I couldn't understand why, what I told you wasn't working.
 
L

LEU

Russ,

I thought I had this all figured out. I fixed this bug and then it causes a
problem somewhere else. Everything works good now for new documents created
from the template. But when I convert a document from WordPerfect to Word I
still have a problem. What is happening is I copy the first page of my
template into the converted document. In my template the first page header is
'First Page Header Section 1' but in the new document it becomes 'Head
Section1'. How do I keep it as 'First Page Header Section 1' so my macro
works OK?

LEU
 
R

Russ

LEU,
Copy and paste can lead to strange results depending on if you're using
special paste modes or including the last paragraph mark of a section which
holds a lot of information. See this link:
Russ,

I thought I had this all figured out. I fixed this bug and then it causes a
problem somewhere else. Everything works good now for new documents created
from the template. But when I convert a document from WordPerfect to Word I
still have a problem. What is happening is I copy the first page of my
template into the converted document. In my template the first page header is
'First Page Header Section 1' but in the new document it becomes 'Head
Section1'. How do I keep it as 'First Page Header Section 1' so my macro
works OK?

LEU
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top