Results 1 to 4 of 4

Thread: [RESOLVED] VB Macro to Email Current Page in Word

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    3

    Resolved [RESOLVED] VB Macro to Email Current Page in Word

    Hello All

    I am trying to write a macro for MS Word that would:

    Look at each page of a word doc and find an email address on that page then
    Send the page as an attached file to the email address.

    Why: I work for a small City in Florida and we have our time cards in Word format.
    The Template we use each week is a Word Doc with a Excel sheet Object in it.

    We use the template to do a mail merge every two weeks to create the new time cards.
    Then we print them and send to the employees. About half of the city employees
    Work with computers and would like to fill out the time card inside the word doc.

    The hope is that the users will be able to fill out the time card and email it back to
    the payroll people.

    So I need your help.

    I have found some code examples on the web to email a word doc to a fixed address.

    What I need help with is:

    Code that would loop thru each page of the word doc and collect the email address from that page
    And then send only that page as an attachment to that email address. Then loop to the next page.

    Or for you to let me know that this is not do-able.

    Thanks for your Help

  2. #2
    Fanatic Member VBKNIGHT's Avatar
    Join Date
    Oct 2000
    Location
    Port25
    Posts
    619

    Re: VB Macro to Email Current Page in Word

    this is feasible, all you have to do is create a function that will search for email inside your document...this is not so complicated..

    i don't think you can send the page alone you have to extract it and save it to another document and send it...
    Last edited by VBKNIGHT; Feb 22nd, 2006 at 08:57 AM.

    If a post has helped you then Please Rate it!

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    3

    Re: VB Macro to Email Current Page in Word

    I have this much code so far.

    Does anyone have code for saving the current page in word to a new Doc ?

    Code to send the current Doc as email Attachment

    Thanks

    VB Code:
    1. Sub ETimeCard()
    2. '
    3. ' ETimeCard Macro
    4. ' Macro created 2/23/2006
    5. '
    6.  
    7.  
    8.  
    9.     Dim template As String
    10.     Dim WordApp As Word.Application
    11.     Dim WordDoc As Word.Document
    12.     Dim WordTemp As Word.Document
    13.     Set WordDoc = ActiveDocument
    14.     Dim docPath As String
    15.     Dim intTimes As Integer
    16.     Dim strLetter As String
    17.     Dim strUser As String
    18.     Dim strStatus As String
    19.     Dim docPageCount As Integer
    20.     Dim msg1 As Integer
    21.     Dim msg2 As String
    22.     Dim EMAddress As String
    23.    
    24.    
    25.    
    26.     MaxPages = Selection.Information(wdNumberOfPagesInDocument)
    27.    
    28.         For i = 1 To MaxPages
    29.             msg1 = i
    30.             MsgBox msg1
    31.        
    32.             Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst, Count:=i, Name:=""
    33.             ActiveDocument.Bookmarks("\page").Select
    34.            
    35.             Selection.Find.ClearFormatting
    36.             Selection.Find.Replacement.ClearFormatting
    37.            
    38.             With Selection.Find
    39.             .Text = "\[*\]"
    40.             .Replacement.Text = ""
    41.             .MatchWildcards = True
    42.             .Forward = True
    43.             .Wrap = wdFindStop
    44.             .MatchCase = False
    45.             .MatchWholeWord = False
    46.             .MatchAllWordForms = False
    47.             .MatchSoundsLike = False
    48.             .Forward = True
    49.             End With
    50.            
    51.             Selection.Find.Execute '    Replace:=wdReplaceAll
    52.            
    53.             EMAddress = Mid$(Selection, 2, Len(Selection) - 2) & vbNewLine
    54.            
    55.             'If Selection.Words.Count <= 10 And _
    56.                 Selection.Type <> wdSelectionIP Then
    57.             'MsgBox "The selection contains " & Selection.Words.Count _
    58.                 & " words."
    59.             'End If
    60.            
    61.            
    62.         'MsgBox EMAddress
    63.         'Options.SendMailAttach = True
    64.         'ActiveDocument.SendMail
    65.  
    66.        
    67.         Next
    68.  
    69. End Sub

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    3

    Re: VB Macro to Email Current Page in Word

    Here is the Final Code I worked out and the Time Card Doc

    VB Code:
    1. Sub ETimeCard()
    2. '
    3. ' ETimeCard Macro
    4. ' Macro created 2/23/2006 by CH
    5. '
    6. 'You must load the Outlook Library located under Tools - References in the Visual Basic Editor
    7. 'You must Create a Folder named "C:\Temp\Time Cards\" to store the Word Doc in (see code below)
    8. 'You will get a 5 sec delay for each Email, this is done by Outlook as Spam Protection
    9.  
    10.  
    11.     Dim template As String
    12.     Dim WordApp As Word.Application
    13.     Dim WordDoc As Word.Document
    14.     Dim WordTemp As Word.Document
    15.     Set WordDoc = ActiveDocument
    16.     Dim docPath As String
    17.     Dim intTimes As Integer
    18.     Dim strLetter As String
    19.     Dim strUser As String
    20.     Dim strStatus As String
    21.     Dim docPageCount As Integer
    22.     Dim msg1 As Integer
    23.     Dim msg2 As String
    24.     Dim EMAddress As String
    25.     Dim BodyText As String
    26.    
    27.    
    28.    
    29.     Dim bStarted As Boolean
    30.     Dim oOutlookApp As Outlook.Application
    31.     Dim oItem As Outlook.MailItem
    32.  
    33.    
    34.    
    35.     MaxPages = Selection.Information(wdNumberOfPagesInDocument)
    36.    
    37.         For i = 1 To MaxPages
    38.             'msg1 = i
    39.             'MsgBox msg1
    40.        
    41.             Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst, Count:=i, Name:=""
    42.             ActiveDocument.Bookmarks("\page").Select
    43.                      
    44.             '***********************************
    45.             Selection.Copy
    46.             Documents.Add template:= _
    47.             "C:\Documents and Settings\cherbold.CNSB\Application Data\Microsoft\Templates\Normal.dot" _
    48.             , NewTemplate:=False, DocumentType:=0
    49.             Selection.Paste
    50.            
    51.             ActiveDocument.SaveAs ("C:\Temp\Time Cards\TimeCard.doc")
    52.             ActiveDocument.Close
    53.            
    54.             '***********************************
    55.                      
    56.            
    57.             Selection.Find.ClearFormatting
    58.             Selection.Find.Replacement.ClearFormatting
    59.            
    60.             With Selection.Find
    61.             .Text = "\[*\]"
    62.             .Replacement.Text = ""
    63.             .MatchWildcards = True
    64.             .Forward = True
    65.             .Wrap = wdFindStop
    66.             .MatchCase = False
    67.             .MatchWholeWord = False
    68.             .MatchAllWordForms = False
    69.             .MatchSoundsLike = False
    70.             .Forward = True
    71.             End With
    72.            
    73.             Selection.Find.Execute '    Replace:=wdReplaceAll
    74.            
    75.             EMAddress = Mid$(Selection, 2, Len(Selection) - 2) & vbNewLine
    76.            
    77.             'If Selection.Words.Count <= 10 And _
    78.                 Selection.Type <> wdSelectionIP Then
    79.             'MsgBox "The selection contains " & Selection.Words.Count _
    80.                 & " words."
    81.             'End If
    82.            
    83.            
    84.             'MsgBox EMAddress
    85.          
    86.        '*****************************************************
    87.        
    88.         'On Error Resume Next
    89.  
    90.        
    91.        
    92.         Set oOutlookApp = GetObject(, "Outlook.Application")
    93.         If Err <> 0 Then
    94.             Set oOutlookApp = CreateObject("Outlook.Application")
    95.             bStarted = True
    96.         End If
    97.        
    98.         Set oItem = oOutlookApp.CreateItem(olMailItem)
    99.        
    100.         With oItem
    101.             .To = EMAddress
    102.             .Subject = "Time Card"
    103.         '.Body = "Your text Here"
    104.             'Add the document as an attachment, you can use the .displayname property
    105.             'to set the description that's used in the message
    106.             'ActiveDocument.FullName , Type:=olByValue, _DisplayName:="Document as attachment"
    107.             .Attachments.Add Source:="C:\Temp\Time Cards\TimeCard.doc"
    108.             .Send
    109.         End With
    110.        
    111.        
    112.        
    113.  
    114.         '**********************************************************
    115.        
    116.        
    117.  
    118.         Next
    119.  
    120. Set oItem = Nothing
    121. Set oOutlookApp = Nothing
    122. End Sub
    Attached Files Attached Files

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