Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-23-2013, 07:36 AM
gmanword gmanword is offline Prompt a macro to run when opening a specific file type Windows 8 Prompt a macro to run when opening a specific file type Office 2010 32bit
Novice
Prompt a macro to run when opening a specific file type
 
Join Date: Nov 2013
Posts: 3
gmanword is on a distinguished road
Default Prompt a macro to run when opening a specific file type


Hello I am attempting to create a macro and add it to the normal.dotm so when I open any file that contains "contract" in the name and is an RTF file type it will prompt to run a macro and automatically find and replace specific text.

I have created the macro that finds and replaces the text. However i cannot get the file to prompt automatically upon opening. I also think i don't have the proper syntax to only run the file if it is an RTF file which contains the name contract.

I have tried changing Document_Open() to AutoOpen, AutoExec but it still does not work. I enabled all macros in security settings as well. I am still relatively new to writing macros.

Any help would be appreciated.


Code:
Private Sub Document_Open()
Call foodandbeveragemacro 
End Sub 
 
Sub foodandbeveragemacro() 
If Not Activedocument.Name "*contract*.rtf" Then Exit Sub 
Dim run As Variant 
run = MsgBox("Do you wish to remove gratuity detail from F&B clause. Canadian Properties only.", vbYesNo, "Canadian F&B Clause Change") 
If run = vbYes Then 
"remaining code left out as it runs fine"
Thanks for any help.

Last edited by macropod; 11-30-2013 at 02:46 PM. Reason: Added code tags
Reply With Quote
  #2  
Old 11-23-2013, 10:25 PM
macropod's Avatar
macropod macropod is offline Prompt a macro to run when opening a specific file type Windows 7 32bit Prompt a macro to run when opening a specific file type Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Try something along the lines of:
Code:
Private Sub Document_Open()
With ActiveDocument
  If .SaveFormat = wdFormatRTF Then
    If InStr(.Name, "Contract", vbTextCompare) > 0 Then
      Call FoodAndBeverageMacro(ActiveDocument)
    End If
  End If
End With
End Sub
 
Sub FoodAndBeverageMacro(Doc As Document)
Dim Rslt
Rslt = MsgBox("Do you wish to remove gratuity detail from F&B clause" & vbCr & _
  "(Canadian Properties only)?", vbYesNo, "Canadian F&B Clause Change")
If Rslt = vbYes Then
With Doc.Content.Find
'Find/Replace code goes here
End With
End If
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-24-2013, 02:23 PM
gmanword gmanword is offline Prompt a macro to run when opening a specific file type Windows 8 Prompt a macro to run when opening a specific file type Office 2010 32bit
Novice
Prompt a macro to run when opening a specific file type
 
Join Date: Nov 2013
Posts: 3
gmanword is on a distinguished road
Default

Thanks for your reply I was able to figure out a different way to get it to work. I created a new string variable called Docname and made it equal the active workbook.name and was then able to use the if docname like "*Contract*.RTF" to call the find and replace macro. Thanks for your input.
Reply With Quote
  #4  
Old 11-24-2013, 02:59 PM
macropod's Avatar
macropod macropod is offline Prompt a macro to run when opening a specific file type Windows 7 32bit Prompt a macro to run when opening a specific file type Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Quote:
Originally Posted by gmanword View Post
I created a new string variable called Docname and made it equal the active workbook.name
Unless there's more to what you're doing than you've posted, I can't see where 'the active workbook.name' has any bearing - all you'd mentioned so far is documents, not workbooks.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 11-30-2013, 12:52 PM
gmanword gmanword is offline Prompt a macro to run when opening a specific file type Windows 8 Prompt a macro to run when opening a specific file type Office 2010 32bit
Novice
Prompt a macro to run when opening a specific file type
 
Join Date: Nov 2013
Posts: 3
gmanword is on a distinguished road
Default Code

Below is the code I used I probably didn't explain it very well in my previous post. Included what I did to get the macro to autorun when the filename contains a specific string.


Code:
Private Sub Document_Open()
Call Filename 
End Sub 
 
Sub Filename() 
Dim Docname As String
Docname = ActiveDocument.Name
 
If Docname Like "*Contract*" Then
  Call contractchange
End If
 
If Docname Like "*contract*" Then
  Call contractchange
End If 
End Sub 
 
Sub contractchange()
Dim run As Variant
run = MsgBox("do you want to make changes',vbYesNo)
If run = vbNo Then Exit Sub
If run = vbYes Then "remaining code goes here)

Last edited by macropod; 11-30-2013 at 02:48 PM. Reason: Added code tags
Reply With Quote
  #6  
Old 11-30-2013, 02:55 PM
macropod's Avatar
macropod macropod is offline Prompt a macro to run when opening a specific file type Windows 7 32bit Prompt a macro to run when opening a specific file type Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

OK, but your new code doesn't test the document format, (i.e. RTF), which you previously specified as a requirement.

As a side note, your two 'Docname Like' tests could be reduced to:
Code:
If Docname Like "*[Cc]ontract*" Then  Call contractchange
Finally, you'll note that my code passed the document reference from the Document_Open macro to FoodAndBeverageMacro. That ensures that, if you have two or more documents open, only the one that is subject to the Document_Open macro and to which FoodAndBeverageMacro should apply can be processed.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
macros don't work, macros in word, vba word

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prompt a macro to run when opening a specific file type Word file type in 2007 casaserves Word 4 08-03-2013 01:11 AM
Prompt a macro to run when opening a specific file type can I run a macro when I open a specific doc ?? ximpostor Word VBA 10 10-12-2012 12:35 PM
Prompt a macro to run when opening a specific file type Need VBA For Macro On How To Remove Specific Words netchie Word VBA 6 08-28-2012 03:37 PM
can I run a macro when I open a specific doc. shreked Word 8 01-12-2012 03:36 AM
Getting COMException Incompatible file type and file extension sbalerao Mail Merge 0 04-21-2011 10:30 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:39 PM.


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