|
-
Jan 27th, 2009, 10:32 PM
#1
Thread Starter
Hyperactive Member
VBA Word..
im opening a MS word document using vb6. whenever i close the document MS Word always ask me if i would like to save the changes in the document. What i want is that the MS word will close without asking me if i would like to save changes (don't save the document). Is this possible?
btw: i used word as template and using the find/replace method to populate values from VB6 to word. so far i set my word template to read only to avoid changes.
________
Penny stocks to buy
Last edited by rothj0hn; Feb 15th, 2011 at 01:38 PM.
-
Jan 27th, 2009, 11:33 PM
#2
Re: VBA Word..
wdApp.Documents(nRpt).Close SaveChanges:=wdDoNotSaveChanges
-
Jan 28th, 2009, 04:30 AM
#3
Thread Starter
Hyperactive Member
Re: VBA Word..
ahm.. what i mean of closing is.. when the user click the close window in MS Word.. sorry if i havent explain it earlier.
________
FORD XY FALCON GT
Last edited by rothj0hn; Feb 15th, 2011 at 01:38 PM.
-
Jan 28th, 2009, 05:02 AM
#4
Re: VBA Word..
if you set the saved property to true, it will not ask to save,
wdApp.Documents(nRpt).saved = true,
but any edit or keypress in the document will change the saved property to false again,
you could put code in the document.close event
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 28th, 2009, 10:46 AM
#5
Re: VBA Word..
Moved To Office Development
-
Jan 29th, 2009, 02:12 AM
#6
Re: VBA Word..
Code:
xlApp.Displayalerts = False
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jan 30th, 2009, 08:00 PM
#7
Thread Starter
Hyperactive Member
Re: VBA Word..
Dim GMC As New Word.Application
GMC.Documents.Open (App.Path & "/Certificates/GoodMoral.doc")
@westconn1
ive tried GMC.Documents.Save False and it works ok unless like westconn1 said if a keypress or any changes executes it will now prompt you for saving changes after closing the document.
@koolsid
ive already try your suggestion brow but nothing happens, ive also seen that GMC.Displayalerts may have values wdAlertsAll,wdAlertsAll, wdAlertsNone.. i used this 3 values but nothing happens..
ahm.. what i'm thinking now is if i can use macro of MS word and create a procedure in document.close event that will automatically not save the document and to avoid the prompt after closing word. is this posible? thats the last resort. but im still looking forward to solve this without using macros because im not familliar with it.. lolz
thanks for the response..
________
Michigan Marijuana Dispensary
Last edited by rothj0hn; Feb 15th, 2011 at 01:39 PM.
-
Jan 30th, 2009, 10:07 PM
#8
Re: VBA Word..
if you are running this from VB then the wd constants may not be set, so you would need to set them as constants or use literals
wordallertsnone which is what you want evaluate to false
wdalertsall to true
but as you say will not prevent that message on close
you can put
thisdocument.saved = true
in the workbook.close event, but then does not prevent user saving manually
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 31st, 2009, 03:10 AM
#9
Re: VBA Word..
Can I see your project? The part which interacts with word.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jan 31st, 2009, 07:22 AM
#10
Thread Starter
Hyperactive Member
Re: VBA Word..
vb Code:
Dim GMC As New Word.Application Dim myDate As String GMC.Documents.Open (App.Path & "/Certificates/GoodMoral.doc") myDate = Format(dtpIssued.value, "mmmm dd, yyyy") 'Find / Replace - Date With GMC.Selection.Find .Text = "GMCDate" .Replacement.Text = myDate .MatchAllWordForms = False End With GMC.Selection.Find.Execute Replace:=wdReplaceAll 'Find / Replace - Name With GMC.Selection.Find .Text = "GMCName" .Replacement.Text = txtStudentName.Text End With GMC.Selection.Find.Execute Replace:=wdReplaceAll GMC.Visible = True End If
here is my code.. i dont have net connection at home, ill try to attached my project the next time..
________
Digital Vaporizer
Last edited by rothj0hn; Feb 15th, 2011 at 01:39 PM.
-
Jan 31st, 2009, 03:53 PM
#11
Re: VBA Word..
To achieve what you want, there is no easy solution for a Word document.
Unlike Excel workbook, Word document does not have BeforeSave event so you cannot cancel the Save event with a Word Document object.
You need to use events of Word Application object via an Application Class.
In your template document "GoodMoral.doc",
1. Insert a Class module named "Class1" as default.
2. Copy and paste this code to the code module of Class1:
Code:
Option Explicit
Public WithEvents App As Word.Application
Private Sub App_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
If Doc.Name = ThisDocument.Name Then Doc.Saved = True
End Sub
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
If Doc.Name = ThisDocument.Name Then Cancel = True
End Sub
3. Copy and paste this code to the code module of ThisDocument object:
Code:
Option Explicit
Dim W As New Class1
Private Sub Document_Open()
Set W.App = Word.Application
End Sub
4. Save the document "GoodMoral.doc" then close it.
5. Re-open the document "GoodMoral.doc" (with Macro Enabled), make some changes then Save or Close it, you will see that the Save event was disabled for this document (what you just changed is not saved).
6. The cancel of Save event may make you become a victim of yourself. From now on, if you want to modify and save "GoodMoral.doc", you must Disable its Macro on open or before saving.
-
Feb 1st, 2009, 07:09 AM
#12
Re: VBA Word..
This thread should be a part of Visual basic 6
I was right when I said that you need to use display alerts. The code which you have given has no errors except that you have not used .displayalerts = false.
Now do this small excercise. Create a new form in vb6. In that create a commandbutton and then call this sub from the commandbutton. You will see what I mean....
You have used Early binding, I have used latebinding. I tried the following code and it works just fine...
vb Code:
Sub ActionDoc()
Dim GMC As Object, WordDoc As Object
On Error Resume Next
'-- See if any existing Word application is open
Set GMC = GetObject(, "Word.application")
On Error GoTo 0
If GMC Is Nothing Then
'-- If no instance found, Create one
Set GMC = CreateObject("Word.application")
End If
'-- Change path as applicable
Set WordDoc = GMC.Documents.Open("c:\temp\temp.doc")
'-- Show word
GMC.Visible = True
'-- Type something in word
With GMC.Selection
.TypeText Text:=" Xyz"
.TypeParagraph
.TypeText Text:=" xyz"
End With
'-- Disable alerts
GMC.DisplayAlerts = False '-- <===Toggle this to see what I mean
'-- Close Document without saving
WordDoc.Close
Set WordDoc = Nothing
'-- Quit Word
GMC.Quit
Set GMC = Nothing
End Sub
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Feb 1st, 2009, 07:34 AM
#13
Re: VBA Word..
That's OK when user close the document but it cannot stop user to save the document after changing it.
-
Feb 1st, 2009, 07:41 AM
#14
Re: VBA Word..
In the above case, it doesn't make a difference because the word doc is readonly Check post #1
The OP just wants to disable the alerts...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Feb 3rd, 2009, 07:20 PM
#15
Thread Starter
Hyperactive Member
Re: VBA Word..
@koolsid
the code works but if i remove the following code and let the user do some changes in his own and he/she close the document the prompt message will now appear. thats what ive notice.
vb Code:
WordDoc.Close Set WordDoc = Nothing '-- Quit Word GMC.Quit
@anhn
the macros is good and its works just like what i wanted. but i have a few little question if i set my document to read only it produce errors in my VB6 applications, but if it is not set to read only it just work fine.
i just use on error goto statement method.
the statement just show the err.description
the message has something to do with the read only property.
thanks to the reply guys.
it was really helpful
________
ARIZONA DISPENSARY
Last edited by rothj0hn; Feb 15th, 2011 at 01:39 PM.
-
Feb 3rd, 2009, 07:48 PM
#16
Re: VBA Word..
Instead of open "GoodMoral.doc" directly with .Open(), you can use it as a template to create a new UNsaved document with .Add()
Code:
Dim wdDoc As Object
'... ...
Set wdDoc = GMC.Documents.Add(Template:=App.Path & "/Certificates/GoodMoral.doc")
That also makes Word happy when 2 or more people use that file at the same time.
It will be better if that file is saved as a template "GoodMoral.dot" and you can set it as read-only or not.
-
Feb 4th, 2009, 07:07 PM
#17
Thread Starter
Hyperactive Member
Re: VBA Word..
so its better to changed it to .dot from .doc? will it still open as a word document? im using ms word 2003 now, if i change it to .dot will it still open in ms word 2007?
________
Extreme Q Vaporizer
Last edited by rothj0hn; Feb 15th, 2011 at 01:40 PM.
-
Feb 4th, 2009, 08:35 PM
#18
Re: VBA Word..
 Originally Posted by rothj0hn
so its better to changed it to .dot from .doc? will it still open as a word document? im using ms word 2003 now, if i change it to .dot will it still open in ms word 2007?
Why don't you try to see what happens?
With.dot file, when you double click it, Word will create a new unsaved document base on it as a template.
If you want to modify a .dot file, you should open it from Word (with File menu or Open button).
-
Feb 5th, 2009, 04:32 AM
#19
Thread Starter
Hyperactive Member
Re: VBA Word..
ok ill try it.. will it still work in 2007? i dont have MS office 2007 at home so i cant try if it will still work on it.
________
F2003-Ga
Last edited by rothj0hn; Feb 15th, 2011 at 01:40 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|