Update source to all excel links in word vba -
i trying update source links in word report using macro in word vba. want able offer dialog box user select file , replaces current source in links in word doc. code have below works slowly. seem have open excel in background or links wont work? not sure why is??
it seems go through eack link in tuen. there way globally change links @ same time possibly using find , repalce? please appreciated! need reprot in work , need find solution possible.
private sub commandbutton1_click() dim oldfile string dim xlsobj object dim xlsfile_chart object dim dlgselectfile filedialog 'filedialog object ' dim thisfield field dim selectedfile variant 'must variant contain filepath of selected item dim newfile variant dim fieldcount integer ' dim x long on error goto linkerror 'create filedialog object file picker dialog box set dlgselectfile = application.filedialog (filedialogtype:=msofiledialogfilepicker) dlgselectfile .filters.clear 'clear filters .filters.add "microsoft excel files", "*.xls, *.xlsb, *.xlsm, *.xlsx" 'filter excel files 'use show method display file picker dialog box , return user's action if .show = -1 'step through each string in filedialogselecteditems collection each selectedfile in .selecteditems newfile = selectedfile 'gets new filepath next selectedfile else 'user clicked cancel exit sub end if end set dlgselectfile = nothing ' update fields set xlsobj = createobject("excel.application") xlsobj.application.visible = false set xlsfile_chart = xlsobj.application.workbooks.open(newfile, readonly = true) application.screenupdating = false xlsobj.application .calculation = xlcalculationmanual .enableevents = false end fieldcount = activedocument.fields.count x = 1 fieldcount activedocument.fields(x) if .type = 56 .linkformat.sourcefullname = newfile end if end next x xlsobj.application .calculation = xlcalculationmanual .enableevents = true end application.screenupdating = true msgbox "data has been sucessfully linked report" 'clean xlsfile_chart.close savechanges:=false set xlsfile_chart = nothing xlsobj.quit set xlsobj = nothing exit sub linkerror: select case err.number case 5391 'could not find associated range name msgbox "could not find associated excel range name " & _ "for 1 or more links in document. " & _ "please sure have selected valid " & _ "quote submission input file.", vbcritical case else msgbox "error " & err.number & ": " & err.description, vbcritical end select ' clean set xlsfile_chart = nothing xlsobj.quit set xlsobj = nothing end sub
dim foldername string application.filedialog(msofiledialogfolderpicker) .allowmultiselect = false .show on error resume next foldername = .selecteditems(1) on error go 0 end if foldername = "" exit sub end if 'continue code using foldername source path
hopefully serve starting point you. path of source folder , store in foldername
. can build link using:
completepath = foldername + [filenamegoeshere]
(don't forget make sure foldername
has "\" on end, else path incorrectly formatted, if doesn't can add in or perform check ensure present on end of foldername
string
Comments
Post a Comment