Can't get the document to close

All,

The following small macro shouyld close the document if Cancel is selected, 
but doesn't.

Sub AutoOpen()
   Dim oDoc As Document
   Set oDoc = ActiveDocument
      
   With oDoc
   Dialogs(wdDialogFileSaveAs).Show
    If Response = vbCancel Then
    oDoc.Close
    ElseIf vbKeyEscape = True Then
    Dialogs(wdDialogFileSaveAs).Close
    End If
    End With
End Sub


Any advive would be appreciated.

Regards,
-- 
Brian McCaffery
0
Utf
4/15/2010 10:10:02 AM
word.vba.general 1023 articles. 0 followers. Follow

2 Replies
1265 Views

Similar Articles

[PageSpeed] 3

Hi Brian,

Sorry to say, that code shows you have much to learn about VBA and Word. 
First starters, I recommend that you read the VBA help topic named 
"Displaying Built-in Word Dialog Boxes". If you can't find it in the Help, 
it's online at 
http://msdn.microsoft.com/en-us/library/aa157606(office.10).aspx. Also read 
http://word.mvps.org/FAQs/MacrosVBA/WordDlgHelp.htm.

When the statement "Dialogs(wdDialogFileSaveAs).Show" executes, the dialog 
closes itself because the user has clicked something. There is no such thing 
as "Dialogs(wdDialogFileSaveAs).Close".

You used "Response", apparently expecting it to somehow contain the return 
value of the dialog. But you never declared it (so VBA defaults to assuming 
that it's a Variant variable with the value 0) and you never assigned any 
value to it. Therefore, the value of Response can never be equal to vbCancel 
(which is a built-in constant with the value 2). That's why your document 
isn't closing.

If you read the help topic about the Show method, you'll find that it 
returns a value with the data type of Long, and that it usually returns one 
of three values: 0 if the Cancel button was pressed, -1 if the OK button was 
pressed, or -2 if the Close button (if there is one in the dialog) was 
pressed. You're interested only in whether or not the user clicked the OK 
button. So you must assign the return value of the Show method to the 
Response variable (declared as Long) and test for whether it equals -1.

Additionally, the statement "ElseIf vbKeyEscape = True Then" is complete 
nonsense. vbKeyEscape is a built-in constant with the value 27, while True 
is another built-in constant with the value -1. So that comparison will also 
always be false.

Strictly speaking, for a tiny macro like this, assigning ActiveDocument to 
the variable oDoc and using that variable once is overkill. But since it's 
generally good practice (in larger macros) to use such a variable, I won't 
tell you to take that out.

A working version of your macro is this:

Sub AutoOpen()
    Dim Response As Long
    Dim oDoc As Document
    Set oDoc = ActiveDocument

    Response = Dialogs(wdDialogFileSaveAs).Show
    If Response <> -1 Then
        oDoc.Close
    End If
End Sub

-- 
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so 
all may benefit.

Brian wrote:
> All,
>
> The following small macro shouyld close the document if Cancel is
> selected, but doesn't.
>
> Sub AutoOpen()
>   Dim oDoc As Document
>   Set oDoc = ActiveDocument
>
>   With oDoc
>   Dialogs(wdDialogFileSaveAs).Show
>    If Response = vbCancel Then
>    oDoc.Close
>    ElseIf vbKeyEscape = True Then
>    Dialogs(wdDialogFileSaveAs).Close
>    End If
>    End With
> End Sub
>
>
> Any advive would be appreciated.
>
> Regards, 


0
Jay
4/15/2010 1:57:23 PM
Thank you for the insight. No need to say sorry for pointing out the obvious. 
:-)

Kind regards,
-- 
Brian McCaffery


"Jay Freedman" wrote:

