vba - Writing Data from Excel to Word -


  • i want use excel store "tag names" in column , associated "replacement text" in column b. when code runs, needs collect each tag, 1 @ time (row row), search entire word document words, , replace them corresponding replacements.
  • i noticed special tags in headers , footers weren't being replaced. turned article (http://word.mvps.org/faqs/customization/replaceanywhere.htm) , found working range of ranges (or cycling through available story ranges in document) able this.
  • i improved code, recommended in link above , worked, long code embedded in "normal" word file, thereby using vba code word operate on word document. however, goal use vba excel operate replacements while reading excel file.
  • when moved code excel, i'm getting hung on automation error reads,

"run-time error '-2147319779 (8002801d)': automation error library not registered.".

  • i've looked answers reviewing registry using "word.application.12" in place of "word.application".

i have windows 7, 64-bit machine, microsoft office 2007. have following libraries selected:

  • excel:

    • visual basic applications
    • microsoft excel 12.0 object library
    • ole automation
    • microsoft access 12.0 object library
    • microsoft outlook 12.0 object library
    • microsoft word 12.0 object library
    • microsoft forms 2.0 object library
    • microsoft office 14.0 object library
  • word:

    • visual basic applications
    • microsoft word 12.0 object library
    • ole automation
    • microsoft office 12.0 object library

i have no issues operating inside of excel regard vba. normally, passing set of strings function, now, have embedded strings inside of function, if planning on swapping 1 string (for number of instances), predetermined string.

function story_test() dim file string dim tag string dim replacementstring string  dim integer  dim wordobj object dim worddoc object dim storyrange word.range dim junk long  dim basefile string  'normally, these lines strings passed in file = "z:\file.docx" tag = "{{prepared_by}}" replacementstring = "joe somebody"  'review open documents, , set worddoc correct 1 'don't worry, have error handling in place more complex code set wordobj = getobject(, "word.application") basefile = basename(file) = 1 wordobj.documents.count     if wordobj.documents(a).name = basefile         set worddoc = wordobj.documents(a)         exit     end if next  'this fix provided fix skipped blank header/footer problem junk = worddoc.sections(1).headers(1).range.storytype   'okay, line can see error. 'when code run excel vba, problem.  word vba, no problem. 'anyone known why is??? '*********************************************************************** each storyrange in wordobj.documents(a).storyranges '***********************************************************************             'all need know following function call         ' have function works replace strings.         'it works fine provided has valid strings , valid storyrange.         call searchandreplaceinstory_forvariants(storyrange, tag, _           replacementstring, preadditive, finaladditive)         set storyrange = storyrange.nextstoryrange     loop until storyrange nothing next storyrange  set wordobj = nothing set worddoc = nothing  end function 

for each storyrange in wordobj.documents(a).storyranges 

should be

for each storyrange in worddoc.storyranges 

since assigned in loop above.


Comments