Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / March 2005

Tip: Looking for answers? Try searching our database.

Word Doc - Object by VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gina - 10 Mar 2005 10:40 GMT
Hi.

I posted before and got some ideas but I am still struggeling.
The final testing of my tool is a disaster (promised to hand it over on
Monday!)
Couldn't find any code around either.

What I try to do is to create an object
    Set WD = CreateObject("Word.Document")             ' WD is a public
variable in the module TransferToWord
    reVorlage = CurrentProject.Path & "\Rechnung1.dot"
    Set DC = Word.Documents.Add(reVorlage, , , True)

I use several different subs and functions to write different parts to word
at the end of each function I:   Set DC = Nothing
at the beginning of each function I do  Set DC =
GetObject(Word.ActiveDocument)

all works fine more or less... but now I had to find out that it isn't
working correctly

at the end of the transfer of records I cal a function to release word:

   Set DC = GetObject(Word.ActiveDocument)
   Set DC = Nothing : Set WD = Nothing

When I close the word application I cannot restart it without that message:
Remote-Server-Computer not existant or not available
I presume the template Rechnung1.dot or word itself must is still open.
when I don't initialise WD I get an activeX-Error mess.
Maybe a helpful person has probably a piece of code on how to create and
release that word document collection object
- or whatever it is,  please ???? please!!!

Gina
JaRa - 10 Mar 2005 12:27 GMT
Basically I tend to put a reference to the Microsoft Word application this
saves me the hasle of creating the object. A disadvantage to this technique
is that Access can not automatically downgrade the word object if you used a
higher version of the word object.

The benefit is that you have all the properties and methods at hand when
writing code in VBA.

This is some Code which I used for a client probably this gives you some
ideas.
As you can see I set a document object which I can easily reference from Code.

Public Sub Translate(Template As String, SaveAs As String, RecordSource As
String)
Dim rs As New ADODB.Recordset
Dim i As Integer
Dim wrd  As New Word.Application

   rs.Open "SELECT * FROM [" & RecordSource & "]",
CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly
   If Not rs.EOF Then
       Set doc = wrd.Documents.Open(Template)
       With doc.ActiveWindow
           .Selection.WholeStory
           .Selection.Find.ClearFormatting
           .Selection.Find.Replacement.ClearFormatting
           For i = 0 To rs.Fields.Count - 1
               With .Selection.Find
                   .Text = "<" & rs.Fields(i).Name & ">"
                   .Replacement.Text = Nz(rs.Fields(i), "")
                   .Forward = True
                   .Wrap = wdFindAsk
                   .Format = False
                   .MatchCase = False
                   .MatchWholeWord = False
                   .MatchWildcards = False
                   .MatchSoundsLike = False
                   .MatchAllWordForms = False
               End With
               .Selection.Find.Execute Replace:=wdReplaceAll
           Next i
       End With
       
       doc.SaveAs SaveAs
       
   End If
   
   Set doc = Nothing
   wrd.Quit
   Set wrd = Nothing
End Sub

Public

> Hi.
>
[quoted text clipped - 31 lines]
>
> Gina
Gina - 10 Mar 2005 13:29 GMT
JaRa
BIG BIG :))) Thanks

took your Dim wrd  As New Word.Application

and replaced all my GetObject(Word.ActiveDocument) refs to
GetObject(wrd.ActiveDocument)

(I still do not trust what I see )... but it makes sense that it will work
longer than just 2 minutes..

doesn't it ?!?!!

of course I have to create Dim wrd with the New !!! before it!!

Gina
... am very grateful!!!! for your code!!

> Basically I tend to put a reference to the Microsoft Word application this
> saves me the hasle of creating the object. A disadvantage to this technique
[quoted text clipped - 85 lines]
> >
> > Gina
JaRa - 10 Mar 2005 14:35 GMT
Don't forget to change

Set DC = Word.Documents.Add(reVorlage, , , True)

into

Set DC = wrd.Documents.Add(reVorlage, , , True)

either since you should always use your instantiated object and not just
word.documents

- Raoul

> JaRa
> BIG BIG :))) Thanks
[quoted text clipped - 108 lines]
> > >
> > > Gina
JaRa - 10 Mar 2005 14:35 GMT
Don't forget to change

Set DC = Word.Documents.Add(reVorlage, , , True)

into

Set DC = wrd.Documents.Add(reVorlage, , , True)

either since you should always use your instantiated object and not just
word.documents

- Raoul

> JaRa
> BIG BIG :))) Thanks
[quoted text clipped - 108 lines]
> > >
> > > Gina
JaRa - 10 Mar 2005 14:35 GMT
Don't forget to change

Set DC = Word.Documents.Add(reVorlage, , , True)

into

Set DC = wrd.Documents.Add(reVorlage, , , True)

either since you should always use your instantiated object and not just
word.documents

- Raoul

> JaRa
> BIG BIG :))) Thanks
[quoted text clipped - 108 lines]
> > >
> > > Gina
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.