Share via

Userform Command Button - Open Document

Anonymous
2012-02-20T14:23:11+00:00

I recorded the below macro and assigned it to open a document from a userform Command Button. 

Option Explicit

Private Sub CommandButton1_Click()

    ChangeFileOpenDirectory "C:\File Location"

    Documents.Open FileName:="""General Letter.dotx""", ConfirmConversions _

        :=False, ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _

        PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _

        WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""

End Sub

The document opens when I click the command button, but not before I receive this message:

Run-time error '5479':

"You cannot close Microsoft Office Word because a dialog box is open.  Click OK, switch to Word, and then close the dialog box."

I'm going to assume this is because the Userform is still open and needs to be closed before it can open the document I'm asking the command button to open. Is there a better macro I should use that won't return this message, or maybe something I could add to this one to prevent that error message?

Thanks!

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Jay Freedman 207.7K Reputation points Volunteer Moderator
2012-02-20T15:01:10+00:00

Although the macro recorder is useful for finding out what commands are (maybe) useful, there are lots of problems with what it produces. (For examples, see http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm.)

I don't have an explanation for the error message, but there are several other things that need attention in the recorded code, and there aren't any errors when those things are fixed.

First, there seems to be some confusion about documents vs. templates. The Documents.Open method is meant to open a document (*.doc, *.docx, or *.docm). If you use it to open a template (*.dotx or *.dotm), it will open the template itself for editing, which usually isn't what you want to do. If what you really want is a new document based on a template, use the Documents.Add method instead.

Second, it isn't necessary to use the ChangeFileOpenDirectory command; just include the path as part of the file name.

Third, when you do use one or the other of these methods, you can omit all of the arguments that have their default values.

Finally, if you want the userform to close when the button is clicked, the last command in the Click procedure should be Me.Hide.

So, if you want to open an existing document (*.docx), use

Private Sub CommandButton1_Click()

    Documents.Open FileName:="C:\File Location\General Letter.docx", AddToRecentFiles:=False

    Me.Hide

End Sub

If you want to create a new document based on a template (assuming the template is in the User Templates location), use

Private Sub CommandButton1_Click()

    Documents.Add Template:=Options.DefaultFilePath(wdUserTemplatesPath) & "\General Letter.dotx"

    Me.Hide

End Sub

[Notice the backslash at the start of the template's name, needed because the result of the DefaultFilePath doesn't end with a backslash.]

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2013-02-15T09:55:55+00:00

    I had the same problem and it seems to be connected with the way word detects whether there are any changes that need saving, particularly to the document1 that opens when word opens. So I just added a small snippet of code to make word think that there was something worth saving in the unsaved document.

    Selection.TypeText Text:=" "

    Selection.TypeParagraph

    Selection.TypeBackspace

    Selection.TypeBackspace

    After that my templates and documents worked fine with macros.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2012-02-20T15:57:34+00:00

    That's exactly what I needed to know!  I appreciate that you provided me with both an explanation and two ways to produce what I need.  I will eventually need something like this to open from a document, but at present I need to open a document based on a template.  Both of these will be useful to me.

    Thanks Jay!

    Was this answer helpful?

    0 comments No comments