> Hi Brian,
> 
> Sorry to say, that code shows you have much to learn about VBA and Word. 
> First starters, I recommend that you read the VBA help topic named 
> "Displaying Built-in Word Dialog Boxes". If you can't find it in the Help, 
> it's online at 
> http://msdn.microsoft.com/en-us/library/aa157606(office.10).aspx. Also read 
> http://word.mvps.org/FAQs/MacrosVBA/WordDlgHelp.htm.
> 
> When the statement "Dialogs(wdDialogFileSaveAs).Show" executes, the dialog 
> closes itself because the user has clicked something. There is no such thing 
> as "Dialogs(wdDialogFileSaveAs).Close".
> 
> You used "Response", apparently expecting it to somehow contain the return 
> value of the dialog. But you never declared it (so VBA defaults to assuming 
> that it's a Variant variable with the value 0) and you never assigned any 
> value to it. Therefore, the value of Response can never be equal to vbCancel 
> (which is a built-in constant with the value 2). That's why your document 
> isn't closing.
> 
> If you read the help topic about the Show method, you'll find that it 
> returns a value with the data type of Long, and that it usually returns one 
> of three values: 0 if the Cancel button was pressed, -1 if the OK button was 
> pressed, or -2 if the Close button (if there is one in the dialog) was 
> pressed. You're interested only in whether or not the user clicked the OK 
> button. So you must assign the return value of the Show method to the 
> Response variable (declared as Long) and test for whether it equals -1.
> 
> Additionally, the statement "ElseIf vbKeyEscape = True Then" is complete 
> nonsense. vbKeyEscape is a built-in constant with the value 27, while True 
> is another built-in constant with the value -1. So that comparison will also 
> always be false.
> 
> Strictly speaking, for a tiny macro like this, assigning ActiveDocument to 
> the variable oDoc and using that variable once is overkill. But since it's 
> generally good practice (in larger macros) to use such a variable, I won't 
> tell you to take that out.
> 
> A working version of your macro is this:
> 
> Sub AutoOpen()
>     Dim Response As Long
>     Dim oDoc As Document
>     Set oDoc = ActiveDocument
> 
>     Response = Dialogs(wdDialogFileSaveAs).Show
>     If Response <> -1 Then
>         oDoc.Close
>     End If
> End Sub
> 
> -- 
> Regards,
> Jay Freedman
> Microsoft Word MVP        FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so 
> all may benefit.
> 
> Brian wrote:
> > All,
> >
> > The following small macro shouyld close the document if Cancel is
> > selected, but doesn't.
> >
> > Sub AutoOpen()
> >   Dim oDoc As Document
> >   Set oDoc = ActiveDocument
> >
> >   With oDoc
> >   Dialogs(wdDialogFileSaveAs).Show
> >    If Response = vbCancel Then
> >    oDoc.Close
> >    ElseIf vbKeyEscape = True Then
> >    Dialogs(wdDialogFileSaveAs).Close
> >    End If
> >    End With
> > End Sub
> >
> >
> > Any advive would be appreciated.
> >
> > Regards, 
> 
> 
> .
> 
0
Utf
4/21/2010 12:35:01 PM
Reply:

Similar Artilces:

i get error 404 not found when trying to download
I'm trying to download nokia pc suite to windows xp to upload gallery onto the computer but get error message404 not found - how can i download this pc suite please -- Krysy Albutt Krysy Albutt wrote: > I'm trying to download nokia pc suite to windows xp to upload gallery > onto the computer but get error message404 not found - how can i > download this pc suite please Care to enlighten us how this pertains to: - 1) Microsoft 2) Publisher 3) Windows If you're having problems downloading from the *NOKIA* site where do you think you should be directing your ...

How to get Calendar to send me reminders
I have changed my e-mail address but for some reason I cannot get Calendar to send reminders to my new address - I think I have carried out all that is necessary to effect the changover but I am not getting reminders sent. Can anyone help please? Is the Calendar Live ID based and accessible at Calendar.live.com ? -- ....winston ms-mvp mail "Dabardevon" wrote in message = news:FDC2B3B2-EDE4-41B1-BFC4-C0F9ED59CFB9@microsoft.com... I have changed my e-mail address but for some reason I cannot get = Calendar to send reminders to my new address - I think I have...

