Code not executing

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
 
J

Jay Freedman

Hi, Con,

Just sticking the name jsano into the macro isn't enough to tell VBA that
you're referring to a field with that name. You need to replace that (in
both places) with

ActiveDocument.FormFields("jsano").Result

You could have avoided this mistake by putting the command

Option Explicit

at the top of the module (or checking the option "Require variable
declaration" in the Tools > Options dialog of the VBA editor). This would
have told you that the variable jsano was not declared, and that would have
forced you to figure out what you really mean. :) See
http://www.mvps.org/word/FAQs/MacrosVBA/DeclareVariables.htm for more
explanation.
 
B

Barry Schwarz

Rename the macro AutoOpen.

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



<<Remove the del for email>>
 

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