PDA

View Full Version : Help with VBA code Transfer excel range to Word bookmark



lsivak
02-03-2016, 03:24 PM
Can someone please help with this? I have spent hours trying to get this to work, and have run into many errors.
The latest error, runtime error 91 of which is on the line .visible = True. What Am I doing wrong????

Sub WordBookmarks()

Dim strDoc As String

Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wb As Workbook
Dim ws As Worksheet
Dim xlRange As Excel.Range
Dim Bookmark_1 As Excel.Range



Set wb = ActiveWorkbook
Set ws = wb.Sheets("Sheet2")
Set xlRange = Range("Bookmark_1")


With wdApp
.Visible = True
.WindowState = wdWindowStateMaximize
End With
Set wdApp = New Word.Application
Set myDoc = wdApp.Documents.Open("C:\Users\dnykg\Documents\Custom Office Templates\Agent Invoice - Bookmarks.dotm")


s = "Text to put after bookmark"
With myDoc.Bookmarks
.Item(Bookmark_1).Range.InsertAfter s
End With

myDoc.Save
myDoc.Close
Set wdApp = Nothing
Set myDoc = Nothing




End Sub

snb
02-04-2016, 03:21 AM
Sub M_snb()
with getobject("C:\Users\dnykg\Documents\Custom Office Templates\Agent Invoice - Bookmarks.dotm")
.bookmarks(1).range.insertafter "Illustration"
.windows(1).visible=true
' .close -1
end with
end sub

lsivak
02-04-2016, 08:40 AM
Thanks so much for the reply, but unfortunately it still doesn't work. I've tried so many variations. Anyone have any other suggestions?

snb
02-04-2016, 09:11 AM
Is does work, unless your file doesn't contain any bookmarks.

VBA isn't about varying but analysing.

lsivak
02-04-2016, 09:36 AM
When I try to run the code, it gives me a run-time error '5941' - The requested member of the collection does not exist. My bookmark exists in both word and the range exists in excel. What am i doing wrong?

snb
02-04-2016, 10:03 AM
My code doesn't refer to a range in Excel.
So you were not running my code ?

Does the file exist überhaupt ?

Which line returns the error ? Load a screendump