Log in

View Full Version : Display data from a userform at its correct place in word



vjdb
03-20-2008, 08:24 AM
Hello,

I have just started VBA and produced a fairly nice userform, if I may say so. The thing is that I want to use for a template with a preprinted header and footnote. So for example the address should be placed 5,5 cm from the very top of the page and 2 cm from the leftside. Is there any way I can get my userform to put the data into the right position on the page?

lucas
03-20-2008, 08:59 AM
I would suggest using bookmarks. Search the word forum and check out this thread, then post back here with your questions. It would help if you could upload your document so we don't have to re-create it.

Hit post reply at the bottom left of the last post in the thread and then scroll down and look for a button that says "manage attachments"

http://www.vbaexpress.com/forum/showthread.php?t=11427

vjdb
03-25-2008, 06:27 AM
I have tried to use bookmarks but the problem is when users want to edit fields after insertion, they are likely to delete the bookmarks. Is there anyway to prevent this? Thanks in advance!

PS: I have attached the document!

lucas
03-25-2008, 06:44 AM
vj, I understand what you are saying but I don't understand why it is an issue. If you are creating a new document using the template and you then send it to someone then they will not have the template that contains the userform anyway.

If you want to be able to insert/retrieve, etc. data from bookmarks take a look at this (http://www.vbaexpress.com/forum/showthread.php?t=11427) thread for some insight. I found it by doing what I suggested in my previous post....search the forumns.

vjdb
03-25-2008, 08:09 AM
I have read the threath you provided. The thing is that i want to use the attached document to make a template which can be used by everybody of the firm. And i'm afraid that they are going to delete the bookmarks when editing text, is their anyway the bookmarks can be made unremvable? TIA!

lucas
03-25-2008, 09:08 AM
Well you could use a userform to add or edit data and have the sheet protected when the form is not open.......see attached

fumei
03-25-2008, 09:50 AM
vjbd, you must define, exactly, what are your requirements, and build from there.

You could use formfields. You could have a userform get the starting information, dump that into formfields, which the user could edit.

However, using formfields has its own issues. Without knowing your real requirements, it is difficult to say what route you is best for you.

vjdb
03-26-2008, 05:51 AM
This is a good solution but I can still remove the bookmarks in the document. I'm going to try to get this code working with my document. Thank you very much for helping me out!

vjdb
03-26-2008, 05:53 AM
@ fumei First of thank's for the help you huys offer me! So here is hat i would like to have:
1) A splash screen (userform) on which the users can fill out fields like address, company name and contact person.
2) Users press ok and the data from the fields show up at the right position in the document.
3) The users are able to edit the data imported via the userform and are also able to type their actual message, the letter or Fax they want to sent to the recipient. This should be done in the body which is just plain text.
4) The actual goal is to make sure that all outgoining communication leaves the firm in the same layout.

What is the best practise for this situation? I have done it via bookmarks but these bookmarks can easily be moved or removed by the users.

lucas
03-26-2008, 06:09 AM
vj, what I do is just use another bookmark for the body text.....

For editing what has been entered:
If you look at my example in post #6 there is a menu item for opening the form again after you have closed it.....it is to the right of help on the main menu. It is named "Input Data." As long as the template is kept in the same place the userform will still be available. You just need to add a large textbox for the body text to the userform.

All data entry and editing is done through the userform and when the userform is closed, the document is protected........

Word is not my strong suit. Gerry probably has some better solutions.

lucas
03-26-2008, 06:15 AM
This is a good solution but I can still remove the bookmarks in the document. I'm going to try to get this code working with my document. Thank you very much for helping me out!


Is this in reference to my example? If so then you mean they can figure out the password and unprotect the document and remove the bookmarks.......is that what you mean?

vjdb
03-26-2008, 06:31 AM
Yes it is in reference to your example, I can remove the bookmarks and edit the data in your document. This off course after closing the userform. I use word 2007.

edit: this is strange, after opening the exmple.dot file again I am not able to edit anything in the document itself, only via the userform. Which is exactly the way it should be :) So I think this is the best practise, but my coding skills are not that good so i have to wresle a bit to git this working for the attached document post #5. Ca n you give a clue on how to get a field which can be edited, the so -called body? TIA!

lucas
03-26-2008, 06:37 AM
uh oh, vj do you mean the sheet is not protected when you close the userform? Or do you mean you are able to go to tools-protection and enter the password and unprotect the document?