Some recipients get the attachment, some get winmail.dat
This one has me baffled. We are using Exchange 2000 and Outlook 2000 and have been for 8 months. No previous problems with attachments (.doc, .xls, .jpg). About 2 weeks ago, we started hearing from certain recipients that the attachment wasn't arriving; instead they were getting an unreadable file called winmail.dat. MS says that this is because the sender is using Rich Text to format the email. We have changed the senders to use Plain Text and still have the same problem. If the email is sent to several recipients, some will get the attachment fine, some will not. This didn'...

How to get the CScrollBar's Handle in CTreeCtrl?
Hey,All: I thought I have a problem when I trid to get the CScrollBar's handle from my CTreeCtrl extended handle. Can anybody tell me whether I can get the handle of the CScrollBar from the CTreeCtrl? Thanks in advance! Carl :-| Carl wrote: > Hey,All: > > I thought I have a problem when I trid to get the CScrollBar's handle from > my CTreeCtrl extended handle. > Can anybody tell me whether I can get the handle of the CScrollBar from the > CTreeCtrl? > > Thanks in advance! > Carl > :-| > > Sorry, it is not that simple. The scroll bars that...

A Crystal Beginner
I am using Crystal 9.2.4.77 and have loaded the Crystal enhancements. Now I want to do against the CRM database. What do I do? I thought I would create a data source to the CRM SQLserver, which I did, then connect to it through Crystal Reports. That works, but when I drill down on the AAA_MSCRM database to choose my tables, it shows me dbo as the only option (where did this come from??) and under that are just a few generic tables. No CRM tables. Something's not quite right. What am I missing? file - log on to aps server "KBLawson" <klawson@aetinc.com> wrote in ...

Cannot get fast forward or fast reverse to work with WMP 11
I am running Vista Home Premium on a HP G 217-EM Notebook which comes pre-installed with Windows Media Player 11. When I play cds and try to fast forward or fast reverse by keeping the fast forward or fast reverse buttons constantly depressed nothing happens. Why is this, and can anyone come up with a solution? Many thanks This doesn't work on my system either, so it's probably just a limitation of WMP. Fast forward and fast reverse should work fine when you rip the CD's to MP3 or WMA files, and play these files instead. Regards -- Tim De Baets http://www....

MDI document creation sequence
Hello, I'm trying to figure out what the correct sequence is when a new document with it's view is being created and I seem to be stuck right after the document manager class calls OpenDocumentFile from the document template class. Who and when exactly creates the document class and document view class object? What about the CMDIChildWnd? Step 1: CWinApp calls it's CDocManager->CreateNewDocument() Step 2: CDocManager->CreateNewDocument() calls CDocTemplate- >OpenDocumentFile(NULL) Then I'm lost in the code. I'm trying to create a simple UML sequence diagram of ho...

Get paid while you get online!!
I found a great Internet company - Cashfiesta.com - that has created a product everyone can benefit from. They pay you while you work or play on your computer. All you need to do is keep their software - the FiestaBar=99 - active while you are online. They even pay you when your friends are using their computers. Unlike other companies, Cashfiesta gives you control over how much money you earn. They have an individual payrate based on the number of Special Offers you sign up for. As some of these offers are free, you can increase your payrate up to 33 times without spending a penny. It's...

Outlook won't close
I'm running Outlook2000 under WinXP and when I close Outlook it either hangs in memoey and continues to run or the mail icon stays up in the taskbar. I then have to open the task manager and kill it there. I've installed the latest Office service packs and searched the knowlege base but cannot find a similar problem posted. I believe I've had this problem as far back as the Win98 days. Anyone have an idea how i can fix this? I'm betting it's just taking so long you're assuming it's locked when it's simply taking a long time. I assume when you close it i...

