VBA/Excel

HollyLouise

New Member
Joined
Feb 7, 2013
Messages
3
Hello All,

New to forum and new to VBA, so any comments would be appeciated. I want excel to open a word document template and place a information in a cell into the word document in between two words. (I write many reports and opposed to writing all the information into excel then word as I do now, I want a template in excel and a word template that will update as I change information in excel). I bookmarked the word which I want replaced in the word document "details" and asked excel to replace it with the value in the cell but it keeps saying Compile Error: sub or function not defined.

I want to reiterate, that I am a complete beginner to this, so any comments would much be appreciated.

My code is saved into a module in VBA and is

Private Sub importExcelData()
Dim wdApp As Object
Dim wdDoc As Object
Dim workBook As workBook
Dim dataInExcel As String
Application.ScreenUpdating = False
Set workBook = Workbooks.Open("C:\Holly\Spreadsheet\123.xls", True, True)
Edit "C:\Holly\Spreadsheet\123.xls"
dataInExcel = workBook.Worksheets("Details").Range("B2").Formula
Selection.TypeParagraph
Selection.TypeText Text:=dataInExcel
'open the word documents
Set wdApp = CreateObject("Word.Application")
Set wdDoc = wdApp.Documents.Open("C:\Holly\Template.docx")

'replace the bookmarks with the variables
FillBookmark wdDoc, sName, "Details"

'show the word document
wdApp.Visible = True

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
It was a code written by someone else and he had it in. If I cant do this, is there a code I can use to transfer data from excel to a template word document that will update if I change the data in word?

Thank you ever so much for your reply.

Kind Regards,

Holly
 
Upvote 0
Look for a Sub or Function with that name in the code written by the other person and copy it into your module.

Edit: I found it with a Google search:

Code:
Private Function fillbookmark(sBookmark As String, sValue As String, _
    odoc As word.document) As Boolean
    With odoc
        If .Bookmarks.Exists(sBookmark) Then
            .Bookmarks(sBookmark).Range.Text = sValue
            fillbookmark = True
        Else
            fillbookmark = False
        End If
    End With
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top