SaveAs & Attach ~ macro help needed

M

moonunit

I'm trying to finish a form I've created in MS Word 2004 for Mac (OS
10.3). [As you'd expect, the idea is that a blank form is sent out b
me and then, once filled in, is emailed back to me]

Apologies if I'm asking something that's been asked before; but I'
struggling (and failing) to make sense of the VBA Object Browser an
Help dialog.

PROBLEM 1
What I want to do is add into my form a macro which saves the documen
under a name partially created by merging two of the form field name
(bookmarks).

By way of a brief example...
Form field 1 = bookmark "name1"
Form field 2 = bookmark "name2"
Save as "Name1Name2.doc"

What I have succeeded in achieving so far, is the following macro
Sub SaveAsTrial()
ActiveDocument.SaveAs "mydoc.doc", wdFormatDocument 'save .doc format
End Sub
This macro runs (On Entry, Calculated on Exit) on the final form fiel
in the document, and successfully saves the document as "mydoc.doc".
have no idea how to proceed...

[I have tried John McGhie's "...load a variable..." solution (2nd Jun
2005 - Macro to SaveAs rtf) but this is based on highlighting the tex
you want as the document title, which doesn't work when you try to ru
the script from within a form...]

BUT, if somebody were able to help me, there is a second problem...

PROBLEM 2
Whilst I can insert a hyperlink at the end of the form with my emai
address and a subject line what I need to do is attach this save
document to the same email.

Any ideas or pointers would be much appreciated

Ti
 
M

moonunit

Thank you so much John; like the cavalry coming over the hill, I kne
you'd have the answer.

Two more questions, though...

1) Filling in your macro script, I may as well have been writin
Spanish. I have absolutely no idea why this works or what any of th
words you gave me, mean. Can you recommend a book which might explai
Macro scripting for the uninitiated?

2) And there was a second part to the original query...
Is it possible to automate attaching the saved form to a hyperlinke
return email address (which launches the user's email client, addresse
the mail and inserts a subject line)?

Thanks again

Ti
 
J

John McGhie [MVP - Word and Word Macintosh]

Tim:

{Sob} I *knew* I shouldn't have been such a lazy sod, and explained my code
properly in the first place :)

OK, first: that code was written the way an experienced VBA coder might
write it, for maximum efficiency. Let's first expand it so that human
beings can see what it's doing:

Here's the original:

Sub SaveForm()

Dim NameString As String

With ActiveDocument.FormFields
NameString = .Item(1).Result & .Item(2).Result
End With
ActiveDocument.SaveAs FileName:=NameString

End Sub

Expanded, this becomes:

Sub SaveForm()

Dim NameString As String
NameString = ActiveDocument.FormFields(1).Result &
ActiveDocument.FormFields(2).Result
ActiveDocument.SaveAs FileName:=NameString

End Sub

The ³With² statement simply means I do not have to type
³activedocument.formfields² twice.

It ALSO means neither does Word, which is important if you are creating a
macro that will loop a few thousand times. Word finds all of the form
fields in the active document once only, stores them, and can thus access
each one by its serial number (1) or (2).

The keyword ³Result² simply means ³The text the form field prints².

The ³&² is the concatenation character. In VBA you can also use the ³+²
sign to do exactly the same thing (add the two strings together). But its
very poor coding practice to use a + sign: it won¹t fail if you try it, but
it won¹t warn you if the two things you are adding are not strings either.
On the other hand, the next coder to see your code can¹t tell whether you
are adding or concatenating: so use the nice ampersand :)

Can I recommend a book? Yes: the Word Help. That¹s how and where I learned
it. However, I do not recommend that ANYONE attempt to learn VBA in Mac
Word: the Help has been castrated, and so has the Visual Basic Editor. You
will never make sense of it.

I assume you have MS Office 2004 Professional? So load up Virtual PC and
drop in a copy of Microsoft Word 2003 (or 2002 or 2000). Since you actually
want the code you write to work in Word 2004 when it¹s finished, you will be
better off with a copy of Word 2000: its VBA is most closely similar to Word
2004¹s. It¹s also a nicer product: stable and rugged. And you should find
it on eBay for about fifteen bucks :) Word 2002 is a toxic bug-farm, Word
2003 is a nice product but it¹s overkill on the Mac. It has quite a few
capabilities in VBA that are not available in Mac Word, and it¹s a bit too
power-hungry to be nice to use on a Mac. That said, I write half the code I
post to these news groups in Word 2003 in VPC. It runs like treacle in
winter on a Mac, but it gets you there...

You won¹t understand what you¹re missing until you try it: the Visual Basic
Environment automatically prompts you through in Word 2003, and the Help is
far, far more comprehensive than it is on the Mac. I can write code in ten
minutes in Word 2003 that would take me more than an hour to write on the
Mac, because I would have to stop and look up everything that Word 2003 just
fills in for me as I go. Word 2000 is probably even nicer: Word 2003 gets
in the way sometimes trying to be too helpful.

You still want a book, huh?

Start here: it¹s free:
http://msdn.microsoft.com/library/default.asp?URL=/library/officedev/odeopg/
deovroffice2000visualbasicprogrammersguide.htm

Then one of these:
http://www.word.mvps.org/Tutorials/BookRecommendations.htm

Generally, I advise people not to buy books on Word: any book you can buy is
out of date by the time you get it. Any book written about the current
version of Word was written by someone who has never seen the current
version of Word! (Why? It takes two years to write a book; it takes two
years to make a new version of Word: you do the math...)

However, Mac Word 2004 basically contains Word 2000 VBA, so any book written
about Word 2000 VBA will be very accurate for Word 2004.

Now: Your hyperlink. No, it is not possible to have the hyperlink
automatically address the document as an attachment. Not in Mac Word.
Sorry about that: that¹s one of the things they left out on the Mac. You
can automatically address an email using a mailto: hyperlink, or you can
automatically attach the document to a blank email using the
activedocument.sendmail command. You can¹t do both.

You CAN do it in AppleScript. And if you are very nice, Paul Berkowitz will
be along in a minute to tell you how...

Hope this helps

Thank you so much John; like the cavalry coming over the hill, I knew
you'd have the answer.

Two more questions, though...

1) Filling in your macro script, I may as well have been writing
Spanish. I have absolutely no idea why this works or what any of the
words you gave me, mean. Can you recommend a book which might explain
Macro scripting for the uninitiated?

2) And there was a second part to the original query...
Is it possible to automate attaching the saved form to a hyperlinked
return email address (which launches the user's email client, addresses
the mail and inserts a subject line)?

Thanks again

Tim

--

Please reply to the newsgroup to maintain the thread. Please do not email
me unless I ask you to.

John McGhie <[email protected]>
Microsoft MVP, Word and Word for Macintosh. Consultant Technical Writer
Sydney, Australia +61 4 1209 1410
 
P

Paul Berkowitz

Well, it can be done if you know in advance which email client will be used.
AppleScripting is different for each mail client. Otherwise, all that can be
done is opening an email message in the user's default mail client addressed
to a recipient. No subject line, no attachment. And that's exactly the same
as a mailto: hyperlink will get you. OR use Word AppleScript (send mail) to
send the document as an attachment to the default mail client, just the same
as in VBA: the choice is the same here.

To help narrow things down, it doesn't appear that you can script adding
attachments in Apple Mail. So if you're willing to assume that the user is
ready and willing to use Entourage (if he has Word, he most probably has
Entourage, but he may not use it or even have any account settings for it,
and thus be unable to send mail), then, yes, it can all be scripted quite
easily, and even incorporated into a VBA macro as via the MacScript command,
which accesses AppleScript. Or you could do the whole thing in AppleScript
instead of VBA.

But first you have to decide if you can insist on Entourage in order to get
the whole thing automated, or just be content with a mailto link in the
default email program where the user adds his own subject and drags his own
attachment, or attaching the document to a blank email (the user fills in
own recipient and subject line).


--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 
M

moonunit

Guys,
Many apologies for not posting my thanks sooner...

I am relieved that you describe the Mac help as 'castrated' - I though
my comprehension was severely tested, but I didn't really know why.

The whole rationale behind my Word 'form' was to find a cross-platfor
'form' which could be e-mailed out, filled-in and e-mail returned an
then imported into a FileMakerPro db. I didn't (and don't!) want t
become any more of an expert in Word, above and beyond this mino
issue.

However, that's not to say that I won't have a couple more queries i
due course! I need to spend some more time with the form so I ca
explain better...

Thanks again.

Ti
 

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