Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-24-2018, 08:47 AM
PKW57 PKW57 is offline Runing a Macro Before Save Windows 7 64bit Runing a Macro Before Save Office 2016
Novice
Runing a Macro Before Save
 
Join Date: May 2016
Location: Illinois
Posts: 10
PKW57 is on a distinguished road
Default Runing a Macro Before Save

Greetings,


This is my first attempt at VBA in Word. I'm used to VBA in Excel and the differences are driving me nuts.


I have code that is supposed to execute before the document is saved. I created a Class Module based on Google results (along with the Register Event Handler sub in ThisDocument & a Module with the Sub Register Event Handler), and placed my code in it as follows:


Code:
Public WithEvents App As Word.Application

Private Sub App_DocumentBeforeSave _
 (ByVal Doc As Document, _
 SaveAsUI As Boolean, _
 Cancel As Boolean)

Qual_Status
Doc_Status
Equip_Status


End Sub
Private Sub Qual_Status()

'Set Qual status to Conditional, Pass or Fail
If OptionButton13 = True Then
    Label1 = "CONDITIONAL"
    Label1.ForeColor = &H80FF&
    End
End If

If OptionButton5 And OptionButton7 And OptionButton71 And OptionButton11 = True Then
    Label1 = "PASS"
    Label1.ForeColor = &H8000&
Else: Label1 = "FAIL"
    Label1.ForeColor = &HC0&
End If

End Sub

Private Sub Doc_Status()

'Set Documentation Status
If OptionButton17 And OptionButton16 And OptionButton15 = True Then
    Label2 = "RELEASED"
    Label2.ForeColor = &H8000&
Else: Label2 = "In Process"
    Label2.ForeColor = &H0&
End If

End Sub

Private Sub Equip_Status()

'Set Equipment status
If Label1 = "PASS" And Label2 = "RELEASED" Then
    Label3 = "RELEASE TO PRODUCTION"
    Label3.ForeColor = &H8000&
ElseIf Label1 = "PASS" And Label2 = "CONDITIONAL" Then
    Label3 = "RELEASE TO PRODUCTION"
    Label3.ForeColor = &H80FF&
Else: Label3 = "ON HOLD"
    Label3.ForeColor = &H0&
End If


End Sub
It runs when I save but errors out on the 1st Else statement (colored red above) with a 424: Object Required, even though the previous command for Label1 forecolor executed cleanly.


If I put this exact same code in ThisDocument, I can execute each sub manually with no problems.


Any assistance with this would be greatly appreciated.




Thanks,
~ Phil
Reply With Quote
  #2  
Old 09-24-2018, 05:10 PM
macropod's Avatar
macropod macropod is offline Runing a Macro Before Save Windows 7 64bit Runing a Macro Before Save Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,371
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You have a mix of an in-line statement with another on the next line. You should have:
Code:
Else
    Label1 = "FAIL"
    Label1.ForeColor = &HC0&
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 09-25-2018, 05:50 AM
PKW57 PKW57 is offline Runing a Macro Before Save Windows 7 64bit Runing a Macro Before Save Office 2016
Novice
Runing a Macro Before Save
 
Join Date: May 2016
Location: Illinois
Posts: 10
PKW57 is on a distinguished road
Default

Paul,


Thanks for the timely response.


I made the change as you suggested, but it still hangs at the same place with the same Object Required error.
Reply With Quote
  #4  
Old 09-25-2018, 02:29 PM
macropod's Avatar
macropod macropod is offline Runing a Macro Before Save Windows 7 64bit Runing a Macro Before Save Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,371
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

If believe you need to specify the forecolour as:
Label1.ForeColor = &H000000C0&
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 09-25-2018, 03:32 PM
macropod's Avatar
macropod macropod is offline Runing a Macro Before Save Windows 7 64bit Runing a Macro Before Save Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,371
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Cross-posted at: https://www.excelforum.com/word-prog...fore-save.html
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 09-26-2018, 01:17 PM
PKW57 PKW57 is offline Runing a Macro Before Save Windows 7 64bit Runing a Macro Before Save Office 2016
Novice
Runing a Macro Before Save
 
Join Date: May 2016
Location: Illinois
Posts: 10
PKW57 is on a distinguished road
Default Solved

Many thanks for the assistance!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Macro to Save PPT to PDF mashiulalam PowerPoint 1 05-10-2017 08:11 AM
Runing a Macro Before Save Macro to save as pdf with ability to choose save as folder rvessio Word VBA 4 07-25-2016 12:37 PM
Runing a Macro Before Save Macro: How to get this macro to save to a specific location LOUF Word VBA 1 12-07-2015 06:47 PM
Runing a Macro Before Save Continuously runing a macro Eduardo Care Word VBA 4 09-01-2015 12:11 PM
Macro Won't Save lou0915 Word VBA 2 10-17-2009 08:13 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:45 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft