Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying data from a checkbox using bookmarks

Status
Not open for further replies.

PCVirgin

Programmer
Nov 11, 2003
75
GB
I am new to VBA and I hope I have posted this in the right forum.

I am trying to copy text from one opened Word document to another using a macro. In the macro I check to see if the source document is opened then move the data at each specified bookmark to another specified bookmark in the destination document.

I have two problems.

1) When I move data the narrative "FORMTEXT " appears before the added data.

2) I do not know how to move the data from a checkbox. I tried to put a bookmark in it but it's too small (or am I missing something??)


Private Sub Document_Open()
Dim strDocName As String
strDocName = "U:\Work\LHN Assessment - MASTER.doc"
If DocOpen(strDocName) = True Then
Dim appWord As Word.Application
Dim SrcDoc As Word.Document
Dim DestDoc As Word.Document
Set DestDoc = ActiveDocument
Set SrcDoc = Documents.Open("U:\Work\LHN Assessment - MASTER.doc")
' Set appWord = CreateObject("Word.Application")
'Sex'
DestDoc.Bookmarks("Text473").Range.Text = SrcDoc.Bookmarks("Text318").Range.Text

DestDoc.Bookmarks("Text487").Range.Text = SrcDoc.Bookmarks("Chk103").Range.Text

Else
MsgBox "LHN Assessment Document is closed"
End If

End Sub
 
1. If you are using DocumentOpen, then you are already in Word - you have an instance of Word. WHY are you creating another instance of Word? Although admitedly, you have commented it out.

then move the data at each specified bookmark to another specified bookmark in the destination document
2. If you have "FORMTEXT", then you are dealing with a formfield. It is much better to actually use formfield code, rather than code for the bookmark for the formfield. They are NOT the same. Formfields have bookmarks, they are not bookmarks themselves. The value contained in a formfield is .Result.

If "Text473" is a formfield, then to gets its value, use .Result, as in:
Code:
DestDoc.[b]FormFields[/b]("Text473").Result

3. "I do not know how to move the data from a checkbox. I tried to put a bookmark in it but it's too small (or am I missing something??)"

A checkbox value is either True (checked), or False (unchecked). You should also be getting an error if you have not unprotected the form.

4. You go to the bother of declaring a string variable, and giving it a value:
Code:
Dim strDocName As String
strDocName = "U:\Work\LHN Assessment - MASTER.doc"
but then never use it. You use the string literal:
Code:
  Set SrcDoc = Documents.Open("U:\Work\LHN Assessment - MASTER.doc")

5. It is good idea to use Document objects as you are doing.

6. Is DocOpen a boolean function that you have somewhere?
Code:
DocOpen(strDocName)
If so, then you do not technically need the = True. However, if it IS True:
Code:
strDocName = "U:\Work\LHN Assessment - MASTER.doc"
If DocOpen(strDocName) = True Then
  Dim appWord As Word.Application
  Dim SrcDoc As Word.Document
  Dim DestDoc As Word.Document
  Set DestDoc = ActiveDocument
  Set SrcDoc = Documents.Open("U:\Work\LHN Assessment - MASTER.doc")
.....why are you opening it again???? If it is open, then use your Document object for it.
Code:
If DocOpen(strDocName) = True Then
   Set SrcDoc = Documents(strDocName)

7. You can get the value (True/False) of a checkbox with .Result, but it returns a Long. So....
Code:
ActiveDocument.FormFields("Text1").Result = _
   ActiveDocument.FormFields("Check1").Result
puts "1" (if the chekcbox is checked), or "0" (if it is unchecked).

Obviously, it would not be difficult to change that to whatever text you like. Say you want to use "Checked" or "Unchecked"...
Code:
   If SrcDoc.FormFields("Check103").Result = 1 Then
      DestDoc.FormFields("Text487").Result = "Checked"
   Else
      DestDoc.FormFields("Text487").Result = "Unchecked"
   End If



"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top