c7c4c7
asked on
I need some VBA code to move data from excel to Word Bookmarks
I want to use VBA code to send data to a Word Document from within Excel
I have an Excel workbook that contains multiple sheets. When the user presses a command button I want the data from the spread sheets to be moved into the Word Document, currently I have bookmarks defined at each of the spots that need to be filled in. After it's complete I want the workbook to be filed out to disk.
I'm open to suggstions other than bookmarks if there is a better way.
Please be specific in your code I'm just learning to use VBA, I tried to give more than 500 points but it won't let me
Thanks for the help
I have an Excel workbook that contains multiple sheets. When the user presses a command button I want the data from the spread sheets to be moved into the Word Document, currently I have bookmarks defined at each of the spots that need to be filled in. After it's complete I want the workbook to be filed out to disk.
I'm open to suggstions other than bookmarks if there is a better way.
Please be specific in your code I'm just learning to use VBA, I tried to give more than 500 points but it won't let me
Thanks for the help
ASKER
I'm not sure what you mean by more detail. I found some code on another question that looks like it's what I want. So I modified it to run from within Excel and here is what I have so far.
Private Sub Proposal_Click()
Dim x As Integer
Dim r As Range
Dim myWord As Object
Set myWord = CreateObject("Word.Applica tion")
Set myWord = GetObject("C:\Documents and Settings\gary\My Documents\Ziris\Roofmax\pr oposal.doc ")
x = myWord.Bookmarks.Count <=== I put this in so I could make sure I got at Bookmark
MsgBox x
myWord.Bookmarks ("Name") <==== I get the error here
End Sub
When I run it though I get a Run-Time error '5863 Application-defined or object defined error
The bookmark name is really "Name"
What is it's problem
Thanks for the help
Private Sub Proposal_Click()
Dim x As Integer
Dim r As Range
Dim myWord As Object
Set myWord = CreateObject("Word.Applica
Set myWord = GetObject("C:\Documents and Settings\gary\My Documents\Ziris\Roofmax\pr
x = myWord.Bookmarks.Count <=== I put this in so I could make sure I got at Bookmark
MsgBox x
myWord.Bookmarks ("Name") <==== I get the error here
End Sub
When I run it though I get a Run-Time error '5863 Application-defined or object defined error
The bookmark name is really "Name"
What is it's problem
Thanks for the help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is not working out the way I expected.
2 problems
1). What I wanted to happen was that the Bookmark would be replaced with the value I inserted from the code
What is think is happening - the value is being replacing or replacing the bookmark but the bookmark is invisible. I've tried to make the bookmark visible through tools-> options-> Show Bookmarks. But the bookmark values remain invisible so I cannot see the changes made by the code.
2).For some reason when I changed the code to
myWord.ActiveDocument.Clos e SaveChanges:=True from False
I keep getting a message saying that Normal.dot needs to be filed out.
Got any ideas??
Thanks
2 problems
1). What I wanted to happen was that the Bookmark would be replaced with the value I inserted from the code
What is think is happening - the value is being replacing or replacing the bookmark but the bookmark is invisible. I've tried to make the bookmark visible through tools-> options-> Show Bookmarks. But the bookmark values remain invisible so I cannot see the changes made by the code.
2).For some reason when I changed the code to
myWord.ActiveDocument.Clos
I keep getting a message saying that Normal.dot needs to be filed out.
Got any ideas??
Thanks
ASKER
Finally figured it out
Thanks for the help
Thanks for the help
You're very welcome.
Sorry I did not have the opportunity to return until today (Monday).
Would you mind posting your resultant code (extracting any personal data, if applicable) so future users of Experts Exchange can review what amendments you made?
If you would prefer not to, that's OK.
Thank you for closing the question so promptly in any respect.
BFN,
fp.
Sorry I did not have the opportunity to return until today (Monday).
Would you mind posting your resultant code (extracting any personal data, if applicable) so future users of Experts Exchange can review what amendments you made?
If you would prefer not to, that's OK.
Thank you for closing the question so promptly in any respect.
BFN,
fp.
ASKER
Sure, here's what I have so far, if you have any feedback on what I did let me know
Private Sub Proposal_Click()
Dim r As Range
Dim myWord As Object
Dim text As String
On Error GoTo UnexpectedError
Set myWord = CreateObject("Word.Applica tion")
myWord.Documents.Open Filename:="C:\Documents and Settings\gary\My Documents\proposal.doc"
myWord.ActiveDocument.Book marks("Cus tName").Se lect
text = Sheets("Calculator").Range ("iName")
myWord.Selection.TypeText text
myWord.ActiveDocument.Book marks("Cus tAddress") .Select
text = Sheets("Calculator").Range ("iAddress ")
myWord.Selection.TypeText text
myWord.ActiveDocument.Book marks("Cus tCityState ").Select
text = Sheets("Calculator").Range ("iCitySta te")
myWord.Selection.TypeText text
myWord.ActiveDocument.Book marks("Cus tPhone").S elect
text = Sheets("Calculator").Range ("iPhone")
myWord.Selection.TypeText text
myWord.ActiveDocument.Clos e SaveChanges:=True
myWord.Quit
UnexpectedError:
' Describe the error to the user.
MsgBox "Unexpected error" & _
Str$(Err.Number) & _
" in subroutine Proposal." & _
vbCrLf & _
Err.Description
myWord.Quit
Set myWord = Nothing
Exit Sub
Set myWord = Nothing
End Sub
Private Sub Proposal_Click()
Dim r As Range
Dim myWord As Object
Dim text As String
On Error GoTo UnexpectedError
Set myWord = CreateObject("Word.Applica
myWord.Documents.Open Filename:="C:\Documents and Settings\gary\My Documents\proposal.doc"
myWord.ActiveDocument.Book
text = Sheets("Calculator").Range
myWord.Selection.TypeText text
myWord.ActiveDocument.Book
text = Sheets("Calculator").Range
myWord.Selection.TypeText text
myWord.ActiveDocument.Book
text = Sheets("Calculator").Range
myWord.Selection.TypeText text
myWord.ActiveDocument.Book
text = Sheets("Calculator").Range
myWord.Selection.TypeText text
myWord.ActiveDocument.Clos
myWord.Quit
UnexpectedError:
' Describe the error to the user.
MsgBox "Unexpected error" & _
Str$(Err.Number) & _
" in subroutine Proposal." & _
vbCrLf & _
Err.Description
myWord.Quit
Set myWord = Nothing
Exit Sub
Set myWord = Nothing
End Sub
PS. Have commented in your related question:
[ https://www.experts-exchange.com/questions/21899966/How-do-I-create-a-loop-to-step-through-cells-in-a-Column.html ]
[ https://www.experts-exchange.com/questions/21899966/How-do-I-create-a-loop-to-step-through-cells-in-a-Column.html ]
Here's a PAQ where I contributed to achieve a similar goal (the second of the two links demonstrates how to use bookmarks to insert text)
"VBA: From Excel to Words..."
[ https://www.experts-exchange.com/questions/21613510/VBA-From-Excel-to-Words.html ]
"Populate Doc file with data from VB"
[ https://www.experts-exchange.com/questions/21673639/Populate-Doc-file-with-data-from-VB.html ]
Either bookmarks, or specific text that remains static in your document (as per the first link above) should work for your needs.
If you need any more tailored assistance for your own requirements, please add more detail to what you are trying to achieve.
Thanks.
BFN,
fp.