Open a TXT file while exiting Word VBA

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Open a TXT file while exiting Word VBA

Post by Robie »

Hi

This is probably a stupid question - so apologies in advance.

Is it possible to open a file (in this a text file) as the last thing in a VBA macro and exit Word VBA? Basically, I am trying to open a text file after completing some VBA work.

Thanks.
Robie.

User avatar
StuartR
Administrator
Posts: 12605
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Open a TXT file while exiting Word VBA

Post by StuartR »

Just include something like

Code: Select all

    Dim fileTextfile
    fileTextfile = Shell("C:\WINDOWS\notepad.exe C:\folder\filename.txt", 1)
before the end of your macro
StuartR


User avatar
HansV
Administrator
Posts: 78485
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open a TXT file while exiting Word VBA

Post by HansV »

And add

Application.Quit

if you want to exit Word. If you want to save any open and modified documents, use

Application.Quit SaveChanges:=True

If you want to close all open documents without saving changes, use

Application.Quit SaveChanges:=False
Best wishes,
Hans

User avatar
StuartR
Administrator
Posts: 12605
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Open a TXT file while exiting Word VBA

Post by StuartR »

Hans,

I assumed that the intention was to exit VBA, rather than to exit Word
StuartR


User avatar
HansV
Administrator
Posts: 78485
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open a TXT file while exiting Word VBA

Post by HansV »

I agree that that's what Robie requested. But just in case he meant something else...
Best wishes,
Hans

User avatar
Jay Freedman
Microsoft MVP
Posts: 1318
Joined: 24 May 2013, 15:33
Location: Warminster, PA

Re: Open a TXT file while exiting Word VBA

Post by Jay Freedman »

And if you want to open the text file in Word instead of in Notepad, put this line last in the macro:

Code: Select all

Documents.Open "C:\folder\filename.txt"

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Open a TXT file while exiting Word VBA

Post by Robie »

Apologies for late reply but wasn't around until today.

Thank you Stuart, Hans and Jay.

I went with Stuart's suggestion as it works for me wonderfully.

THANK YOU VERY MUCH - :clapping: :fanfare: :cheers: