Code works - sometimes

M

Mr B

I have been attempting to add some text to a Word document via VBA.

Here is a snipit of my code:

Dim wordApp As Object
Dim WordDoc As Object
strNewDocPathAndName = strNewDocPath & "\" & strNewName & ".doc"
wordApp.ActiveDocument.SaveAs FileName:=strNewDocPathAndName
Set WordDoc = wordApp.ActiveDocument
WordDoc.Selection.TypeText Text:="This is the spot."

I have had a couple of times that the code above works to write the text to
the document. However, most of the time it just appears to skip the code
altogether.

I have tried several versions of the code with no success.

I'm sure there is something that I am just missing.

Any evaluation of my code would be appreciated.
 
B

Brendan Reynolds

In the code you posted, the variable 'wordApp' is declared, but never
instantiated. There is no call to CreateObject or GetObject.
 
M

Mr B

Thanks for the reply. In my actual code, I do have the variables
instantiated and there is a call to the GetObject along with a on error
statement that if the GetObject fails, the CreateObject is used.

I am not having any problem creating the instance of Word. I am also not
having any problem creating the document from a template and saving it to the
desired folder with the correct name.

Where I am having problems is that after saving the document I then check
for a condition and depending on that condition I want to place a line of
text in the document.

I only placed enough of my code to allow someone to attempt to tell me why
it does not write the line of text into the document. I'm just stumped. I've
tried several verations but nothing that is consistant.

The second option if the condition meets certain criteria is to insert a
previously prepared document instead of the text. Neither of these actions
happen on a consistant basis.

Still need you help.

Mr B


Brendan Reynolds said:
In the code you posted, the variable 'wordApp' is declared, but never
instantiated. There is no call to CreateObject or GetObject.
 
J

John Nurick

Hi B,

If this works when you step through the code in the debugger, but sometimes
fails when running at full speed, I'd investigate whether the problem is
related to the Allow Background Saves option in Word.
 
M

Mr B

John,

Thanks for the reply.

Actually, even when I just step through the code, it does not error out but
simply does nothing.

Any ideas?

Do I need to select a specific location in the document first? Like a
bookmark or somthing like that?

I have some code about this code that writes a date to a bookmarked location
and it works. I just really did not want to have to require users to build
in another bookmark in order to make this work.

Mr B


John Nurick said:
Hi B,

If this works when you step through the code in the debugger, but sometimes
fails when running at full speed, I'd investigate whether the problem is
related to the Allow Background Saves option in Word.
 
B

Brendan Reynolds

I appreciate your desire to be concise, but I think you may have erred on
the side of brevity. I think you'll need to post more of the code. In
particular, it might help if you posted the code that checks the
conditions - that would seem to be a leading candidate for the position of
prime suspect in the case.

--
Brendan Reynolds
Access MVP

Mr B said:
Thanks for the reply. In my actual code, I do have the variables
instantiated and there is a call to the GetObject along with a on error
statement that if the GetObject fails, the CreateObject is used.

I am not having any problem creating the instance of Word. I am also not
having any problem creating the document from a template and saving it to
the
desired folder with the correct name.

Where I am having problems is that after saving the document I then check
for a condition and depending on that condition I want to place a line of
text in the document.

I only placed enough of my code to allow someone to attempt to tell me why
it does not write the line of text into the document. I'm just stumped.
I've
tried several verations but nothing that is consistant.

The second option if the condition meets certain criteria is to insert a
previously prepared document instead of the text. Neither of these actions
happen on a consistant basis.

Still need you help.

Mr B
 
M

Mr B

Brendan,

Thanks again for your interest in try to assist me.

I am currently not at the location where I can get my hands on any more of
my code. I will be happy to post more of my code, however, when I step
throught the code, the "If" statements work just as expected but the
statements that are susposed to write the text to the document are being
executed, but nothing is being written to the document.

I am just wondering if I need to select a specific location in the document?

Just a thought.

Thanks again for any assistance.

Mr B
 
B

Brendan Reynolds

The following test worked for me ...

Public Sub TestSub()

Dim objApp As Word.Application
Dim objDoc As Word.Document

Set objApp = New Word.Application
Set objDoc = objApp.Documents.Add()
objApp.Selection.TypeText "Some Text"
objDoc.SaveAs "c:\usenet\test.doc"

End Sub

Note that the Selection property is not a property of the Word.Document
object, it is a property of the Word.Application object. Now, it may be that
in your actual code, you have 'WordApp.Selection', but I can't see your
actual code. I can only see your posted code, and in your posted code, you
have 'WordDoc.Selection'.

Do you see the difficulty here? Until you post the actual code, people are
likely to continue a) finding problems in the posted code that are not in
the actual code and b) not finding problems in the actual code because they
are not in the posted code.
 
M

Mr B

Brendan,

Thanks so much for your assistance.

First, the fact that the selection property is not a property of the
Word.Document object but a part of the Application object. When I fixed that
all worked as desired.

Second, the reason that I did not post the entire code is that it is quite
lengthy and if I had posted all the code I was afraid most people would never
have looked through it all anyway.

I kinda knew that the problem was some oversite on my part but for the life
of me I could not put a hand on it.

Thanks so much again for the help.

Mr B
 

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