when the document is loaded execute code does not run ?

J

JGM

Hi Con,

Your problem is quite simple, really. Next time, try the step by step
debugging (place the cursor inside the macro in the VBA editor and hit F8)
to get a clue as to why it does not work.

In your case, you now what "jsano" is, but the VBA compiler has no clue...
You have to define it somehow.

As it is
If jsano = "" Then
is always false because the compiler does not know what "jsano" is, but it
is not an error per se, so the code runs, but the Then part is never
executed... Anyway, I might be wrong as I am no expert, but that's what I
understand from looking at your code.

Play around with the following to get a sense of declaring objects in your
code:
_______________________________________
Dim ff As FormField

For Each ff In ActiveDocument.FormFields
If ff.Name = "jsano" Then
If ff.Result = "" Then
ff.Result = "test"
End If
End If
Next ff
_______________________________________

HTH
Cheers!
 
J

Jezebel

_______________________________________
Dim ff As FormField

For Each ff In ActiveDocument.FormFields
If ff.Name = "jsano" Then
If ff.Result = "" Then
ff.Result = "test"
End If
End If
Next ff
_______________________________________

This is a bit cludgy. simpler is

On error resume next
ActiveDocument.FormFields("jsano").Result = "test"
on error goto 0
 
J

JGM

Hi Jezebel,

Generally speaking, you are right, but, did you read the sentence I wrote
preceding my code? It is just that Con did not even include code that would
let the compiler know, or at least guess, that "jsano" was a formfield. So,
as I said, I just wanted him/her to undertsand that you cannot assign
properties to an undeclared object/variable as he had done in:
Private Sub Document_Open()
If jsano = "" Then
He had not declared what "jsano" was in his code and on top of that he was
working with assumed properties (the Result property of the formfield
"jsano")...

Also, Con's purpose was not to replace the content of "jsano" with a
predefined content, but to check if "jsano" was empty, and if so, to insert
some text from a source document.

So, I wrote that code to show him/her how it works because he/she said
he/she was new to VBA.

Finally, while your example is shorter and less cludgy, I do not think
he/she still could use it as is, you would have to provide him/her with a
more complete example to take into account that he/she was using this code
in an If statement. See his/her original post.

Cheers!

_____________________________________
Jean-Guy Marcil
(e-mail address removed)
 
J

Jezebel

sheesh, no need to be so defensive. Your explanation was fine, and indeed
your code would work. ('Exit For' after finding the target would be an
improvement -- no point going on looking once it's found)
 
J

JGM

Hi there,

Not defensive, just wanted to explain for Con's benefit.

You are right about the Exit For... I always forget that one!

Have a good one!
 
C

Con

This is my first attempt at coding on vba and have created the following:

I have a field names jsano in my document as a text field, it is a simple
update and it does not execute when the document loads, any help would be
greatly appreciated.

Private Sub Document_Open()
If jsano = "" Then
Open "c:\jsa.ctrl" For Input As #1
Input #1, njsa
Close #1
jsano = njsa
xjsa = njsa + 1
Open "jsa.ctrl" For Output As #1
Write #1, xjsa
Close #1
End If
End Sub
 
C

Con

thank you all for your assistance.


Jezebel said:
sheesh, no need to be so defensive. Your explanation was fine, and indeed
your code would work. ('Exit For' after finding the target would be an
improvement -- no point going on looking once it's found)
 

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