Run code when Save/Save As is selected Word 2000

E

elle0612

Hi

I would like to display a message box to the user on saving a document if a
certain string of words is present in the first column of a table in the
document. I have discovered this code,

Dim rw As Row
Dim celltext As String
Dim tbl As Table

For Each tbl In ActiveDocument.Tables

For Each rw In tbl.Rows
rw.Select
celltext = rw.Cells(1).Range.Words.First.Text

If celltext = "blah di blah" Then

msgBox = " This is a blah di blah document and needs to be Emailed to blah
di blah".

This looks as if its what I need, but how do I incorporate it with the
Save/Save as commands. Or perhaps its not quite what I need - advice please,

Thanks
 
J

Jezebel

If you write macros called FileSave and FileSaveAs, they will run in place
of the built-in commands.

Alternatively, you can trap the DocumentBeforeSave event.
 
E

elle0612

Thanks for that, its not working quite right, though nearly right.

Code I now have is:

Sub FileSave()
'
' FileSave Macro
' Saves the active document or template
'

Dim rw As Row
Dim celltext As String
Dim tbl As Table

For Each tbl In ActiveDocument.Tables
For Each rw In tbl.Rows
rw.Select
celltext = rw.Cells(1).Range.Words.First.Text

If celltext = "blah di blah" Then
MsgBox "This is a blah di blah Document and needs to be Emailed"
End If
Next
Next

ActiveDocument.Save
End Sub

It works if I have only one word in the cell, but will not recognise a
phrase of 3 words ie - recognises Blah if it is the only word in the cell,
but not Blah di Blah in the cell. I want it to pick out a particular string
of words. Something to do with "First" perhaps, I'm not sure.

Can anyone help
 
E

elle0612

This is another piece of code I've tried that isn't working, I'm not at all
sure what I'm doing wrong. There's no message box popping up.

Sub FileSave()
'
' FileSave Macro
' Saves the active document or template

Dim oCell As Cell
Dim oRow As Row

For Each oRow In Selection.Tables(1).Rows
For Each oCell In oRow.Cells
If oCell.Range.Text = "Blah di Blah" Then
MsgBox "Email this Document"
End If
Next oCell
Next oRow

ActiveDocument.Save

End Sub
 
H

Helmut Weber

Hi,

the cell's text includes the end of cell mark.
Try something like this:

If Left(oCell.Range.Text, Len(oCell.Range.Text) - 2) = "test" Then

For

celltext = rw.Cells(1).Range.Words.First.Text
If celltext = "blah di blah" Then ...

Can't be, as to Word this would be three words.
Three words can'be equal to one word. ;-)

And...

in cell 1: "TestA TestB"
Word(1) would include the following space!

I use code like this:
MsgBox "[" & Selection.Cells(1).Range.Words.First.Text & "]"
or
MsgBox "[" & Selection.Cells(1).Range.Words(1) & "]"

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
E

elle0612

I am discovering that the finding of text in cells is too difficult for me at
present since I'm a beginner.

Another option would be this (but I would need to send the cursor to the top
of the document to successfully accomplish it).

With Selection.Find
..Text = "Test One Two Three"
..forward = True
..Match Whole Word = True
End With

If Selection.Find.Execute = True Then

MsgBox "Test One Two Three has been found"

End If

Is there any code I can add above the "With Selection.Find" to send the
cursor to the top of the page, or in the header even, so that it will find
the words further down the page???

Thanks - getting desperate now.
 
H

Helmut Weber

Hi elle0612,
I am discovering that the finding of text in cells is too difficult for me at
present since I'm a beginner.

Keep on, after a while you'll do it like nothing.
Everyone, even the celebrities here have been beginners.

Sub Test56649()
Dim rngDcm As Range
Dim FoundInTables As Long
Dim FoundElseWhere As Long

Set rngDcm = ActiveDocument.Range
With rngDcm.Find
.Text = "one two three"
While .Execute
If rngDcm.Information(wdWithInTable) Then
FoundInTables = FoundInTables + 1
End If
If .found And Not rngDcm.Information(wdWithInTable) Then
FoundElseWhere = FoundElseWhere + 1
End If
Wend
End With
If FoundInTables = 0 And FoundElseWhere = 0 Then
MsgBox "not found at all"
Else
MsgBox "inside of tables = " & FoundInTables
MsgBox "elsewhere = " & FoundElseWhere
End If
End Sub

HTH
--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top