How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask Tech_Ben Your Own Question

Tech_Ben
Tech_Ben, Computer Software Engineer
Category: Programming
Satisfied Customers: 1617
Experience:  Years of professional experience in C++, Visual Basic, .NET, Java, DB2, Oracle, SQL Server, PL/SQL
Type Your Programming Question Here...
Tech_Ben is online now
A new question is answered every 9 seconds

I have a word 2007 document, I have added a command button

Resolved Question:

I have a word 2007 document, I have added a command button with a script so that the document attaches itself to an outlook email automatically, I want though for the script to include a code where the Send To email address will automatically appear - please help - this is the script I am using:
Private Sub CommandButton1_Click()
Options.SendMailAttach = True
ActiveDocument.SendMail
End Sub
Submitted: 1 year ago.
Category: Programming
Expert:  Tech_Ben replied 1 year ago.

Tech_Ben :

Thank you for the question. Is there an address you would like to always have shown in the "To" field when you click the button?


 

Customer:

yes

Tech_Ben :

Ok, I will just send a sample with a fake email that you can replace in your code. I just need a few minutes if that is ok.


 

Customer:

Ok no problem, I have tried so many codes on different forum but it keeps saying error, the code I sent earlier is working perfactly with attaching itself to email but just can't figure out the 'Send to' bit...

Customer:

How are we doing? :)

Tech_Ben :

Almost there


 

Tech_Ben :

I have the email configured, just working on attaching the active document


 

Customer:

will it be a case of me pasting in the code?


 

Tech_Ben :

Yes


 

Tech_Ben :

See if this works for you, there is a problem with my Outlook that prevents me from testing it now


 

Tech_Ben :

Private Sub CommandButton1_Click()
ActiveDocument.HasRoutingSlip = True
With ActiveDocument.RoutingSlip
.subject = "Your Subject


"
.AddRecipient "someone@email.com"
.Delivery = wdAllAtOnce
End With
ActiveDocument.Route
End Sub

Customer:

I didn't think routing slip worked on 2007 - that is why I am having the problem - will try it again.


 

Customer:

No it doesn't work


 

Tech_Ben :

Try this then


 

Tech_Ben :

Private Sub CommandButton1()
ActiveDocument.SendMail
With ActiveDocument.MailEnvelope.item
.Recipients.Add "test@test.com"
.subject = "Your Subject"
End With
End Sub


 

Customer:

no, it is still not working - do I need to delete anything otherXXX@XXXXXX.XXX ? assume I keep the " symbols???


 

Tech_Ben :

you should only need to change the email address that is there


 

Tech_Ben :

what message do you get?


 

Customer:

the whole thing has frozen - hang on whilst I go in again

Tech_Ben :

ok


 

Customer:

I put the code in, saved it and when I click on the button nothing happens at all

Tech_Ben :

is Outlook open anywhere?


 

Customer:

yes

Tech_Ben :

Can you close it?


 

Customer:

yes hang on

Customer:

i will try again


 

Tech_Ben :

Ok, in the mean time I'll work on one more in case that doesn't work


 

Customer:

it says compile error only comments may appear after end sub, end function or end property (???)


 

Tech_Ben :

there shouldnt be anything after "End Sub" in your code


 

Customer:

there is nothing


 

Customer:

now outlook won't open either!!

Tech_Ben :

Check for an outlook.exe process running on your machine and kill it


 

Customer:

done, document has died, lost the lot, trying again on separate document

Tech_Ben :

Try this instead as well


 

Tech_Ben :

Sub CommandButton1_Click


()
Options.SendMailAttach = True
ActiveWindow.EnvelopeVisible = True
With ActiveDocument.MailEnvelope.item
.subject = "My Subject"
.Recipients.Add "test@test.com"
End With
End Sub



 

Customer:

do i leave the gaps in as above? or do they need to be neat together?


 

Tech_Ben :

the indentation doesn't make a difference


 

Customer:

nothing

Tech_Ben :

Nothing happens at all? Can you look in the original Word document and Outlook (if it's open)?


 

Customer:

no nothing, at the top there is a drop down menu that has General, Command Button 1, Command Button 2 and Command Button 11

Tech_Ben :

What if you just test it by clicking "Run" from the Visual Basic Editor?


 

Customer:

Where is "run"


 

Customer:

can u call me?


 

Tech_Ben :

I can't unfortunately. I have some code that can easily attach a hard coded file name. Is the document name always going to be the same?


 

Customer:

I don't understand what you mean, it has started to work but without the email address included even though I have added it to the code


 

Tech_Ben :

I mean I have code that will generate a new email with the subject line, and will attach C:\mydocument.doc


 

Tech_Ben :

rather than the active document


 

Customer:

I can generate the new email, I just need the Send to email address included


 

Tech_Ben :

yes, it's just much different to interface with Outlook once it's already opened, so you can't easily just add the email address the way you're doing it


 

Customer:

so what do I do?


 

Tech_Ben :

Is it acceptable to use code like this?


 

Customer:

??

Tech_Ben :

Private Sub CommandButton1_Click()

Dim outlookapp As Object
Dim item As Object
Dim subject As String
Dim msg As String

Set outlookapp = CreateObject("outlook.application")

msg = "Enter Message here"
subject = "Enter subject here"
Set item = outlookapp.CreateItem(0)
With item
.To = "test@test.com <mailtoXXX@XXXXXX.XXX>"
.subject = subject
.Body = msg
.Display
.Attachments.Add "C:\doc1.doc"
End With

End Sub


 

Tech_Ben :

That will work, but notice the .Attachments.Add line towards the end


 

Customer:

that just brings up the email with the attachments

Tech_Ben :

That will attach the document you specify on that line


 

Customer:

do I just type in there the exact name of the doc? do I include spaces?

Tech_Ben :

you need to put the whole path to the document


 

Tech_Ben :

for example, C:\documents\october\test1.doc


 

Customer:

how do I find that?

Tech_Ben :

where do you save your document to?


 

Customer:

desktop


 

Tech_Ben :

ok, and what is the document name?


 

Customer:

Final Word Processing Instruction Form.docm

Tech_Ben :

and what is the username you log in to the computer as?


 

Tech_Ben :

and which version of Windows are you using?


 

Customer:

my email address


 


2010

Tech_Ben :

Windows XP?


 

Tech_Ben :

Windows 7?


 

Customer:

7

Tech_Ben :

if you save to the desktop, the path would be like this: "


C:\Users\Ben\Desktop\Final Word Processing Instruction Form.doc"

Tech_Ben :

but you change "Ben" do the name you use


 

Tech_Ben :

you might want to go to Start and type C:\Users


 

Tech_Ben :

to see what your folder would be called


 

Customer:

ok will try

Tech_Ben :

ok


 

Customer:

yes that worked, thankyou, however I need the client to be able to change the form on a daily basis and for the form to be sent as per the form changes (i.e. it has drop down menu's etc) will that happen - I will give it a try

Tech_Ben :

Yes, it will always attach the latest version of the document


 

Customer:

I will try it now

Tech_Ben :

ok


 

Tech_Ben :

Just make sure you save your document after making the changes, before sending


 

Customer:

great! What about when the client saves this document to their desktop will the script need altering? This doc needs to live on their desktop so to send me the request form each day

Tech_Ben :

Yes, so if someone else is sending it, they need to modify the same path you did to reflect their user name


 

Tech_Ben :

Or you can do it for them if they're the only one using the script


 

Customer:

so i should keep my email address in there as to the place it is to be sent to, but walk them through the scrip process to change the document path to their document location?

Customer:

is there anyway around this?

Tech_Ben :

You can even just ask them what their user name is XXXXX XXXXX the script for them, before sending it to them for the first time


 


 

Tech_Ben :

that way they don't have to modify the script themselves


 

Tech_Ben :

But yes, keep your email in there


 

Customer:

ok so say you are the client, basically all I need to ask is:


what is your username when you log on to your computer i.e. joe.bloggs?


are you on windows 7 or xp?


what if they say XP?

Tech_Ben :

I am on Windows 7


 

Tech_Ben :

If they say XP, it will be like this:


 

Tech_Ben :

Windows 7: C:\Users\Jog.Bloggs\Desktop\Final Word Processing Instruction Form.doc"


 

Tech_Ben :

Windows XP: "


C:\Documents and Settings\Joe.Bloggs\Desktop\Final Word Processing Instruction Form.doc"

Customer:

So that is all I need to ask them and I can change the script for them?

Tech_Ben :

yes


 

Customer:

thank you so very much, will it work even when I have outlook on?

Tech_Ben :

yes, it will


 

Tech_Ben :

and thank you!


 

Tech_Ben :

I hope this answers your question. Please rate me "OK Service" or higher if you are satisfied with my response, otherwise please reply and let me know how I can assist you further. Thanks again!


 

Customer:

thanks so much again

Tech_Ben, Computer Software Engineer
Category: Programming
Satisfied Customers: 1617
Experience: Years of professional experience in C++, Visual Basic, .NET, Java, DB2, Oracle, SQL Server, PL/SQL
Tech_Ben and 3 other Programming Specialists are ready to help you
Expert:  Tech_Ben replied 1 year ago.
Our chat has ended, but you can still continue to ask me questions here until you are satisfied with your answer. Come back to this page to view our conversation and any other new information.

What happens now?

If you haven’t already done so, please rate your answer above. Or, you can reply to me using the box below.
Customer: replied 1 year ago.


Do you want a bonus? Happily do so if you can answer a question, when saved to other desktops with script amended (which works fine) it doesn't send the latest version, in fact it keeps asking them to save again on to desktop when they hit the save button (and then doesn't allow them to save) whereas on mine it just saves without any problem! Please can you help - desperate!?

Expert:  Tech_Ben replied 1 year ago.
Let's enhance the code a bit. We can get the desktop location automatically by adding a function to get it, rather than hard coding it in the script. This should make it easier by always returning the current users desktop. As long as the user saves the file as "Final Word Processing Instruction Form.doc" to their desktop, this should always work no matter what version of Windows or what user is logged in. Notice a difference in the ".Attachments" line, and the new function at the bottom. Just replace your current code with this, give it a run, and send it to your client.

Private Sub CommandButton1_Click()

Dim outlookapp As Object
Dim item As Object
Dim subject As String
Dim msg As String

Set outlookapp = CreateObject("outlook.application")
Instruction Form.doc"
msg = "Enter Message here"
subject = "Enter subject here"
Set item = outlookapp.CreateItem(0)
With item
.To = "test@test.com <mailto:test@test.com>"
.subject = subject
.Body = msg
.Display
.Attachments.Add GetDesktopFolder & Application.PathSeparator & "Final Word Processing Instruction Form.doc"
End With

End Sub

Function GetDesktopFolder()
Dim objShell
Set objShell = CreateObject("WScript.Shell")
GetDesktopFolder = objShell.SpecialFolders("Desktop")
Set objShell = Nothing
End Function
Customer: replied 1 year ago.

Hiya Ben


 


Thanks for this, so if I change the coding to that, I don;t therefore need to change the coding for them when I save it to their desktops - is that what you mean? Sorry if I am dence, I am using VB for the first time and as you can see I am not very good!

Attachments are only available to registered users.

Register Here

Attachments are only available to registered users.

Register Here

Attachments are only available to registered users.

Register Here
View Full Image

Attachments are only available to registered users.

Register Here


 

Expert:  Tech_Ben replied 1 year ago.
That's right, just make sure they are saving the file as "Final Word Processing Instruction Form.doc" to their desktop, and the code will take care of the rest. The function I added has VBA logic to determine what the users desktop folder is rather than having to hard code it, then the attachment line builds a string that adds the desktop location, the path separator (\), then the file name so you are left with something like "C:\Users\SomeGuy\Desktop\Final Word Processing Instruction Form.doc", or "C:\Documents and Settings\AnotherGuy\Desktop\Final Word Processing Instruction Form.doc" depending on who runs it.
Customer: replied 1 year ago.


I pasted in the code and nothing is happening, do I only need to amend the email? or do I need to amend anything else?

Expert:  Tech_Ben replied 1 year ago.
You should replace the entire code with what I pasted above. You just need to amend the "msg", "subject" and ".To" lines, and make sure your file name is correct in the ".Attachments.Add" line. The code should definitely run if it was running for you before.
Customer: replied 1 year ago.


It says ambigous name detetected "GetDeskTopFolder"

Expert:  Tech_Ben replied 1 year ago.
Can you send me the code you are using?
Customer: replied 1 year ago.

Private Sub CommandButton1_Click()


Dim outlookapp As Object Dim item As Object Dim subject As String Dim msg As String


Set outlookapp = CreateObject("outlook.application") Instruction Form.doc" msg = "Please can you kindly process my request and let me know as soon as possible regarding capacity" subject = "NWR Request Form" Set item = outlookapp.CreateItem(0) With item .To = "XXXXX@XXXXXX.XXX" .subject = subject .Body = msg .Display .Attachments.Add GetDesktopFolder & Application.PathSeparator & "Final Word Processing Instruction Form.docm" End With


End Sub


Function GetDesktopFolder() Dim objShell Set objShell = CreateObject("WScript.Shell") GetDesktopFolder = objShell.SpecialFolders("Desktop") Set objShell = Nothing End Function

Expert:  Tech_Ben replied 1 year ago.
Can you try mine here?

http://wikisend.com/download/916590/Final Word Processing Instruction Form.doc

Just save it to your desktop and run the code.
Customer: replied 1 year ago.
,
Customer: replied 1 year ago.


sorry, what is that?

Expert:  Tech_Ben replied 1 year ago.
That's a link to my version. I was hoping you could try it on your end and confirm that it works ok.
Customer: replied 1 year ago.


it's is just downloading, is it something for me to paste in instead of the above, going to start crying in a mo

Attachments are only available to registered users.

Register Here

Attachments are only available to registered users.

Register Here

Attachments are only available to registered users.

Register Here
View Full Image

Attachments are only available to registered users.

Register Here
having such a bad day

Customer: replied 1 year ago.

it's just a blank page?

Expert:  Tech_Ben replied 1 year ago.
Yes, it is just the code. Go to the VB editor and run it to make sure it works for you. You should see a green play button in the editor that will run the code.
Customer: replied 1 year ago.


the word doc was blank.

Expert:  Tech_Ben replied 1 year ago.
Yes, it's blank because I have no content in the document, but the code is there in the editor that attaches the document. You should be able to run it regardless of what content is in the document. The code needs to go in the Visual Basic Editor rather than in the document.
Customer: replied 1 year ago.


where do I find the code in the word doc?

Expert:  Tech_Ben replied 1 year ago.
If you go to Tools -> Controls -> View Code, that's where it goes. Or if you have a "Developer" tab, you can click that and then click "Visual Basic"
Customer: replied 1 year ago.


got the code pasted it in - changed email - still nothing.

Expert:  Tech_Ben replied 1 year ago.
Can you try with the document I sent you? I have a feeling something isn't being done right, because the code should definitely work.
Customer: replied 1 year ago.

still not working - is this the code of your doc


Private Sub CommandButton1_Click()


Dim outlookapp As Object Dim item As Object Dim subject As String Dim msg As String


Set outlookapp = CreateObject("outlook.application") msg = "Enter Message here" subject = "Enter subject here" Set item = outlookapp.CreateItem(0) With item .To = "XXXXX@XXXXXX.XXX <mailto:XXXXX@XXXXXX.XXX>" .subject = subject .Body = msg .Display .Attachments.Add GetDesktopFolder & Application.PathSeparator & "Final Word Processing Instruction Form.doc" End With


End Sub


Function GetDesktopFolder() Dim objShell Set objShell = CreateObject("WScript.Shell") GetDesktopFolder = objShell.SpecialFolders("Desktop") Set objShell = Nothing End Function

Expert:  Tech_Ben replied 1 year ago.
Yes. Still nothing happens, or you get an error?
Customer: replied 1 year ago.


just nothing happens, the one you sent yesterday worked fantastic, my only problem was it that when my colleague (whom I am testing it on) amended the form from her desktop - it did eveything it was suppose to do but didnt send me the latest version (it didn't save their amendments) and when they click save it asks them to save as new but then doesn't allow them to do that either...

Expert:  Tech_Ben replied 1 year ago.
If they aren't clicking the button to email, the code would not impact them. Is the document you sent them read-only? That's the only case where the save would behave like that.
Customer: replied 1 year ago.


They click the button to email and that function works, it is just if they click save beforehand (which they need to do to send the latest version) it prompts them to save as new - how do I check if read only?

Expert:  Tech_Ben replied 1 year ago.
Right click the file on your desktop and look at the properties to see if it is marked read-only.
Customer: replied 1 year ago.


sorry ben its just not working, going to see if I can revert back to what you showed me yesterday and figure out if they can save without it being prompted to save as all the time

JustAnswer in the News:

 
 
 
Ask-a-doc Web sites: If you've got a quick question, you can try to get an answer from sites that say they have various specialists on hand to give quick answers... Justanswer.com.
JustAnswer.com...has seen a spike since October in legal questions from readers about layoffs, unemployment and severance.
Web sites like justanswer.com/legal
...leave nothing to chance.
Traffic on JustAnswer rose 14 percent...and had nearly 400,000 page views in 30 days...inquiries related to stress, high blood pressure, drinking and heart pain jumped 33 percent.
Tory Johnson, GMA Workplace Contributor, discusses work-from-home jobs, such as JustAnswer in which verified Experts answer people’s questions.
I will tell you that...the things you have to go through to be an Expert are quite rigorous.
 
 
 

What Customers are Saying:

 
 
 
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer New York
< Last | Next >
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer New York
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex Los Angeles, CA
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP Hesperia, CA
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin Kernersville, NC
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther Woodstock, NY
  • Thank you so much for taking your time and knowledge to support my concerns. Not only did you answer my questions, you even took it a step further with replying with more pertinent information I needed to know. Robin Elkton, Maryland
 
 
 

Meet The Experts:

 
 
 
  • ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7463
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
< Last | Next >
  • http://ww2.justanswer.com/uploads/SP/spatlanta2010/2011-6-23_12450_photo.64x64.gif ATLPROG's Avatar

    ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7463
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
  • http://ww2.justanswer.com/uploads/ComputersGuru/2010-02-13_051118_Photo41.JPG LogicPro's Avatar

    LogicPro

    Computer Software Engineer

    Satisfied Customers:

    5603
    Expert in C, C++, Java, DOT NET, Python, HTML, Javascript, Design.
  • http://ww2.justanswer.com/uploads/unvadim/2010-11-15_210218_avatar.jpg unvadim's Avatar

    unvadim

    Computer Software Engineer

    Satisfied Customers:

    1158
    Good knowledge of OOP principles. 3+ years of programming experience with Java and C++. Sun Certified Java Programmer 5.0.
  • http://ww2.justanswer.com/uploads/lifesaver333/2010-10-17_191349_ls.jpeg lifesaver's Avatar

    lifesaver

    Computer Software Engineer

    Satisfied Customers:

    950
    Several years of intensive programming and application development experience in various platforms.
  • http://ww2.justanswer.com/uploads/EH/ehabtutor/2012-8-2_202016_1.64x64.jpg ehabtutor's Avatar

    ehabtutor

    Computer Software Engineer

    Satisfied Customers:

    864
    Bachelor of computer science, 5+ years experience in software development, software company owner
  • http://ww2.justanswer.com/uploads/RA/rajivsharma086/2012-6-6_17128_displaypic.64x64.jpg Raj's Avatar

    Raj

    Computer Engg.

    Satisfied Customers:

    860
    BE CS, 4+ Experience in Programming and Database (ERP)
  • http://ww2.justanswer.com/uploads/eljonis/2010-01-06_130406_eljon2.jpg Eljon's Avatar

    Eljon

    Consultant

    Satisfied Customers:

    590
    11 yrs of programming (PHP, WordPress, XSL, SQL, JavaScript)