Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2003

Tip: Looking for answers? Try searching our database.

when the document is loaded execute code does not run ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Con - 31 Oct 2003 09:39 GMT
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
JGM - 30 Oct 2003 19:06 GMT
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!
--
_______________________________________
Jean-Guy Marcil
jmarcil@sympatico.ca

> This is my first attempt at coding on vba and have created the following:
>
[quoted text clipped - 14 lines]
>  End If
> End Sub
Jezebel - 30 Oct 2003 21:34 GMT
> _______________________________________
> Dim ff As FormField
[quoted text clipped - 7 lines]
> Next ff
> _______________________________________

This is a bit cludgy. simpler is

On error resume next
ActiveDocument.FormFields("jsano").Result = "test"
on error goto 0
JGM - 30 Oct 2003 23:13 GMT
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
jmarcil@sympatico.ca

> > _______________________________________
> > Dim ff As FormField
[quoted text clipped - 13 lines]
> ActiveDocument.FormFields("jsano").Result = "test"
> on error goto 0
Jezebel - 31 Oct 2003 00:06 GMT
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)

> Hi Jezebel,
>
[quoted text clipped - 44 lines]
> > ActiveDocument.FormFields("jsano").Result = "test"
> > on error goto 0
JGM - 31 Oct 2003 02:53 GMT
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!

--
_______________________________________
Jean-Guy Marcil
jmarcil@sympatico.ca

> 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
[quoted text clipped - 51 lines]
> > > ActiveDocument.FormFields("jsano").Result = "test"
> > > on error goto 0
Con - 31 Oct 2003 20:25 GMT
thank you all for your assistance.

> 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
[quoted text clipped - 51 lines]
> > > ActiveDocument.FormFields("jsano").Result = "test"
> > > on error goto 0
 
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.