lucas
03-26-2008, 07:27 AM
just add the textboxes and set their properties to mult-line.
add the bookmarks to the page.
add the textbox change event for each of the new textboxes:
Private Sub TextBox3_Change()
wb "body1", TextBox3
End Sub
Private Sub TextBox4_Change()
wb "body2", TextBox4
End Sub
and add two lines the initialize statement to load the info in the textboxes when the form is opened.
Private Sub UserForm_Initialize()
unprotectdoc
TextBox1.SetFocus
TextBox1.Text = ActiveDocument.Bookmarks("to").Range.Text
TextBox2.Text = ActiveDocument.Bookmarks("project").Range.Text
TextBox3.Text = ActiveDocument.Bookmarks("body1").Range.Text
TextBox4.Text = ActiveDocument.Bookmarks("body2").Range.Text
End Sub

vjdb
03-26-2008, 08:01 AM
Okay, we are getting somewhere:bow: But i have a big line of coding in de "OK" button (aka "Akkoord") Including bookmarks can these be removed then? This is the code:
Private Sub cmdAkkoord_Click()
Dim strSI As String
Dim strAanhef As String
Dim strAfsluiting As String
Dim strTav As String
Dim strHM As String
Dim strUwkenmerk As String
Dim strOnskenmerk As String
Dim strDatumPlaats As String
Dim strBehandeld As String
If optTNederlands = True Then
strTav = "T.a.v."
strUwkenmerk = "Uw kenmerk"
strOnskenmerk = "Ons kenmerk"
strDatumPlaats = "Datum & Plaats"
strBehandeld = "Behandeld door"
End If
If optSiSchuitemaker = True Then strSI = _
"Schuitemaker Machines B.V." Else strSI = "Schuitemaker Industrials B.V."
If optHeer = True And optTNederlands = True Then strHM = "Heer"
If optMevrouw = True And optTNederlands = True Then strHM = "Mevrouw"
If optFormeel = True And optTNederlands = True Then strAanhef = "Geachte"
If optVriendelijk = True And optTNederlands = True Then strAanhef = "Beste"
If optFormeel = True And optTNederlands = True Then strAfsluiting = "Hoogachtend,"
If optVriendelijk = True And optTNederlands = _
True Then strAfsluiting = "Met vriendelijke groet,"
If optTEngels = True Then
strTav = "Attn."
strUwkenmerk = "Your reference"
strOnskenmerk = "Our reference"
strDatumPlaats = "Date & Place"
strBehandeld = "Threated by"
End If
If optHeer = True And optTEngels = True Then strHM = "Sir"
If optMevrouw = True And optTEngels = True Then strHM = "Madam"
If optFormeel = True And optTEngels = True Then strAanhef = "Dear"
If optVriendelijk = True And optTEngels = True Then strAanhef = "Dear"
If optFormeel = True And optTEngels = True Then strAfsluiting = "Yours sincerely"
If optVriendelijk = True And optTEngels = True Then strAfsluiting = "Kind regards"

Application.ScreenUpdating = False
With ActiveDocument
.Bookmarks("Bedrijf").Range.Text = txtNaam
.Bookmarks("TAV").Range.Text = strTav
.Bookmarks("TAVV").Range.Text = txtTavVoornaam
.Bookmarks("TAVA").Range.Text = txtTavAchternaam
.Bookmarks("Adres").Range.Text = txtTavAchternaam
.Bookmarks("UwKenmerk").Range.Text = strUwkenmerk
.Bookmarks("UwKenmerkInput").Range.Text = txtUwkenmerk
.Bookmarks("Onskenmerk").Range.Text = strOnskenmerk
.Bookmarks("OnskenmerkInput").Range.Text = txtOnskenmerk
.Bookmarks("Datum").Range.Text = strDatumPlaats
.Bookmarks("DatumInput").Range.Text = txtDatum
.Bookmarks("BehandeldDoor").Range.Text = strBehandeld
.Bookmarks("BehandeldDoorInput").Range.Text = txtBehandeld
.Bookmarks("Functie").Range.Text = cboFunctie.Value
.Bookmarks("Aanhef").Range.Text = strAanhef
.Bookmarks("HM1").Range.Text = strHM
.Bookmarks("Achternaam").Range.Text = txtTavAchternaam
.Bookmarks("Afsluiting").Range.Text = strAfsluiting
.Bookmarks("SI").Range.Text = strSI
.Bookmarks("AfsluitingBehandeld").Range.Text = txtBehandeld
.Bookmarks("AfsluitingFunctie").Range.Text = cboFunctie.Value

Excuse me for not knowing this, I am a true newby!

lucas
03-26-2008, 08:07 AM
If optTNederlands = True Then


I would say that this is an if statment associated with an option button....do you have an option button on your userform with the name "optTNederlands"?

vjdb
03-26-2008, 08:10 AM
yes i do, i'm working with the document which is attached to message #5 of this threath.

lucas
03-26-2008, 08:23 AM
ah, yes it needs to stay. You can add the code easily though. You have a lot of work to do but I think you have enough clues to get you started. I'm not sure from your posts if you are going to use the existing textboxes for the body or if you are going to add them but you can see that it is not that difficult.......

vjdb
03-26-2008, 08:46 AM
Thank you for tour advice, I'm going to work on it. The protect and unprotect commands do't seem to work, am I on the right path with this line of codes?:
Option Explicit
Dim bname As String
Private Sub wb(bname, ByVal inhalt As String)
'Steiner http://www.vbaexpress.com/kb/getarticle.php?kb_id=126
If ActiveDocument.Bookmarks.Exists(bname) Then
Dim r As Range
Set r = ActiveDocument.Bookmarks(bname).Range
r.Text = inhalt
ActiveDocument.Bookmarks.Add bname, r
Else
Debug.Print "Bookmark not found: " & bname
End If
End Sub
Private Sub cmdAnnuleren_Click()
Unload Me
ActiveDocument.Close SaveChanges:=False
End Sub
Private Sub cmdReset_Click()

optSiSchuitemaker.Value = True
optFormeel.Value = True
optTNederlands.Value = True
txtNaam.Value = Null
txtAdres.Value = Null
txtTavVoornaam.Value = Null
txtTavAchternaam.Value = Null
txtUwkenmerk.Value = Null
txtOnskenmerk.Value = Null
txtDatum.Value = Null
txtBehandeld.Value = Null
cboFunctie.Value = Null
End Sub
Private Sub cmdAkkoord_Click()
Dim strSI As String
Dim strAanhef As String
Dim strAfsluiting As String
Dim strTav As String
Dim strHM As String
Dim strUwkenmerk As String
Dim strOnskenmerk As String
Dim strDatumPlaats As String
Dim strBehandeld As String
If optTNederlands = True Then
strTav = "T.a.v."
strUwkenmerk = "Uw kenmerk"
strOnskenmerk = "Ons kenmerk"
strDatumPlaats = "Datum & Plaats"
strBehandeld = "Behandeld door"
End If
If optSiSchuitemaker = True Then strSI = "Schuitemaker Machines B.V." Else strSI = _
"Schuitemaker Industrials B.V."
If optHeer = True And optTNederlands = True Then strHM = "Heer"
If optMevrouw = True And optTNederlands = True Then strHM = "Mevrouw"
If optFormeel = True And optTNederlands = True Then strAanhef = "Geachte"
If optVriendelijk = True And optTNederlands = True Then strAanhef = "Beste"
If optFormeel = True And optTNederlands = True Then strAfsluiting = "Hoogachtend,"
If optVriendelijk = True And optTNederlands = True Then strAfsluiting = _
"Met vriendelijke groet,"
If optTEngels = True Then
strTav = "Attn."
strUwkenmerk = "Your reference"
strOnskenmerk = "Our reference"
strDatumPlaats = "Date & Place"
strBehandeld = "Threated by"
End If
If optHeer = True And optTEngels = True Then strHM = "Sir"
If optMevrouw = True And optTEngels = True Then strHM = "Madam"
If optFormeel = True And optTEngels = True Then strAanhef = "Dear"
If optVriendelijk = True And optTEngels = True Then strAanhef = "Dear"
If optFormeel = True And optTEngels = True Then strAfsluiting = "Yours sincerely"
If optVriendelijk = True And optTEngels = True Then strAfsluiting = "Kind regards"

