Word c# interop bookmark issue(s)
-
I have a winform app that writes a word document template to a database. I'd like to read this back in and insert text at cartain points in the document. I've been reading the web and it looks like using bookmarks is the way to go. I'm stuck on setting bookmarks, however. My code is below:
object oNull = System.Reflection.Missing.Value;
Word.Range start = para.Range; object oRng = start; string newText = "Bookmark1"; Word\_doc.Bookmarks.Add(newText, ref oRng);
when I run it the traceback shows: "...Bad bookmark name." This is at the line: "Word_doc.Bookmarks.Add(newText, ref oRng);" 2 questions then are: 1) How do I properly set a bookmark in a word doc using interop (automation)? 2) Is there another/better way to be able to insert a table at a certing location in a word doc? Thanks in advance. Jboyd
-
I have a winform app that writes a word document template to a database. I'd like to read this back in and insert text at cartain points in the document. I've been reading the web and it looks like using bookmarks is the way to go. I'm stuck on setting bookmarks, however. My code is below:
object oNull = System.Reflection.Missing.Value;
Word.Range start = para.Range; object oRng = start; string newText = "Bookmark1"; Word\_doc.Bookmarks.Add(newText, ref oRng);
when I run it the traceback shows: "...Bad bookmark name." This is at the line: "Word_doc.Bookmarks.Add(newText, ref oRng);" 2 questions then are: 1) How do I properly set a bookmark in a word doc using interop (automation)? 2) Is there another/better way to be able to insert a table at a certing location in a word doc? Thanks in advance. Jboyd
If you create Word application object, uou can use
Selection.Range
to tell the application: "use current selection" or you should create range object in context:
Sub cxzcxz()
Dim doc As Word.Document
Dim rng As Word.rangeSet doc = Documents(1)
Set rng = doc.Paragraphs(1).rangeEnd Sub
How to create bookmark using VBA (Word application)?
Sub test()
Dim oBkm As Word.Bookmark
Set oBkm = AddBookmark("Bookmark1", Selection.Range)
MsgBox oBkm.Name, vbInformation, "Message"
End SubFunction AddBookmark(ByVal sName As String, ByRef oRange As Word.Range) As Word.Bookmark
Set AddBookmark = oRange.Bookmarks.Add(sName, oRange)
End Functionsorry, for my language I'm still learning