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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close a Word VBA Form using a macro on different document

Status
Not open for further replies.

hkeiner

Technical User
Oct 13, 2010
9
US
I am trying to write a macro that that will close a form (fPointLinks) that displays automatically whenever certain Word documents are opened. I can not change the code in the Word document that contains the VBA form (becasue the document is created by others), so I must create my own macro on the Normal document to close the form. The name of the document with the form will vary each time I receive a new copy. The attached VBA screen shot provides an example of the documents involved.

I tried the below VBA command to close the form but I get an error that says that I am not using a valid object qualifier. What am I missing?

Code:
Public Sub ClosePointLinkForm()
    fPointLinks.hide
End Sub
 
1. fPointLinks.Hide does NOT close the userform. It hides it. It does not close (unload) the userform.

2. read up on Scope.

3. why not just click the Close button on the userform?

4. please elaborate on your situation. Normally, you would not be in another document if the userform is showing. Normally, if the userform has the focus you are not in another document. Is the userform Modal?


unknown
 
I tried to keep my question simple figuring that if I can learn how to manipulate a fPointLinks form from a macro on the Normal template (such as closing or hiding it), I am on my way to figuring out out how manipulate the form in other ways. Perhaps a fuller expanation is better.

This is the situation... My department uses a standard workpaper document (a Word 2007 file) with "features" the require the use of a VBA form and some VBA code associated with the VBA form. To use a feature, a user clicks an icon on the QAT to dispay the form, clicks one of several radio buttons on the form to make a choice, and then clicks an OK button on the form to cause the appropriate VBA sub feature) to run. This process requires three "clicks" every time a user wants to use a particulare feature.

My desire is to add separate icons to the QAT for each feature so that the user only has to click once to cause a sub on the form to run.

These are my constraints/abilities:
- The name of the standard Word document with the form and VBA code is not constant or predicatable and will be renamed by the user.
- The VBA code on the standard Word document CAN NOT be changed. The template for this document was developed by a software vendor and the Word documents (clones) are created via the vendor's own application.
- I can add vba code to the normal template of each user.
 
Said another way, my macro would (1) open the fPointLinks form, (2) mimic the selection of a radio button option, (3) mimic the clicking on the OK button on the form, (4) and close/hide the form.
 
Thanks for posting the other thread. I answered the OP (I hope) fully in the VBAExpress thread.

Except....

"Said another way, my macro would (1) open the fPointLinks form, (2) mimic the selection of a radio button option, (3) mimic the clicking on the OK button on the form, (4) and close/hide the form. "

This is NOT what you first posted!
I am trying to write a macro that that will close a form (fPointLinks) that displays automatically whenever certain Word documents are opened

Now you are asking about showing the userformn in the first place???? This is a LOT different from trying to close a userform that has been opened automatically.

Tell you what. When you have figured out - exactly - what you really want to do, post that and we'll see what can be done.

I have to tell you that: (1) open the fPointLinks form, (2) mimic the selection of a radio button option, (3) mimic the clicking on the OK button on the form, (4) and close/hide the form. "

seems very odd to me. Mimic the selection????

If you are going to automate user input - so there is no user? - then why bother opening the userform? Just do the actions.


unknown
 
In particular, please expand on:

"To use a feature, a user clicks an icon on the QAT to dispay the form, clicks one of several radio buttons on the form to make a choice, and then clicks an OK button on the form to cause the appropriate VBA sub feature) to run. This process requires three "clicks" every time a user wants to use a particulare feature. "

IF - and only IF - those "processes" do not use elements from the userform, then you can do this fairly easily. IF - and only IF - those actions are simply actions on the document, then simple! make the QAT icon fire those actions.




unknown
 
I am trying to write a macro that that will close a form (fPointLinks) that displays automatically whenever certain Word documents are opened.

This was one of the things I was trying to automate. The Word document automatically pops up the userform whenever the document is opened, using an AutoOpen macro on the Document. The user often does not want to use the user form and must click on the cancel button to close/hide it for the work to proceed. I wanted to add my own AutoOpen macro to the Normal template that would to close the userform without the user haveing to do so manually. If the user does want to use the userform, he can display it using a button on the QAT that opens the user form.

The other thing I was trying to do is to create a macro on the normal template that does all the steps when the user form is needed (ie. (1) open the fPointLinks form, (2) mimic the selection of a radio button option, (3) mimic the clicking on the OK button on the form, (4) and close/hide the form.)

Two different things I was trying to do. That is why they were asked in separate posts originally.

I used the term "mimic" because I want my macro to do what the user currently does manually on the form. Perhaps a bad choice of words but it seemed appropriate at the time.
 
I used the term "mimic" because I want my macro to do what the user currently does manually on the form. "

I do not think you are reading your own posts (or mine) very well.

The user does things manually. OK. So is your code some sort of mind reader? HOW does it know what the user is going to manually do? Hmmmm? HOW is your code supposed to know to do X actions, as opposed to Y actions?

And if it could, then why - as I asked - bother with the userform at all?

This seems very badly designed. WHY does the userform open automatically? If it is not needed, then this is simply poor design.