Application.ScreenUpdating = False
With ActiveDocument
.Bookmarks("Bedrijf").Range.Text = txtNaam
.Bookmarks("TAV").Range.Text = strTav
.Bookmarks("TAVV").Range.Text = txtTavVoornaam
.Bookmarks("TAVA").Range.Text = txtTavAchternaam
.Bookmarks("Adres").Range.Text = txtTavAchternaam
.Bookmarks("UwKenmerk").Range.Text = strUwkenmerk
.Bookmarks("UwKenmerkInput").Range.Text = txtUwkenmerk
.Bookmarks("Onskenmerk").Range.Text = strOnskenmerk
.Bookmarks("OnskenmerkInput").Range.Text = txtOnskenmerk
.Bookmarks("Datum").Range.Text = strDatumPlaats
.Bookmarks("DatumInput").Range.Text = txtDatum
.Bookmarks("BehandeldDoor").Range.Text = strBehandeld
.Bookmarks("BehandeldDoorInput").Range.Text = txtBehandeld
.Bookmarks("Functie").Range.Text = cboFunctie.Value
.Bookmarks("Aanhef").Range.Text = strAanhef
.Bookmarks("HM1").Range.Text = strHM
.Bookmarks("Achternaam").Range.Text = txtTavAchternaam
.Bookmarks("Afsluiting").Range.Text = strAfsluiting
.Bookmarks("SI").Range.Text = strSI
.Bookmarks("AfsluitingBehandeld").Range.Text = txtBehandeld
.Bookmarks("AfsluitingFunctie").Range.Text = cboFunctie.Value


End With
Application.ScreenUpdating = True
protectdoc
Unload Me
End Sub
Private Sub txtNaam_Change()
wb "Bedrijf", txtNaam
End Sub
Private Sub txtAdres_Change()
wb "Adres", txtAdres
End Sub
Private Sub UserForm_Initialize()
unprotectdoc
txtNaam.SetFocus
txtNaam.Text = ActiveDocument.Bookmarks("Bedrijf").Range.Text
txtAdres.Text = ActiveDocument.Bookmarks("Adres").Range.Text
optSiSchuitemaker.Value = True
optFormeel.Value = True
optTNederlands.Value = True
txtDatum = Date & ", Rijssen"
With cboFunctie
.AddItem "Medewerker Sales/Marketing"
.AddItem "Medewerker Inkoop"
.AddItem "Vertegenwoordiger"
.AddItem "Financieel directeur"
.AddItem "Algemeen directeur"
End With
End Sub

lucas
03-26-2008, 11:29 AM
The protect and unprotect commands do't seem to work

remember that the words protectdoc and unprotectdoc are just calls to macros that reside in a standard module. You need to make sure the two macros are available to the userform......if they are not there you can just insert a standard module and past the two macros as seen in the screenshot ....

vjdb
03-28-2008, 05:10 AM
hello Lucas, This works fine for me, Thank you very much! The only thing that i don't get the strings to work. I get the following error message: "Variable undefined" (translated from Dutch) But i have defined the variable as string in the initialize sub. This is the line of coding i used:

Private Sub strDatumPlaats_Change()
wb "Datum", strDatumPlaats
End Sub
Private Sub UserForm_Initialize()

Dim strSI As String
Dim strNederland As String
Dim strPostbus As String
Dim strsiOnder As String
Dim strAanhef As String
Dim strAfsluiting As String
Dim strTav As String
Dim strHM As String
Dim strUwkenmerk As String
Dim strOnskenmerk As String
Dim strDatumPlaats As String
Dim strBehandeld As String
If optTNederlands = True Then
strTav = "T.a.v."
strUwkenmerk = "Uw kenmerk"
strOnskenmerk = "Ons kenmerk"
strDatumPlaats = "Datum & Plaats"
strBehandeld = "Behandeld door"
strPostbus = "Postbus"
strNederland = "Nederland"
End If
If optSiSchuitemaker = True Then strSI = "Schuitemaker Machines B.V." Else strSI = "Schuitemaker Industrials B.V."
If optSiSchuitemaker = True Then strsiOnder = "Machines" Else strsiOnder = "Industrials"
If optHeer = True And optTNederlands = True Then strHM = "Heer"
If optMevrouw = True And optTNederlands = True Then strHM = "Mevrouw"
If optFormeel = True And optTNederlands = True Then strAanhef = "Geachte"
If optVriendelijk = True And optTNederlands = True Then strAanhef = "Beste"
If optFormeel = True And optTNederlands = True Then strAfsluiting = "Hoogachtend,"
If optVriendelijk = True And optTNederlands = True Then strAfsluiting = "Met vriendelijke groet,"
If optTEngels = True Then
strTav = "Attn."
strUwkenmerk = "Your reference"
strOnskenmerk = "Our reference"
strDatumPlaats = "Date & Place"
strBehandeld = "Threated by"
strPostbus = "P.O. Box"
strNederland = "The Netherlands"
End If
If optHeer = True And optTEngels = True Then strHM = "Sir"
If optMevrouw = True And optTEngels = True Then strHM = "Madam"
If optFormeel = True And optTEngels = True Then strAanhef = "Dear"
If optVriendelijk = True And optTEngels = True Then strAanhef = "Dear"
If optFormeel = True And optTEngels = True Then strAfsluiting = "Yours sincerely"
If optVriendelijk = True And optTEngels = True Then strAfsluiting = "Kind regards"
Application.ScreenUpdating = True


