Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Word c# interop bookmark issue(s)

Word c# interop bookmark issue(s)

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpquestioncsharpdatabasecom
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jboyd111
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • J jboyd111

      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

      M Offline
      M Offline
      Maciej Los
      wrote on last edited by
      #2

      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.range

      Set doc = Documents(1)
      Set rng = doc.Paragraphs(1).range

      End 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 Sub

      Function AddBookmark(ByVal sName As String, ByRef oRange As Word.Range) As Word.Bookmark
      Set AddBookmark = oRange.Bookmarks.Add(sName, oRange)
      End Function

      sorry, for my language I'm still learning

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups