Word Doc - Object by VBA

G

Gina

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
 
G

Guest

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
 
G

Gina

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!!



JaRa said:
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






Gina said:
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
 
G

Guest

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



Gina said:
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!!



JaRa said:
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






Gina said:
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
 
G

Guest

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


Gina said:
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!!



JaRa said:
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






Gina said:
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
 
G

Guest

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



Gina said:
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!!



JaRa said:
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






Gina said:
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
 

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