Something simple-----

MagicCity33

Member
Joined
Oct 5, 2005
Messages
7
Location
Birmingham
Programming Experience
3-5
I'm trying my hardest to get adjusted to writing code in VB.NET and I have something that is eluding me.

If I want the text in txtgb1Age.Text to be placed into a word doc I normally do something like this

dim MyAge as string

MyAge = txtgb1Age.TEXT

.ActiveDocument.Bookmarks.Item("age").Range.Text = Microsoft.VisiualBasic.Format(MyAge, "##")

But I wanted to try and use the actual textbox as the varible this time and I don't understand why it returns "".

.ActiveDocument.Bookmarks.Item("age").Range.Text = Microsoft.VisiualBasic.Format(objfrmMain.txtgb1Age.Text, "##"):mad:

objfrmMain is dim as objfrMain as New FrmMain

What am I missing here?

Everything works fine as long as I create new variables but I would like to get away from that since I have had up to 50 public variables listed in a mod.

Also

The code above is listed in a module under a function called Print_WD_Document.

Thanks in Advance:confused:
 
Last edited:
Try using:

.ActiveDocument.Bookmarks.Item("age").Range.Text = String.Format("{0:d}", objfrmMain.txtgb1Age.Text)
 
Sorry, I just re-read your question. My previous post probably won't make a difference. I would stop your app right before the .ActiveDocument.... line and make sure that objfrmMain.txtgb1Age.Text actually contains something. Maybe it is being changed at some point?
 
I did that. That's how I discovered that I was getting nothing. The values are not retained after it gets into the function that is in the module.

It works if I set up a public variable then set the variable = textbox before going to the function.

I just don't understand why I have to do it this way. Seems like I would be able to use the textbox as the var as long as it is called under its respective form.
 
you might want to check this:
objfrmMain is dim as objfrMain as New FrmMain

did you make sure that in the Load event of FrmMain you set:
objfrMain = Me

and in the module you've declared:
Friend objfrMain as FrmMain

?

should be something like this:
VB.NET:
Friend Module Module1
   Friend objfrmMain as FrmMain
End Module

Public Class FrmMain

 " Windows Forms Generated Code "

  Private Sub FrmMain_Load (...) Handles Mybase.Load
    objfrmMain = Me
  End Sub
End Class
then you should be able to use:
VB.NET:
 .ActiveDocument.Bookmarks.Item("age").Range.Text = Microsoft.VisiualBasic.Format[B]([U]objfrmMain.txtgb1Age.Text[/U][/B], "##")
------------------------------------------------------------
from the sounds of it you're making a new instance of the main form then using that new instance of it to get the Age (which is just an empty textbox because the form was never opened) and you dont want to do that in this case
 
Something Simple

Hey Thanks!!!

That did it.

I have objFrmMain set as public in the module but I didn't have

objfrmMain = ME

in the form load event.

Why do you have to do it this way though?
 
objfrmMain (why the double Hungarian?) is a null reference (basically an empty variable, i.e. Nothing) until you assign something to it. You can't expect it to just automatically know which form it's supposed to refer to. You have to tell it somewhere.
 
Back
Top