Link to home
Create AccountLog in
Avatar of RJHarvey72
RJHarvey72Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Looking to create Outlook macro to automate fax sending

We use a fax from email solution that requires the user to enter in the recipient as [FAX:XXXXX] within Outlook where XXXXX is the fax number. This will then send it exchange and GFI Faxmaker.

I'd like to try make this a little more user friendly and therefore encourage users to use it.

Basically I want a button in outlook that will run a macro that will prompt the user for a phone number (it must start with 0) and when they click OK it will add the recipient as [FAX:whatever they enter]. It doesn't require to send the email just to avoid them having the enter the [FAX:XXXX] part.

If this script could also set the From part of the email as well that would be perfect. For example setting it to be departmentaFax@company.com.

Thanks

Avatar of SiddharthRout
SiddharthRout
Flag of India image

Why not write a macro which prompts the user for the two values? Or am I missing a point? Something like this... (Not Sure if this is what you want?)

Sub Sample()
    Dim sFaxNo      As String
    Dim sFrom       As String
    Dim sSubject    As String
    Dim sBody       As String
    Dim sTo         As String
    Dim Ret         As Boolean
    
    sTo = "RJHarvey72@company.com"
    sFrom = "departmentaFax@company.com"
    sSubject = "Hello World"
    sBody = "I am hungry!!!"
    
    sFaxNo = InputBox("Please enter the Fax Number, starting with '0'", "Fax Number")
    
    If Len(Trim(sFaxNo)) = 0 Then
        Exit Sub
    Else
        If Left(Trim(sFaxNo), 1) <> "0" Then
            MsgBox "Incorrect Fax Number"
            Sample
            Exit Sub
        Else
            Ret = SendFax(sFrom, sSubject, sFaxNo, sBody, sTo)
            
            If Ret = True Then
                MsgBox "Fax sent successfully"
            Else
                MsgBox "Error in sending fax"
            End If
        End If
    End If
End Sub

Function SendFax(Frm As String, sSub As String, FaxNo As String, _
bodyText As String, toName As String) As Boolean
    Dim xml     As Object
    Dim result  As String

    Set xml = CreateObject("MSXML2.XMLHTTP.6.0")

    xml.Open "GET", _
             "http://www.webservicex.net/fax.asmx/SendTextToFax?FromEmail=" & _
             Frm & "&Subject=" & sSub & "&FaxNumber=" & FaxNo & _
             "&BodyText=" & bodyText & "&ToName=" & toName, False
    xml.Send

    result = xml.responsetext
 
    If InStr(result, "Your Message has sent successfully") > 0 _
    Then SendFax = True
End Function

Open in new window


Sid
Avatar of RJHarvey72

ASKER

Sid,

Thanks for taking the time to look at this. I think we are almost there. If we can simplify it a tiny bit then that will be perfect. All I'm looking to do is to prompt them for a fax number as you've done and hardset the From: part of the email. Once they've entered a valid number then all that will happen is that the email will look as below where XXXX is the number they've entered.

Many thanks

Richard

 User generated image
To send a FAX are you signed up with any "Fax Services" ?

The code that I gave above does what you want. See the line

Ret = SendFax(sFrom, sSubject, sFaxNo, sBody, sTo)

in the above code.

If you want to just display an email like in the snapshot above then use this code. You don't need to specify a "From" as it will go through the default account. For that, see this code.

Paste this code in a module in VBE Outlook.

Sub Sample()
    Dim sFaxNo          As String
    Dim sSubject        As String
    Dim sBody           As String
    Dim Ret             As Boolean
    Dim objOutlookMsg   As MailItem
    Dim objOutlookRecip As Recipient
    
    sSubject = "Hello World"
    sBody = "I am hungry!!!"
    
    sFaxNo = InputBox("Please enter the Fax Number, starting with '0'", "Fax Number")
    
    If Len(Trim(sFaxNo)) = 0 Then
        Exit Sub
    Else
        If Left(Trim(sFaxNo), 1) <> "0" Then
            MsgBox "Incorrect Fax Number"
            Sample
            Exit Sub
        Else
            
            Set objOutlookMsg = Application.CreateItem(olMailItem)

            With objOutlookMsg
                '~~> Add the To recipient(s) to the message.
                Set objOutlookRecip = .Recipients.Add(sFaxNo)
                objOutlookRecip.Type = olTo
                
                '~~> Set the Subject and Body.
                .Subject = sSubject
                .Body = sBody
                
                '~~> display the message
                .Display
            End With
        End If
    End If
End Sub

Open in new window


Sid
Sid,

I don't need to use a Fax Service as we use a product called GFI Faxmaker which allows users to send/receive from Outlook. When they address the To part of the email as [FAX:XXXX] then it is routed to the Fax Server which then sends the faxes using ISDN. Depending on the FROM part of the email the recipient will see the fax as coming from a certain fax number. For example;

If the FROM is set to DepartmentAFax then it will show it as 01234567 whereas if it is sent from DepartmentBFax then it will show as having come from 01234568.

That's why I want to set the FROM part of the email .

The only other thing is that the TO part needs to be [FAX:XXXXX] as opposed to XXXXX.

If we can get those two minor things sorted then that is perfect.

Thanks again.

>>>The only other thing is that the TO part needs to be [FAX:XXXXX] as opposed to XXXXX.

For this

Add this line

sFaxNo = "[FAX:" & sFaxNo & "]"

Open in new window


before this line

Set objOutlookRecip = .Recipients.Add(sFaxNo)

Open in new window


in the above code.

Let me think about the "From" part.

Sid
ASKER CERTIFIED SOLUTION
Avatar of SiddharthRout
SiddharthRout
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Brilliant, that's perfect.

Thanks again for your help.

Richard
You are welcome :)

Sid