Hyperlinks in Office documents
WHen I insert hyperlinks in a local document, I browse to the (other local) document that I want to link to. For some reason, those links periodically turn into c:\. . . applicata_data\office, etc. hyperlinks and they break. Does anyone know why this happens and how to prevent it from happening? Thx Hi Joyce, what did the hyperlink address look like before and after -- complete link. how did you enter the hyperlink what version of Excel You can only have one workbook with the same file name (example.xls) open at time. --- HTH, David McRitchie, Microsoft MVP - Excel [site changed ...

How can I make a multilevel outline document in Publisher?
I'd like to at least be able to import/copy & paste the multi-level outline from Word into Publisher but it will not "hold" the outline format. Does anyone know a way to either get Publisher to allow me to create a multi-level outline or at least paste one in from Word, including all the formatting? Any tips would be appreciated. Thanks. ...

Spinning Wait Cursor on Editing Word Documents
Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel Over the past few days in nearly all of my documents, when I begin editing old documents, or editing new documents, I get the Spinning Wait Cursor which lasts indefinitely (5min+). I have run through Disk and Permission Verifies and repairs and the problem persists. <br><br>Recommendations? Make sure that both Office & OS X are fully updated. It's pointless to chase symptoms if all updates haven't been done. You may also be experiencing a font conflict -- quite prevalent with Sno...

Can't get auto-update (update) to install
Version: 2008 Operating System: Mac OS X 10.4 (Tiger) Processor: Power PC I've been trying to manually check for updates to Word 2008, and I get a &quot;critical update&quot; notice, asking me to install auto-update. When I try to install it, I cannot. It just keeps repeating the attempt, in an &quot;endless loop.&quot; What to do? <br> Thank you. ...

Spreadsheet getting corrupted
Hello All: I have a relatively large spreadsheet. I am getting errors (see after my signature line). What could be causing these errors? Is it that the spreadsheet is too large? Thanks Deguza -------------------------------------------------------- Microsoft Office Excel File Repair Log Errors were detected in file 'C:\Documents and Settings\deguza\My Documents\sales\Sales_2006_2007.xls' The following is a list of repairs: Global PivotTable report information was repaired due to integrity problems. Global PivotTable report information was repaired due to integrity problems. Glo...

Reg getting the next IV Document Number field !
Hi, I am creating a window in dexterity similar to the item transaction entry window (Transactions>Inventory> Transaction Entry). In the Item transaction Entry window, the Document number field defaults to the next document number when we move the focus to that field. I wanted to know if there is any function , or table that i can access to get the next document number in the new window that i am creating. Thanks, swetha -- swetha >In the Item transaction Entry window, the Document number field defaults to the next document number when we move the focus to that field.< T...

How do I get??
Sorry if this seems a fairly easy question to you guys. I have a spreadsheet and I need to get the current system time into a cell. The format of the time is hh:mm am/pm. Any assistance would be most helpful. Hi try =INT(NOW()) in this cell and format is to your needs -- Regards Frank Kabel Frankfurt, Germany PM wrote: > Sorry if this seems a fairly easy question to you guys. > > I have a spreadsheet and I need to get the current system time into a > cell. The format of the time is hh:mm am/pm. > > Any assistance would be most helpful. PM, in the cell you want pu...

Get notified when Out of Office is on
I am using Outlook 2007. With 2003, when returning from leave, I used to get a notification that the Outlook out of office was still on and asking me if I wanted to switch it off. This reminder no longer appears with 2007. How can I switch this setting on so that I get the notification? "Mary Ann" <MaryAnn@discussions.microsoft.com> wrote in message news:ED5533A7-D0EA-41B8-B1A0-A9D15FB6FD98@microsoft.com... >I am using Outlook 2007. With 2003, when returning from leave, I used to get > a notification that the Outlook out of office was still on and asking me ...

Getting Control ID in Active X control
GetWindowLong return 0. You need to call GetDlgCtrlID from outside the activex control as the function available within the control returns an arbitrary value of zero. Take a look at this article http://xendra.ens.uabc.mx/~sinfante/loo/libro/ch17_4.htm particularly the paragraph preceeding the title "MFC and Ambient Properties". This really begs the question why you want the ID in the first place as activex controls have to be fully self containing to operate - with a few exceptions where sibling controls need to be able to locate each other. - Tim "ytv" <...

over VPN, it gets stuck when sending outgoing email
When I use VPN to connect to work from home, the outlook can not finish sending outgoing emails. The progress bar moves to the end but then stops there forever. It can never finish. The receiving of new incoming emails work fine. All other network functionality works, e.g. internet browsing, telnet, ftp, etc, all work fine. My system is win2k. I use linksys wireless DSL router. I'd appreciate any hint! Are you using Exchange server? I'd set up your mail profile to prompt you to work offline or online - when using VPN, work offline with scheduled syncs. If not using exchange, pos...

Get photos from a camera
I have an asp.net intranet application that I need to upload 1 or all photos from a digital camera at a PC workstation. We are using DataGrids and GridViews to display them on the web once they are uploaded but need something at the client. I assume I need some sort of ActiveX process or something. Has anyone run into a solution for this need? Thanks. -- David "DavidC" <dlchase@lifetimeinc.com> wrote in message news:2B061F4F-B548-4AA4-96FA-2F87383192D2@microsoft.com... > I have an asp.net intranet application that I need to upload 1 or all > photos ...

how to get the format of this cell right?
When I let go: Selection.Replace " ", Replacement:="-", Lookat:=xlPart, _ SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False " " is entered by means of an array: Words = Array(" ", "+", "/", "---", "--", "&") on a cell containing: 5 a I would expect to get: 5-a as a result. Unfortunately, it produces 5:00:00-AM. How can I solve this tiny but frustrating problem? best regards Pet...

How do I get my best friend back?
I NEED TO KNOW "monsterboy16" <monsterboy16@discussions.microsoft.com> wrote in message news:7918DF09-EB34-46E4-852E-8DAAC3FFF45F@microsoft.com... > I NEED TO KNOW Easy. Just lift the lid on your toilet seat and whistle. Dipshit! -- "Don't pick a fight with an old man. If he is too old to fight, he'll just kill you." Pretend that you're in a coma and unable to communicate in any way (such as posting messages in newsgroups). "monsterboy16" <monsterboy16@discussions.microsoft.com> wrote in message news...

how to get the pointer variable address from a dll
hi all, I have a DLL which has a output parameter LPVOID, A application calls this DLL function, the Dll responsibility is to assign a valid pointer to the lpBuffer, Till here it is OK, but once it returns to the Application this lpBuffer becomes NULL. Prototype, __declspec(dllexport) BOOL TRSTMonGetStatus(LPVOID lpBuffer) Please let me know what to do, I am stuck in this. try this __declspec(dllexport) BOOL TRSTMonGetStatus(LPVOID &lpBuffer) AliR. <karunyalakshmi@gmail.com> wrote in message news:1182179607.989434.318540@x35g2000prf.googlegroups.com... > > hi a...

How to reduce the number of shortcuts/links in the recent documents list?
When I look into the folder: C:\Documents and Settings\<username>\recent then there are currently approx 20 recent documents listed. How can I permanently reduce the number or even disable this list? Cindy Cindy Parker wrote: > When I look into the folder: > > C:\Documents and Settings\<username>\recent > > then there are currently approx 20 recent documents listed. > > How can I permanently reduce the number or even disable this list? > > Cindy > Right click on the Start button and choose Properties --> Customize ...

Why do I get this?
IMCEAEX-_O=ABC_OU=CN121_CN=RECIPIENTS_CN=USER1@companyABC.com <-- why am I getting this as the FROM field from a recently upgrade user to Exchange 2003? ...