Insert text at end of Word document

J

Jim Burke in Novi

How do I insert a string of text at the very end of a Word document? I just
want to take an existing document and add some text to the very end of it,
with a blank line before the new text. Any help is appreciated!
 
J

Jim Burke in Novi

I know - I want to open a Word document from Access and insert some text at
the end of the document.
 
J

Jim Burke in Novi

I got it to work, but if I try it a second time I get this error:

The remote server machine does not exist or is unavailable.

The user will potentially be doing this several times in a session, so
having it only work the first time does me no good. Here's the code I have:

Dim myRange As Range
Dim myDoc As Word.Document
Dim objWord As Word.Application

Set objWord = New Word.Application
Set myDoc = objWord.Documents.Add(docName, , , False)
Set myRange = myDoc.Range
myRange.Select
Selection.InsertAfter newText
objWord.ActiveDocument.SaveAs docName
objWord.ActiveDocument.Close
objWord.Quit
Set objWord = Nothing

'docName' is a variable with the fully qualified document name and 'newText'
is a variable that will have the text I want to insert. I also need to have a
blank line inserted before my new line of text is inserted. Right now it's
just appending it to the end of the last line of the document. I want to go
to the end of the document, then have a blank line inserted, then my new line
of text on a new line after that. I know VBA fairly well, but don't know the
Word object model very well (as is probably fairly obvious)
 
P

pietlinden

I got it to work, but if I try it a second time I get this error:

The remote server machine does not exist or is unavailable.

The user will potentially be doing this several times in a session, so
having it only work the first time does me no good. Here's the code I have:

   Dim myRange As Range
   Dim myDoc As Word.Document
   Dim objWord As Word.Application

   Set objWord = New Word.Application
   Set myDoc = objWord.Documents.Add(docName, , , False)
   Set myRange = myDoc.Range
   myRange.Select
   Selection.InsertAfter newText
   objWord.ActiveDocument.SaveAs docName
   objWord.ActiveDocument.Close
   objWord.Quit
   Set objWord = Nothing

'docName' is a variable with the fully qualified document name and 'newText'
is a variable that will have the text I want to insert. I also need to have a
blank line inserted before my new line of text is inserted. Right now it's
just appending it to the end of the last line of the document. I want to go
to the end of the document, then have a blank line inserted, then my new line
of text on a new line after that. I know VBA fairly well, but don't know the
Word object model very well (as is probably fairly obvious)
I cheated and looked in the help file... (maybe I should test it?)
Set myRange = Documents("Changes.doc").Content
myRange.InsertAfter "the end."

Okay, it works...
docWord is set to point to an open document... (just FYI)

docWord.Content.InsertParagraphAfter
docWord.Content.InsertAfter "THE END"
 
J

Jim Burke in Novi

I'm getting the insert to work, but then I get errors. With my posted method
I'd used earlier, it worked the first time, no problem, but if I tried it a
second time I got the 'The remote server machine does not exist or is
unavailable.' error message. Trying either of the methods you posted does the
insert OK, but then it gives me an InsertAfter error message!

I tried this:

Set myRange = myDoc.Content
myRange.InsertParagraphAfter
myRange.InsertAfter "New and last ending"

And this

myDoc.Content.InsertParagraphAfter
myDoc.Content.InsertAfter "THE END"

Both of these insert my text starting on a new line,but give me the exact
same error message - Method 'InsertAfter' of object 'Range' failed. The thing
is, it does the Insert - the text is there. But it's giving me that error
message for some reason.
 
R

RoyVidar

Jim said:
I got it to work, but if I try it a second time I get this error:

The remote server machine does not exist or is unavailable.

The user will potentially be doing this several times in a session,
so having it only work the first time does me no good. Here's the
code I have:

Dim myRange As Range
Dim myDoc As Word.Document
Dim objWord As Word.Application

Set objWord = New Word.Application
Set myDoc = objWord.Documents.Add(docName, , , False)
Set myRange = myDoc.Range
myRange.Select
Selection.InsertAfter newText
objWord.ActiveDocument.SaveAs docName
objWord.ActiveDocument.Close
objWord.Quit
Set objWord = Nothing

'docName' is a variable with the fully qualified document name and
'newText' is a variable that will have the text I want to insert. I
also need to have a blank line inserted before my new line of text
is inserted. Right now it's just appending it to the end of the last
line of the document. I want to go to the end of the document, then
have a blank line inserted, then my new line of text on a new line
after that. I know VBA fairly well, but don't know the Word object
model very well (as is probably fairly obvious)

The reason you get the error the second time (and probably also have
an extra instance of Word in memory), is most likely becuase you're
using an unqualified reference to the Word Selection object.

Here's some more info http://support.microsoft.com/kb/319832
http://support.microsoft.com/default.aspx?kbid=178510

There might also be some problems related to the implicit referencing
through ActiveDocument, which sometimes might give additional
challenges.

I'm worndering - you're saying that you wish to add to existing
documents, but you're using the .Add method of the documents
collection, which will create a new document. Did you mean to use
the .Open method?

Try

Dim oWord As Word.Application
Dim oDoc As Word.Document

Set oWord = CreateObject("Word.Application")
Set oDoc = objWord.Documents.Open(docName)
oDoc.Select ' select contents of doc
With oWord.Selection
.EndKey Unit:=wdStory ' move cursor to the end
.TypeParagraph ' add paragraph/line
.TypeText Text:=newText
.TypeParagraph ' add paragraph/line
End With
oDoc.Save
oDoc.Close
set oDoc = nothing
oWord.Quit
Set oWord = Nothing
 
J

Jim Burke in Novi

That did the trick. Thanks. One of these days I'll sit down and take some
time to learn about the Word object model. It's not exactly intuitive! Thanks
again.
 

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