txtNaam.SetFocus
txtNaam.Text = ActiveDocument.Bookmarks("Bedrijf").Range.Text
txtAdres.Text = ActiveDocument.Bookmarks("Adres").Range.Text
txtTavVoornaam.Text = ActiveDocument.Bookmarks("TAVV").Range.Text
txtTavAchternaam.Text = ActiveDocument.Bookmarks("TAVA").Range.Text
txtTavAchternaam.Text = ActiveDocument.Bookmarks("Achternaam").Range.Text
txtUwkenmerk.Text = ActiveDocument.Bookmarks("UwkenmerkInput").Range.Text
txtOnskenmerk.Text = ActiveDocument.Bookmarks("OnsKenmerkInput").Range.Text
txtDatum.Text = ActiveDocument.Bookmarks("DatumInput").Range.Text
txtBehandeld.Text = ActiveDocument.Bookmarks("BehandeldDoorInput").Range.Text
txtBehandeld.Text = ActiveDocument.Bookmarks("AfsluitingBehandeld").Range.Text

optSiSchuitemaker.Value = True
optFormeel.Value = True
optTNederlands.Value = True
txtDatum = Date & ", Rijssen"
With cboFunctie
.AddItem "Medewerker Sales/Marketing"
.AddItem "Medewerker Inkoop"
.AddItem "Vertegenwoordiger"
.AddItem "Financieel directeur"
.AddItem "Algemeen directeur"
End With
End Sub


Is it possible to let the strings behave in the same way as the textboxes? TIA!

vjdb
03-31-2008, 12:03 AM
Goodmorning! Does anybody have an idea? The exact question is: Can i use strings inside a bookmark? gr Koen

fumei
03-31-2008, 10:35 AM
"Can i use strings inside a bookmark? "

Yes of course.

As for your "Variable undefined" error, I strongly suggest you look up Scope.

"But i have defined the variable as string in the initialize sub"

Yes, you DO declare the variable strDatumPlaats in the Initialize Sub. Like this:

Private Sub strDatumPlaats_Change()
wb "Datum", strDatumPlaats
End Sub


Private Sub UserForm_Initialize()

' other stuff
Dim strDatumPlaats As String

' other stuff

And....that is where it stays...inside Initialize. It has NO existence outside the scope of Intialize. When Initialize finishes....strDatumPlaats As String...finishes as well. it is destroyed, and therefore:
Private Sub strDatumPlaats_Change()
wb "Datum", strDatumPlaats
End Sub
will fail as "variable undefined". There is no variable strDatumPlaats. It dies when Initialize terminated.

Use a global (Public) variable.

BTW: Sub strDatumPlaats_Change() is confusingly named.

strDatumPlaats would indicate a string variable

Sub strDatumPlaats_Change() indicates a control object - and what object is unknown - it could be a textbox - as control objects have a Change event.

strYadda is used for string variables
txtYadda is for a textbox control

vjdb
03-31-2008, 01:37 PM
Hello, I understand that it is possible to use strings with bookmarks but i want to use it like this: http://www.vbaexpress.com/kb/getarticle.php?kb_id=126 This makes the bookmark a container. It truly contains the value and therefore the value can be edited afterwords by calling the userfom in again. This works with textboxes, but I can't get it working with strings. I have to say that i am a newby. I hope you have an idea on how to solve it. Right now i have organized things like this: see attachment. The big disadvantage is that the userform cannot be called in again.

lucas
03-31-2008, 02:13 PM
The big disadvantage is that the userform cannot be called in again.

I don't understand. Why not?

