Results 1 to 19 of 19

Thread: VBA Word..

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    398

    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.

  2. #2

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    398

    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.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VBA Word..

    Moved To Office Development

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    398

    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.

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    398

    Re: VBA Word..

    vb Code:
    1. Dim GMC As New Word.Application
    2.  
    3.       Dim myDate As String
    4.  
    5.        
    6.           GMC.Documents.Open (App.Path & "/Certificates/GoodMoral.doc")
    7.  
    8.  
    9.           myDate = Format(dtpIssued.value, "mmmm dd, yyyy")
    10.  
    11.           'Find / Replace - Date
    12.  
    13.           With GMC.Selection.Find
    14.  
    15.               .Text = "GMCDate"
    16.  
    17.               .Replacement.Text = myDate
    18.  
    19.               .MatchAllWordForms = False
    20.  
    21.           End With
    22.  
    23.           GMC.Selection.Find.Execute Replace:=wdReplaceAll
    24.  
    25.     'Find / Replace - Name
    26.  
    27.           With GMC.Selection.Find
    28.  
    29.               .Text = "GMCName"
    30.  
    31.               .Replacement.Text = txtStudentName.Text
    32.  
    33.     End With
    34.  
    35.             GMC.Selection.Find.Execute Replace:=wdReplaceAll
    36.     GMC.Visible = True
    37.  
    38.       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.

  11. #11
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  12. #12
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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:
    1. Sub ActionDoc()
    2.     Dim GMC As Object, WordDoc As Object
    3.    
    4.     On Error Resume Next
    5.    
    6.     '-- See if any existing Word application is open
    7.     Set GMC = GetObject(, "Word.application")
    8.    
    9.     On Error GoTo 0
    10.    
    11.     If GMC Is Nothing Then
    12.         '-- If no instance found, Create one
    13.         Set GMC = CreateObject("Word.application")
    14.     End If
    15.    
    16.     '-- Change path as applicable
    17.     Set WordDoc = GMC.Documents.Open("c:\temp\temp.doc")
    18.    
    19.     '-- Show word
    20.     GMC.Visible = True
    21.    
    22.     '-- Type something in word
    23.     With GMC.Selection
    24.         .TypeText Text:=" Xyz"
    25.         .TypeParagraph
    26.         .TypeText Text:=" xyz"
    27.     End With
    28.    
    29.     '-- Disable alerts
    30.     GMC.DisplayAlerts = False '-- <===Toggle this to see what I mean
    31.    
    32.     '-- Close Document without saving
    33.     WordDoc.Close
    34.     Set WordDoc = Nothing
    35.        
    36.     '-- Quit Word
    37.     GMC.Quit
    38.     Set GMC = Nothing
    39. 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

  13. #13
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: VBA Word..

    That's OK when user close the document but it cannot stop user to save the document after changing it.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  14. #14
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    398

    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:
    1. WordDoc.Close
    2.     Set WordDoc = Nothing
    3.     '-- Quit Word
    4.     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.

  16. #16
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    398

    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.

  18. #18
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: VBA Word..

    Quote 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).
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    398

    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
  •  



Click Here to Expand Forum to Full Width