Log in

View Full Version : Solved: from excel to word (fill bookmarks)



white_flag
01-06-2012, 03:12 AM
Hello

I have an excel file, that will open a word document with some bookmarks and then, try to fill those bookmarks, without delete them.The code is skipping those instructions:


Set r = doc.Bookmarks(strBM).Range
doc.Bookmarks.Add Name:=strBM, Range:=strText


the code to fill the bookmarks:


Sub FillBM(strBM As String, strText As String)


Dim doc As Document
Dim r As Range

On Error GoTo Err_Handler

Set doc = Documents.Open(ThisWorkbook.Path & "\tabel")
With doc
r.Text = strText
Set r = doc.Bookmarks(strBM).Range

doc.Bookmarks.Add Name:=strBM, Range:=strText

End With

Err_Handler:
Exit Sub

End Sub

white_flag
01-06-2012, 03:24 AM
r.Text = strText error 91 'object variable or with block variable not set
doc.Bookmarks.Add Name:=strBM, Range:=strText error 13 type mismatch

fumei
01-06-2012, 08:41 PM
doc.Bookmarks.Add Name:=strBM, Range:=strText will indeed give error 13, as Range requires a range object and strText is a string.

the error 91 comes from the fact r is Dim'd but never Set. Where is it skipping:
Set r = doc.Bookmarks(strBM).Range

this is not in the Sub

white_flag
01-07-2012, 04:04 AM
Hello Gerry, thx for watching this:

I putted like this to skip err 91

Sub FillBM(strBM As String, strText As String)

Dim doc As Document
Dim r As Range

Set doc = Documents.Open(ThisWorkbook.Path & "\tabel")
Set r = doc.Bookmarks(strBM).Range

With doc
r.Text = strText
doc.Bookmarks.Add Name:=strBM, Range:=strText
End With


End Sub


but the same error 13(missmatch) like you said range not equale to string.

what can be done?

white_flag
01-07-2012, 04:18 AM
I putted like this:


Sub FillBM(strBM As String, strText As String)
Dim doc As Document
Set doc = Documents.Open(ThisWorkbook.Path & "\tabel.doc")

With doc
.Bookmarks.Add Name:=strBM, Range:=strText
End With

End Sub



now err 4218

macropod
01-07-2012, 10:40 PM
Hi white_flag,

It really does look like you're blundering around in vba without any real idea of what you're doing. For example, I can't see any refernces to the Word application and you've even got lines of code in the wrong order:

r.Text = strText
Set r = doc.Bookmarks(strBM).Range
You can't write to the 'r' range before you've defined it!!!

Try something along the lines of:
Sub UpdateBookmark(BmkNm As String, NewTxt As String)
'Note: A reference to the Word object model is required
Dim wdApp As Word.Application
Set wdApp = Word.Application
Dim wdDoc As Word.Document
Dim BmkRng As Word.Range
With wdApp
Set wdDoc = .Documents.Open(ThisWorkbook.Path & "\tabel.doc")
With wdDoc
If .Bookmarks.Exists(BmkNm) Then
Set BmkRng = .Bookmarks(BmkNm).Range
BmkRng.Text = NewTxt
.Bookmarks.Add BmkNm, BmkRng
End If
End With
.Visible = True
End With
Set BmkRng = Nothing: Set wdDoc = Nothing: Set wdApp = Nothing
End Sub

fumei
01-08-2012, 01:05 AM
I agree it does seem you are stumbling around a bit. First of all, please clarify if you are executing this from Excel. If so, then for sure you need to do as macropod suggests (ensuring you DO have a reference to Word!). If you are executing from Excel then:

Dim doc As Document

will be meaningless to VBA. Document is not an Excel term.

macropod
01-08-2012, 01:41 AM
First of all, please clarify if you are executing this from Excel. If so, then for sure you need to do as macropod suggests (ensuring you DO have a reference to Word!). If you are executing from Excel then:

Dim doc As Document

will be meaningless to VBA. Document is not an Excel term.
Hi Gerry,

I took white_flag's initial post at face value:

I have an excel file, that will open a word document with some bookmarks and then, try to fill those bookmarks, without delete them.
This seems to be reinforced by the reference to 'ThisWorkbook.Path'.

white_flag
01-08-2012, 04:28 AM
Hello there

I have an word reference in the sub that will start the fillBmk not in sub fillBmk (based on my VBA small knowledge was enough). anyway, thank you, Paul and Gerry (for pointing me in the good direction).

have a nice day