Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form in Word is interfering with Page Setup code

Status
Not open for further replies.

Jtorres13

Technical User
Nov 11, 2006
162
US
I have a macro that cleans documents (removes page breaks, section breaks, special characters, textboxes, etc.)

When the user runs the macro, they first get a form for them to choose an option or not. The option is to delete pictures and frames or not. It's a checkbox.

If they check the box the macro runs compeltely including the code to remove those objects. If they don't check it, the macro runs partial code.

The problem is that when it gets to the part where it needs to make the Page Setup Portrait, it pops the form again and stops the macro. When I click OK to keep running, the form hangs and doesn't want to respond. Here's the code without the meaningless bulk .

It appears as if the form continues to be the ACTIVE DOCUMENT. It doesn't matter if I hide the form before or after running the code.

Any ideas? Here's the code.

Private Sub cmdOK_Click()
' Verify if the user wants to remove textboxes, frames
' and pictures (signatures and handwritten comments)

If chkRemoveSignatures.Value = True Then
'Run the entire macro with
'additional code to delete mentioned items
‘<<Some code goes here>>

With activedocument.PageSetup
.Orientation = wdOrientPortrait
End With

‘<<Other code goes here>>

'Back to the top of the page
Selection.WholeStory
Selection.StartOf
frmCleanupDocs.Hide
Else
'If FALSE, then run the entire macro without
'the additional code

'Change Page Setup to Portrait
With activedocument.PageSetup
.Orientation = wdOrientPortrait
Selection.WholeStory
Selection.StartOf
frmCleanupDocs.Hide
End Sub
 
For me you have, at least, a syntax error: missing End With
 
Ah yes, I missed that when shoertening the code. But it's there int he full code. So, that's not it. For some reason the form is the active document and I cant get Word to select the document I'm cleaning to be the Active Document.
 
Why not using the proper element of the Application.Documents collection instead of the possibly unknown ActiveDocument ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Yes, much better!

Use Document objects. That way you can make explicit instructions. For example:
Code:
Dim FormDoc As Document
Dim WorkingDoc As Document
' assuming the current active doc has the form
Set FormDoc = ActiveDocument
MyForm.Show ' or whatever name
' how are you opening the doc to work on?
' assuming it is NOT the document with the userform
Set WorkingDoc = Documents.Open Filename:="[i]working doc[/i]"
Now you can use WorkingDoc as the document (object) and do whatever you are doing.

If the document to be worked on is already open, then as PH suggests, use the Documents collection.
Code:
Set WorkingDoc = Documents([i]document name[/i])

faq219-2884

Gerry
My paintings and sculpture
 
I can't use the Documents object because I don't know the name of the document. They could have more than one document open at the same time and are switching back and forth, multi-tasking. Why can't I hide or disable the form while the code runs?
 
I don't know the name of the document
But you know the name of the document that has the form, don't you ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Please explain more, because you are not being clear. Step-by-step please answer these questions.

1. Is the document being actioned the one that contains the userform?

2. HOW are you determining which document to action on?
They could have more than one document open at the same time and are switching back and forth, multi-tasking.
No...they are not. If you are running the userform, then they can't do that. Userforms are not mutli-tasking. When they have focus...they have focus until instructions are given to switch focus.

They can execute actions on documents that do not have focus (that is, they are NOT the active document). Making a document active is not required to perform action on it. It DOES have to be open though.
Why can't I hide or disable the form while the code runs?
Excuse me? How would code run from a userform while it is disabled??

Yes, you can have executing code from a userform that is Hidden.

From your original post:
It appears as if the form continues to be the ACTIVE DOCUMENT.
Ummm, the userform is NEVER the active document. The userform is a userform, not a document at all.

A userform is called by the .Show method. That userform can be in the active document, or in another open but NOT active document. It can then action a different open but not active document. While a userform most commonly actions the active document, it certainly does not have to.

This is one of the main reason for having your userforms, and code, in an independent template. Independent of the active document. That way, you can action the active document (if you want) or another document (if you want).

BTW: why are you using: frmCleanupDocs.Hide?

If you are done, you should be using Unload.

BTW2:
Code:
Selection.WholeStory
Selection.StartOf[/quote]is redundant code.  Actually, it is excessive code.


What that does is Select the entire document, then (doing absolutely nothing with the Selection) unselect it leaving the Selection point at the start of the document.
[code]Selection.HomeKey Unit:=wdStory
does the same thing.