lucas
03-31-2008, 02:31 PM
If you are using the document that is created from the template on the same hard drive/network you can still call the userform from the referenced template. see attached. run the template so that you have open a new document1....add your data and close the form....then look for the Menu Item next to help that says "input data" that will open your form again.

If you want the userform to travel with the document then that is a different story I think. Most likely you would have to move away from the idea of a template(.dot). And just duplicate the file each time to send to people at a different physical location.

This my understanding of how things work in Word. I'm sure(and I hope) Gerry or Tony will clear up any misunderstandings I may have exhibited in this post......

fumei
04-01-2008, 02:13 PM
"This works with textboxes, but I can't get it working with strings."

The contents of textboxes ARE strings.

vjdb
04-02-2008, 12:33 AM
If you are using the document that is created from the template on the same hard drive/network you can still call the userform from the referenced template. see attached. run the template so that you have open a new document1....add your data and close the form....then look for the Menu Item next to help that says "input data" that will open your form again.

This is true, i am able to open the formfield again but then the formfields are empty and when i provide new input this input ends up after the previously entered data in the sheet. This because the text is not part of the bookmarks like in this example http://www.vbaexpress.com/kb/getarticle.php?kb_id=126 . That is why i asked if it is possible to make selfdefined strings part of the bookmarks as well. For example this line of coding:
Private Sub cmdAkkoord_Click()
unprotectdoc

Dim strAanhef As String
Dim strAfsluiting As String
Dim strTav As String
Dim strHM As String
Dim strUwkenmerk As String
Dim strOnskenmerk As String
Dim strDatumPlaats As String
Dim strBehandeld As String
Dim strSI As String
Dim strPostbus As String
Dim strKVK As String
Dim strRekening As String
Dim strBTW As String
Dim strBTWtaal As String
Dim strIBAN As String
Dim strBIC As String

If optSiSchuitemaker = True Then
strSI = "Schuitemaker Machines B.V."
strKVK = "06067174"
strRekening = "59.01.35.929"
strBTW = "NL800699300B01"
strIBAN = "NL 00ABNB 0590135929"
strBIC = "B"
End If
If optSiIndustrials = True Then
strSI = "Schuitemaker Industrials B.V."
strKVK = "08154711"
strRekening = "50.40.67.478"
strBTW = "NL817452965B01"
strIBAN = "NL 53ABNA 0504067478"
strBIC = "A"
End If

How can i make these strings part of the bookmarks?

This does not work when i try something like this:
Private Sub strSI_Change()
wb "Bedrijf", strSI
End Sub

Private Sub UserForm_Initialize()

txtNaam.SetFocus
strSI.Text = ActiveDocument.Bookmarks("Bedrijf").Range.Text


I get the the Variable Undifined Error when i try this. I understand this has to do with the different subs. But where should i define the strings then?



If you want the userform to travel with the document then that is a different story I think. Most likely you would have to move away from the idea of a template(.dot). And just duplicate the file each time to send to people at a different physical location. --> it can stay on a local drive or network.

lucas
04-02-2008, 05:29 AM
I've just got a minute to look at this but I suggest you read Gerry's post #23 again. The variable scope is the problem. You have to declare it as a public varible outside of any of your macro's....at the top of the module right below "Option Explicit"

vjdb
04-02-2008, 06:27 AM
Thank you for you time Lucas, i did this and it works. But now i get an error Option Explicit
Dim bname As String
Dim strTest As String
Private Sub wb(bname, ByVal inhalt As String)
'Steiner http://www.vbaexpress.com/kb/getarticle.php?kb_id=126
If ActiveDocument.Bookmarks.Exists(bname) Then
Dim r As Range
Set r = ActiveDocument.Bookmarks(bname).Range
r.Text = inhalt
ActiveDocument.Bookmarks.Add bname, r
Else
Debug.Print "Bookmark not found: " & bname
End If
End Sub
Private Sub CommandButton2_Click()
If opt1 = True Then strTest = "Hello" Else strTest = "Working"
protectdoc
Unload Me
End Sub
Private Sub TextBox1_Change()
wb "to", TextBox1
End Sub
Private Sub TextBox2_Change()
wb "project", TextBox2
End Sub
Private Sub TextBox3_Change()
wb "body1", TextBox3
End Sub
Private Sub TextBox4_Change()
wb "body2", TextBox4
End Sub
Private Sub strTest_Change()
wb "Test", strTest
End Sub
Private Sub UserForm_Initialize()
unprotectdoc
TextBox1.SetFocus
TextBox1.Text = ActiveDocument.Bookmarks("to").Range.Text
TextBox2.Text = ActiveDocument.Bookmarks("project").Range.Text
TextBox3.Text = ActiveDocument.Bookmarks("body1").Range.Text
TextBox4.Text = ActiveDocument.Bookmarks("body2").Range.Text
strTest.Text = ActiveDocument.Bookmarks("Test").Range.Text
End Sub
The bold line gives the error: compile error: invalid qualification Any idea where this comes from?

