Update linked fields in Word document from Excel VBA -
i trying automatically update information (such names, dates , numbers) across 3 different word documents putting data spreadsheet , linking respective cells word. spreadsheet has macros in auto-update parts of spreadsheet internally.
everything working fine, except updating links in word documents. when trying update link in word right-clicking on , selecting "update link" option brings macro warning dialog spreadsheet, asking whether want activate macros or not. doesn't once during 20s or update process takes (which seems unusually long me). updating link works, if you're willing click "activate macros" button of few dozen times.
i tried automate updating fields in document word vba, has same problem, brings macros dialog half minute. here's code that:
sub updatefields() activedocument.fields.update end sub
i tried update word documents directly spreadsheet, not work either, because when excel tries open word document via vba program stops executing , trows error:
"excel waiting application complete ole action."
clicking ok , waiting not because error message reappears after few seconds, , way stop manually kill excel process.
here's excel macro code:
sub loopthroughfiles() path = application.activeworkbook.path dim wordfile string wordfile = dir(path & "\*.doc") while len(wordfile) > 0 run update(path & "\" & wordfile) wordfile = dir loop end sub function update(filepath string) dim worddoc word.document set wordapplication = createobject("word.application") set worddoc = wordapplication.documents.open(filepath) 'this produces error activedocument.fields.update end function
note files in folder 3 documents , spreadsheet, , program find files without problems.
i have searched solutions online did not find anything, found odd, since seems pretty common thing vba. again, have little experience vba, might missing point , there super simple solution not aware of.
i think see error, silent failure, becuase document contains links, there open dialog waiting "yes" or "no" update links.
we can suppress dialog disabling automatic link updates (wordapplication.options.updatelinksatopen = false
).
function update(filepath string) dim wordapplication word.application dim worddoc word.document dim updatelinks boolean set wordapplication = createobject("word.application") updatelinks = wordapplication.options.updatelinksatopen 'capture original value wordapplication.options.updatelinksatopen = false 'temporarily disable set worddoc = wordapplication.documents.open(filepath) worddoc.fields.update 'msgbox "links updated in " & worddoc.name '## save , close document worddoc.save worddoc.close '## reset previous value , quit word application wordapplication.options.updatelinksatopen = updatelinks ' wordapplication.quit end function
also, remember save , close document, , quit word application inside function.
i made other modification:
in function, activedocument
not object in excel, need qualify it, otherwise line throw error. rather refer wordapplication.activedocument
, refer worddoc
have assigned.
Comments
Post a Comment