Link to home
Start Free TrialLog in
Avatar of c7c4c7
c7c4c7Flag for United States of America

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
Avatar of [ fanpages ]
[ fanpages ]

Hi,

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.
Avatar of c7c4c7

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.Application")
    Set myWord = GetObject("C:\Documents and Settings\gary\My Documents\Ziris\Roofmax\proposal.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
ASKER CERTIFIED SOLUTION
Avatar of [ fanpages ]
[ fanpages ]

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of c7c4c7

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.Close SaveChanges:=True from False
     I keep getting a message saying that Normal.dot needs to be filed out.

Got any ideas??

Thanks
Avatar of c7c4c7

ASKER

Finally figured it out

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.
Avatar of c7c4c7

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.Application")
   
    myWord.Documents.Open Filename:="C:\Documents and Settings\gary\My Documents\proposal.doc"
    myWord.ActiveDocument.Bookmarks("CustName").Select
    text = Sheets("Calculator").Range("iName")
    myWord.Selection.TypeText text
    myWord.ActiveDocument.Bookmarks("CustAddress").Select
    text = Sheets("Calculator").Range("iAddress")
    myWord.Selection.TypeText text
    myWord.ActiveDocument.Bookmarks("CustCityState").Select
    text = Sheets("Calculator").Range("iCityState")
    myWord.Selection.TypeText text
    myWord.ActiveDocument.Bookmarks("CustPhone").Select
    text = Sheets("Calculator").Range("iPhone")
    myWord.Selection.TypeText text

    myWord.ActiveDocument.Close 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