Macro Help

L

LEU

What am I doing wrong? I have the following macro that is not working. I copy
a document from the network to my C drive, then in a new document I open my
form and run this macro:

Sub CmdPaste1_Click()
Dim DocA As Document
Set DocA = Documents.Open("c:\Temp.doc")
Set oBM = ActiveDocument.Bookmarks
With DocA
Text3.Value = oBM("Text41").Range.Text
Text4.Value = oBM("Text27").Range.Text
Text5.Value = oBM("Text28").Range.Text
Text6.Value = oBM("Text16").Range.Text
Cmb1.Value = oBM("DropDown1").Range.Text
Cmb2.Value = oBM("DropDown2").Range.Text
Cmb3.Value = oBM("DropDown3").Range.Text
DocA.Close wdDoNotSaveChanges
End With
Kill "c:\Temp.doc"
End Sub

What it does when it runs is it puts the word FORMTEXT at the beginning of
each text field. Instead of putting the text from DropDown1, 2 & 3, it puts
the word DROPDOWN in Cmd1, 2 & 3. Then if I click on my OK button on my form
to save the info to the document, it gives me a run-time error that says
Object has been deleted.
 
G

Greg Maxey

It is a little difficult trying to decipher what it is that you are trying
to do. I assume that c:\temp.doc is a file with some text formfields and
dropdown fields. You want the results of these fields put in your userform.

If this is true then you are dealing with FormFields and not Bookmarks.
What was your With DocA statement supposed to do? How is anyone suppose to
know what is going on when you click your OK button since you didn't post
the code. I will guess that if you are trying to do something with
c:\temp.doc then the error is thrown because you deleted the file with your
Kill statement.

Some adaptation of this might get you back on track.

Private Sub CommandButton1_Click()
Dim DocA As Document
Dim oFF As FormFields
Set DocA = Documents.Open("c:\Temp.doc")
Set oFF = ActiveDocument.FormFields
Me.TextBox1.Value = oFF("Text1").Result
Me.ComboBox1.Value = oFF("DropDown1").Result
DocA.Close
Kill "c:\Temp.doc"
Set DocA = Nothing
Set oFF = Nothing
End Sub
 
L

LEU

Thank you Greg. It works great now.

Greg Maxey said:
It is a little difficult trying to decipher what it is that you are trying
to do. I assume that c:\temp.doc is a file with some text formfields and
dropdown fields. You want the results of these fields put in your userform.

If this is true then you are dealing with FormFields and not Bookmarks.
What was your With DocA statement supposed to do? How is anyone suppose to
know what is going on when you click your OK button since you didn't post
the code. I will guess that if you are trying to do something with
c:\temp.doc then the error is thrown because you deleted the file with your
Kill statement.

Some adaptation of this might get you back on track.

Private Sub CommandButton1_Click()
Dim DocA As Document
Dim oFF As FormFields
Set DocA = Documents.Open("c:\Temp.doc")
Set oFF = ActiveDocument.FormFields
Me.TextBox1.Value = oFF("Text1").Result
Me.ComboBox1.Value = oFF("DropDown1").Result
DocA.Close
Kill "c:\Temp.doc"
Set DocA = Nothing
Set oFF = Nothing
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
 

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

Similar Threads


Top