I just ran a test on a document (in Landscape) with various pictures and frames. I made a userform with a checkbox. A commandbutton when clicked checks to see if the checkbox is True, if it is, it removed the pictures and frames, and made the document Portrait. When finished, it Unloaded itself. It worked fine. No hangs.

I am sure we can help to get this going for you. Please try and describe exactly what is happening more.

faq219-2884

Gerry
My paintings and sculpture
 
1. I think the confusion is in using the term "form."

The form I'm referring to is a control in VBA, not a user form you create in the document. So, the form is a control or "document" in itself. I think this is why it keeps bringing up the form when my code refers to "ActiveDocument". It thinks I want to change the form to Portrait orientation, and it crashes.

2. From any document, they can run a macro that opens the form frmCleanUPDocs.Show. They click a check box or not and press OK. The code is in the OK button. So, it works from any document because the form is NOT in a specific document. It becomes the active document as soon as it comes up. I need to learn how to switch to the document I was just in before I clicked showed the form.

3.
Excuse me? How would code run from a userform while it is disabled??
Yes, you can have executing code from a userform that is Hidden.

Will hiding the form help me gain control over the docuemtn again, making it the active document? I think this is what's failing.

4.
BTW: why are you using: frmCleanupDocs.Hide?

If you are done, you should be using Unload.

Thanks, I'll fix that. I'm not very good at this, as you can see.

 
Um, yes, there certainly is confusion on the term "form". Unfortunately this is common. Also, unfortunately, you seem to perpetuating it.

A userform is what used to be called a dialog. It is independent of the document.
The form I'm referring to is a control in VBA, not a user form you create in the document. So, the form is a control or "document" in itself.
This is twisted.


A userform IS a control in VBA. There is, technically speaking, no such thing as a user form created in the document. A document can be designed in a specific look - this is (unfortunately) what a lot of people call a "form".
2. From any document, they can run a macro that opens the form frmCleanUPDocs.Show. They click a check box or not and press OK. The code is in the OK button. So, it works from any document because the form is NOT in a specific document. It becomes the active document as soon as it comes up. I need to learn how to switch to the document I was just in before I clicked showed the form.
OK. You have a userform frmCleanUpDocs. I am assuming that this userform is in Normal.dot. It has to be IN some file. I have asked where it is. You do not answer. Why?

I am assuming it is normal.dot because (again unfortunately) that is where people put things, and that is where Word tends to default things. However, a userform MUST be created, and therefore it MUST be created in a file. WHAT file is up to the person creating it. I also think it is normal.dot because you mention that they can run a macro to Show the userform from any document.

That means it is in Normal.dot, the active document, or a loaded global template.
because the form is NOT in a specific document.
WRONG! Sorry, but it IS in a specifc document. It has to be. Userforms do not float around by themselves. They are IN a document. Probably, it is in normal.dot.

A userform called from normal.dot will take the current active document to be....the active document. How could it be anything else. You have an active document. You call the userform. The active document is still the active document.
It becomes the active document as soon as it comes up.
I will repeat this. No, it does not. The userform is a userform, NOT a document. It does NOT "become" the active document when it comes up.

I will say it again. A userform can action ANY document that is open - whether it is active, or not active. If the code states "ActiveDocument", then it will action....the active document. This is why Ph, and I, suggested using the Document collection. It makes things explicit.
I need to learn how to switch to the document I was just in before I clicked showed the form.
Again, use the Document collection.

This is going in circles.

1. Find out, and clearly state where is the userform.
2. POST SOME CODE. Show us how you are calling the userform.

If you want to use the userform to action the document you are in (the active document), then you can use ActiveDocument. However, you can ALSO be very explicit. That is, make the active document a Document object (say, ThisDoc) before calling the userform. Now you can tell the userform to action ThisDoc...meaning whatever document you made ThisDoc. It can be active, or not. It does not matter. The code will action ThisDoc whether it is active...or not.

As I stated, I made a userfrom that did what it seems you are doing (removing, or not, images, frames, depending on a checkbox, then changing Orientation) with no problems whatsoever.

Also, again, I do not know why you are doing Hide.

faq219-2884

Gerry
My paintings and sculpture
 
I had to abandon the original design of the macro and go without the form, two macros, one to delete everything including pictures and textboxes and the other without these things. Sorry, but I don't think my skills are up there yet. Thanks everyone for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top