fumei
04-02-2008, 11:09 AM
Excuse me?

strTest.Text = ActiveDocument.Bookmarks("Test").Range.Text


You declared strTest as a string. There is no such object qualification as strTest.Text!

BTW: while Dim strtest As String will work, you really should declare it as

Public strTest As String

I will say it again, be careful with your naming!

Private Sub strTest_Change()
wb "Test", strTest
End Sub

This looks like an OBJECT - as objects have Change events.

However, you declare srtTest as a string. Strings are not objects, they are variables.

In fact, as your code is:
TextBox4.Text = ActiveDocument.Bookmarks("body2").Range.Text
' this next is an object!....not a string
' BUT you declare it as a string
strTest.Text = ActiveDocument.Bookmarks("Test").Range.Text
this indicates to me that you have a STRING (strTest) declared, and you also have an OBJECT named strTest.

This is bad naming.

"The bold line gives the error: compile error: invalid qualification "

It IS an invalid qualification.

vjdb
04-07-2008, 07:14 AM
Hello Fumei,

I understand your point but my coding skill are not that good. I have no clue how to solve this. Should I transform the string to an object or is it possible to fill the bookmarks with strings? Can you give me a direction? TIA!

vjdb
04-08-2008, 03:51 AM
I have found a way to get it working. I used this line of coding:
Option Explicit
Dim bname As String
Public strTest As String
Private Sub wb(bname, ByVal inhalt As String)
'Steiner http://www.vbaexpress.com/kb/getarticle.php?kb_id=126
If ActiveDocument.Bookmarks.Exists(bname) Then
Dim r As Range
Set r = ActiveDocument.Bookmarks(bname).Range
r.Text = inhalt
ActiveDocument.Bookmarks.Add bname, r
Else
Debug.Print "Bookmark not found: " & bname
End If
End Sub
Private Sub CommandButton2_Click()
If opt1 = True Then strTest = "Hello" Else strTest = "Working"
Selection.GoTo What:=wdGoToBookmark, Name:="Test"
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.InsertAfter strTest
ActiveDocument.Bookmarks.Add Range:=Selection.Range, _
Name:="Test"
protectdoc
Unload Me
End Sub
Private Sub TextBox1_Change()
wb "to", TextBox1
End Sub
Private Sub TextBox2_Change()
wb "project", TextBox2
End Sub
Private Sub TextBox3_Change()
wb "body1", TextBox3
End Sub
Private Sub TextBox4_Change()
wb "body2", TextBox4
End Sub

Private Sub UserForm_Initialize()
unprotectdoc
TextBox1.SetFocus
TextBox1.Text = ActiveDocument.Bookmarks("to").Range.Text
TextBox2.Text = ActiveDocument.Bookmarks("project").Range.Text
TextBox3.Text = ActiveDocument.Bookmarks("body1").Range.Text
TextBox4.Text = ActiveDocument.Bookmarks("body2").Range.Text
End Sub

Is this how you guys would solve my problem? If so Thank You for all the help!
PS: better more compact solutions are not available?

vjdb
04-08-2008, 07:41 AM
Hello guys,

I have it worked out now, please see the attached file. The only thing is that i don't see the "Input data" menu item in word. Does anybody have an explanation for this? TIA!

lucas
04-08-2008, 08:50 AM
the macro and the custom menu were not in the template.

Click here (http://slucas.virtualave.net/Wink/CustomMenuItem.htm) for a flash demo on how to add a custom menu to Word.

I added the sub.....showform to module1 of your file and added the custom menu for you. You really should follow the link so you will understand how it works.

vjdb
04-09-2008, 01:53 AM
Thank you very much! It now works perfect! The only thing is that i can't find the tool-->customize button in word 2007 any idea's about this one? And one last question can the doc be saved without any vbacode in it? TIA again