Read this (your words): "I wanted to add my own AutoOpen macro to the Normal template that would to close the userform without the user haveing to do so manually. "

THAT reads as if you are stating: I want an automatic closing of the automatic opening of the userform. So as soon as th euserform is opened (automatically) I want it to be closed, also automatically.

If this is what you are saying...say so! Post EXACTLY what you want to happen.


unknown
 
Yes, the form is poorly designed. The Word documents (containing the form and related VBA code) are created by a document management system and I have no control over the form design or VBA code on these documents. The only thing I can hope to do is to add some code to the Normal template to make the documents more user friendly for myself. The user friendly features would be:

1)Automatically Close/Hide/Kill the form when it pops up automatially each time the the Word document is opened.

2)Create a QAT icon (macro) that will Open/show the form when it is desired.

3)Create a QAT icon (macro) that will run code on the Word document as if I had selected a specific radio button option on the form and then clicked OK. There is one radio button option is is most often used by me.

If my simple example of hiding the form using code on the Normal template is not possible considering my constraints(as discussed further in my crosspost), then probably none of the other user friendly features will be possible either.



 
I have explained how it is possible to do #1. This is on the other thread, but I will reiterate.

In Normal, create a standard module. In it, write a simple close procedure for the userform.
Code:
Sub KillTheForm() 
    Unload frmPointLinks 
End Sub

Now Export the module to a .bas file. I named it KillTheDamnForm.

Now, in Normal, write a procedure that will:

1. Import the .bas file as a code module into the ActiveDocument.

2. execute the KillTheForm procedure.
Code:
Sub ImportThenExecute() 
    Dim Doc As Document 
    Set Doc = ActiveDocument 
    Doc.VBProject.VBComponents.Import "c:\KillTheDamnForm.bas" 
    Application.Run Macroname:="doc!KillTheDamnForm.KillTheForm" 
End Sub

It is a matter of Scope. The userform comes from your ActiveDocument. It must be closed by the ActiveDocument. Thus, you add a procedure to do that (via an exported and then imported file).

#2 - can be done the same way. A simple .Show procedure for the userform.

#3 - repeating my post of Oct 14, IF those actions are actions on the document that do NOT require elements from the userform, then just do the actions. if they DO require elements from the userform...then obviously you need the userform.


unknown
 
[tt]
ROFL
------------
KTDF
[/tt]

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Actually....

"1)Automatically Close/Hide/Kill the form when it pops up automatially each time the the Word document is opened."

Importing a .bas file will NOT do it automatically. The only way to have action performed automatically when a document is opened is to have the actions in the DocumentOpen event. Obviously the code to close the userform is not there. The code to SHOW the userform is.

So, I stand (self) corrected. However, from your opening sentence:

"I am trying to write a macro that that will close a form (fPointLinks) that displays automatically whenever certain Word documents are opened"

My suggestion CAN do this. But not automatically. If the bloody document management system creates documents with automatic userform display, there is nothing you can do about it, except of course change things. But you state this is not possible.

Oh, and shoot the developers of that doc system.

Having documents always have a userform display on opening is - to put it mildly - a dismal design unless that is what is required. It sounds like they put it there with no regard for users. Which is not unusual sadly.

So, IF these documents always display the userform when they are opened, there is no way to prevent this without changing the DocumentOpen event of the document.


unknown
 
Having documents always have a userform display on opening is - to put it mildly - a dismal design unless that is what is required. It sounds like they put it there with no regard for users. Which is not unusual sadly.

I can only presume that the programers thought it simpler and more user friendly if the userform is automatically visible to all users all the time, requireing them only to close the form if it is not needed. While I can easily add macros to my QAT (for example, to open the userform when needed) most users of these Word documents would not do that themselves and thus would not be able to readily use the userform.

By the way, I have made some progress. While I am not allowed add any VBA code to the Word document or user form, I was able to add some macros to my Normal template that run subs on the Word document that correspond to choices on the userform. The silver bullet was the "Application.Run" command that you suggested earlier. Below is an example of what I did. Thanks for your help.

Dim Doc As Document
Dim doc As Document
Dim TemplateName As String
TemplateName = ActiveDocument.Name
Set doc = ActiveDocument
Application.Run Macroname:=_
"doc.mPointLinksFields.ReplaceTokens"
 
Ignore the duplicate lines in the example code. It was a duplicate paste.
 
That is an example of what I was talking about. You can execute procedures (instructions) if you fully qualify it.

BTW: you CAN pass parameters (arguments) to a procedure with Run, if that is helpful.


unknown
 
If the automation macro only opens the userform, you can bypass it by holding SHIFT key when opening the document.
Another way is to create your own procedure that blocks automation macros and opens fole:
Code:
Sub OpenDocWithoutAutoMacros()
With Application
    .WordBasic.DisableAutoMacros 1
    With .Dialogs(wdDialogFileOpen)
        .Name = "*.*"
        .Show
    End With
    .WordBasic.DisableAutoMacros 0
End With
End Sub
It downgrades VBA to old WordBasic, but there is no other way using newer objects.

combo
 
Thanks guys!!! The advice and tips I got in this thread helped me get things working OK.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top