Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2006

Tip: Looking for answers? Try searching our database.

Run code when Save/Save As is selected Word 2000

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
elle0612 - 19 Aug 2006 23:02 GMT
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
Jezebel - 20 Aug 2006 00:33 GMT
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.

> Hi
>
[quoted text clipped - 23 lines]
>
> Thanks
elle0612 - 20 Aug 2006 09:59 GMT
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

> Hi
>
[quoted text clipped - 21 lines]
>
> Thanks
elle0612 - 20 Aug 2006 19:41 GMT
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

> Thanks for that, its not working quite right, though nearly right.
>
[quoted text clipped - 56 lines]
> >
> > Thanks
Helmut Weber - 20 Aug 2006 20:05 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

elle0612 - 21 Aug 2006 20:06 GMT
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.

> Hi,
>
[quoted text clipped - 22 lines]
>
> HTH
Helmut Weber - 21 Aug 2006 21:34 GMT
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
Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.