VBA - Word - Bookmarks disappearing when I try to write text on them -


i trying make macro in excel, takes sample word file bookmarks on , writes on bookmarks. works 1 bookmark, second, third, etc deletes other entries.

e.g. after running of code, have written "info4". see info1, info2 , info 3 being written , deleted while macro run.

any ideas? here comes code:

option explicit  public sub main()      if [set_in_production] on error goto main_error      dim word_obj            object     dim word_doc            object     dim obj                 object     dim rng_range           variant     dim obj_table           object     dim origdoc$     dim l_row&: l_row = 2      on error resume next      set word_obj = getobject(, "word.application.14")     if err.number = 429         set word_obj = createobject("word.application.14")         err.number = 0     end if      if [set_in_production] on error goto main_error else on error goto 0     origdoc$ = activeworkbook.path & "\" & cstr(replace(time, ":", "_")) & "_" & generate_name & ".docx"     word_obj.visible = true     word_obj.displayalerts = false     set word_doc = word_obj.documents.open(activeworkbook.path & "\sample_2.docx")     word_obj.activedocument.saveas filename:=origdoc      'after saveas -> write      dim obj_bmrange     object      set obj_bmrange = word_obj.activedocument.bookmarks("info1").range     obj_bmrange.text = "info1" & vbcrlf     set obj_bmrange = nothing      set obj_bmrange = word_obj.activedocument.bookmarks("info2").range     obj_bmrange.text = "info2" & vbcrlf     set obj_bmrange = nothing      set obj_bmrange = word_obj.activedocument.bookmarks("info3").range     obj_bmrange.text = "info3" & vbcrlf     set obj_bmrange = nothing      set obj_bmrange = word_obj.activedocument.bookmarks("info4").range     obj_bmrange.text = "info4" & vbcrlf     set obj_bmrange = nothing     word_obj.displayalerts = false      set word_obj = nothing     set word_doc = nothing     set rng_range = nothing     set obj = nothing     set obj_table = nothing      on error goto 0     exit sub  main_error:      msgbox "error " & err.number & " (" & err.description & ") in procedure main of sub mod_main"  end sub 

i have tried rewrite bookmarks, once deleted, success no different. thus, waiting ideas! :d

the following approach works me. (note had remove lines of code specific workbook , files since don't have access of that. doesn't (shouldn't) change relevant problem present.)

something makes no sense in code posted declaring word_doc variable, not using it, instead relying on activedocument. substituted word_doc appropriate.

i inserted on error goto 0 re-instate normal error handling. when use on error resume next normal error handling deactivated, need approach getobject. once word application accessed needs turned on. using @ end of routine makes no sense.

as mentioned others, word removes bookmark when content written if bookmark has content (you see [square brackets]). around this, bookmark needs recreated around content assigned range. since involves couple of steps wrote separate function writing bookmark - writetobookmarkretainbookmark.

when test excel information written each bookmark , bookmarks exist @ end.

option explicit  public sub main()      dim word_obj            object     dim word_doc            object     dim obj                 object     dim rng_range           variant     dim obj_table           object     dim origdoc$     dim l_row&: l_row = 2      on error resume next      set word_obj = getobject(, "word.application.14")     if err.number = 429         set word_obj = createobject("word.application.14")         err.number = 0     end if     on error goto 0      word_obj.visible = true     word_obj.displayalerts = false     set word_doc = word_obj.activedocument '    word_obj.activedocument.saveas filename:=origdoc      'after saveas -> write      dim obj_bmrange     object      set obj_bmrange = word_doc.bookmarks("info1").range     writetobookmarkretainbookmark obj_bmrange, "info1" & vbcrlf     set obj_bmrange = nothing      set obj_bmrange = word_doc.bookmarks("info2").range     writetobookmarkretainbookmark obj_bmrange, "info2" & vbcrlf     set obj_bmrange = nothing      set obj_bmrange = word_doc.bookmarks("info3").range     writetobookmarkretainbookmark obj_bmrange, "info3" & vbcrlf     set obj_bmrange = nothing      word_obj.displayalerts = false      set word_obj = nothing     set word_doc = nothing     set rng_range = nothing     set obj = nothing     set obj_table = nothing      exit sub  main_error:      msgbox "error " & err.number & " (" & err.description & ") in procedure main of sub mod_main"  end sub  function writetobookmarkretainbookmark(rng object, content string)     dim sbkmname string      sbkmname = rng.bookmarks(1).name     rng.text = content     rng.document.bookmarks.add sbkmname, rng end function 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -