Managing Word from Access vba

  • Thread starter Thread starter kevin.witty
  • Start date Start date
K

kevin.witty

I've used Albert D. Kallal's brilliant Merge20 to help me create a word
merge document, which ends up named "Form Letters 1". I can't figure out
the syntax to save that doc with a new name from within vba in Access. (In
fact I can't figure out how to do much of anything with Word vba since I get
no intellisense on it, and I can't find where it's documented!!) If anyone
can help me with the syntax, I'll appreciate it. If anyone can point to
where
the documentation is, or how to get intellisense turned on I may not have to
ask questions like this again <g>. Thanks, Kevin
 
you may want to start with the following. I cut and paste some of the codes
I used, so there may be some bug. The important thing you want to do is add
a Word library reference to your Access by opening the code and select
'Microsoft Word x.x object libary'


Private Sub GetWord(DOC As String)
On Error Resume Next
Dim appWord As Word.Application
Set appWord = GetObject(, "Word.Application") ' get the running
instance
If Err.Number = 429 Or Err.Number = 91 Or Err.Number = 462 Then
Err.Clear
Set appWord = GetObject("", "Word.Application") ' not there, create
a new instance
ElseIf Err.Number > 0 Then
MsgBox Err.Description
Exit Sub
End If

appWord.Documents.Add Access.CurrentProject.Path & "\" & DOC
appWord.Visible = True
appWord.Activate
End Sub
 
Hi,
I can't figure out the syntax to save that
doc with a new name from within vba in
Access.

Here's how to do that:

oMyWordObject.ActiveDocument.SaveAs "C:\NameOfWordDoc.doc"
 
(In
fact I can't figure out how to do much of anything with Word vba since I get
no intellisense on it

Unfortunately, my example uses late binding, so Intellisense does not work.
The downside is when you write code, it is very hard. The up side is that
the code works on every pc you deploy the software too!

To get intellisense back on, you have to set a reference to word (bad), and
then change code from late binding to early binding. In fact, a lot of
developers write and develop the code using Early binding, and the right
before deployment, they use switch the code to late binding. I wanted my
code to just run on whatever pc tried it..so late binding was a MUST to use.

You can start changing the code back to early binding for development.

In my sample code a few of the routines actually "hint", or show what object
name I had used when EARLY binding was in force (I also developed that code
with early binding). If you look at this code snip (from my merge example):

Dim strNewName As String
Dim wordApp As Object 'Word.Applicaton
Dim WordDoc As Object 'Word.Document

Note how the base object name is moved to the comments and the generic
"object" is used. I did not consistently do this through that example, but
in the above, you can clearly see the early binding defs I had original
used. So, you could change the above to:

Dim wordApp As Word.Applicaton
Dim WordDoc As Word.Document

In fact, often, in my code, you will see:

Dim wordApp As Object 'Word.Applicaton
'Dim wordApp As Word.Applicaton

Dim WordDoc As Object 'Word.Document
'Dim WordDoc As Word.Document

So, what one does is comment out the late code, and switch it back to early
for development. (don't forget to add, or remove the word ref in the
references).

Also, yes, when the users save the document, they are prompted for a file
name. You can provide a name, but that means YOUR CODE needs to come up with
both a name, and also a full pathname. Further, you need to ensure that path
name is legal, and you also need to ensure that the document name you give
does NOT already exist to prevent overwriting the existing document.

The suggest of:

WordApp.ActiveDocument.SaveAs "filename"

Should work fine
 
Aww, Albert, will it bother you very much if I tell you that you are a
genius and the MS developers are dodos? That if I'd had the information
I've gotten here in the past two weeks two weeks ago it would have saved me
about 6 days of frustration? That if the MS Developers had said, "Gee, do
you think that someone who has developed a beautiful invoice with a logo
imbedded in it in Access might want to email it or send it to Word? Naw!
That would be too much like an integrated suite!"

Umm, can you tell that I'm a little, shall we say, "disappointed" in MS?

Much as I love Merge20, I would have loved it even more if I'd seen

Dim wordApp As Object ' NO Intellisense, NOT reliant on
Word version, late binding, most flexible
'Dim wordApp As Word.Application ' Provides Intellisense,
reliant on Word version, early binding. most helpful
' Use it to develop your code and expose methods and procedures,
' but be aware that they may and probably will break when MS changes
its mind.
' Same applies to the below.

Dim WordDoc As Object 'Word.Document
'Dim WordDoc As Word.Document

It probably would have been even more helpful if the MS developers had
provided better ways of exposing methods and procedures and the parameters
they take, and better Help, but after 20 years of failing to do so, why
should they argue with success?

Again, thanks for Merge20. It's fabulous! Now I'm trying to figure out how
to use some of your methodology to create a Word Form.

Kevin
 
Aww, Albert, will it bother you very much if I tell you that you are a
genius and the MS developers are dodos? That if I'd had the information
I've gotten here in the past two weeks two weeks ago it would have saved me
about 6 days of frustration? That if the MS Developers had said, "Gee, do
you think that someone who has developed a beautiful invoice with a logo
imbedded in it in Access might want to email it or send it to Word? Naw!
That would be too much like an integrated suite!"

Umm, can you tell that I'm a little, shall we say, "disappointed" in MS?

Much as I love Merge20, I would have loved it even more if I'd seen

Dim wordApp As Object ' NO Intellisense, NOT reliant on
Word version, late binding, most flexible
'Dim wordApp As Word.Application ' Provides Intellisense,
reliant on Word version, early binding. most helpful
' Use it to develop your code and expose methods and procedures,
' but be aware that they may and probably will break when MS changes
its mind.
' Same applies to the below.

Dim WordDoc As Object 'Word.Document
'Dim WordDoc As Word.Document

It probably would have been even more helpful if the MS developers had
provided better ways of exposing methods and procedures and the parameters
they take, and better Help, but after 20 years of failing to do so, why
should they argue with success?

Again, thanks for Merge20. It's fabulous! Now I'm trying to figure out how
to use some of your methodology to create a Word Form.

Kevin
 
Kevin,
It would be nice if we could predict what every single customer in the world
could possibly want to do with our software, and could have predicted it so
many years ago when we first started integrating office into a suite, but
that's not realistic is it? That's why we provide the rich programming VBA
interface and object models, and why we have great newsgroups like this to
help people out when they get stuck.
Let's keep the expectations a little more grounded to earth, k? We're all
working hard here to make the Office suite the best it can be, and we're
continuously trying to make it better than it was yesterday.
 
Back
Top