Splitting an MS Word File using Excel VBA - referencing section ranges -


hopefully quick one.

i through macro splitting word file (merged file of letters) individual pdfs , naming them based on ref number included in file.

 'start split each sec in activedocument.sections      set rng = sec.range  'range of section     sectext = sec.range.text 'all text within section     sectextposition = instr(sectext, "our ref: ")  'position of "out ref: " within section     strcdrs = mid(sectext, (sectextposition + 9), 16)  'retrieved cdrs reference      if sec.index < activedocument.sections.count         rng.moveend wdcharacter, -1 'drop trailing section break     end if         rng.exportasfixedformat strfolder & "\" & replace(strcdrs, "/", "-") & "-" & strlettertype & ".pdf", wdexportformatpdf      set rng = nothing next sec 

this works when embedded in word file. however, when embedding in excel file , referencing document, type mismatch on the:

        set rng = sec.range  'range of section 

look @ value of sec.range, looks fine, appears rng range object. missing obvious?

full draft code follows:

sub splitexport() dim sec section dim rng range dim strsplitfile string dim strcdrs string dim strlettertype string dim strfolder string dim sectext string dim sectextposition long dim strfldr filedialog dim strfile filedialog dim wordfile word.document  'set word application   set wordapp = createobject("word.application")  'pick file split  set strfile = application.filedialog(msofiledialogfilepicker) strfile     .title = "select file split"     .allowmultiselect = false     .show     strsplitfile = .selecteditems(1)     end  'check if file selected if strsplitfile = ""     msgbox "cannot proceed without file selection", vbokonly + vbcritical, "error"     exit sub end if  'set letter type string strlettertype = inputbox("please enter letter code...") if strlettertype = ""     msgbox "cannot proceed without letter code", vbokonly + vbcritical, "error"     exit sub end if  'set folder save pdfs  set strfldr = application.filedialog(msofiledialogfolderpicker) strfldr     .title = "select folder save split files"     .allowmultiselect = false     .show     strfolder = .selecteditems(1) end  'check folder selected if strfolder = ""     msgbox "cannot proceed without folder selection", vbokonly + vbcritical, "error"     exit sub end if  'open file split set wordfile = wordapp.documents.open(strsplitfile) wordfile.activate  'start split each sec in activedocument.sections      set rng = sec.range  'range of section     sectext = sec.range.text 'all text within section     sectextposition = instr(sectext, "our ref: ")  'position of "out ref: " within section     strcdrs = mid(sectext, (sectextposition + 9), 16)  'retrieved reference      if sec.index < activedocument.sections.count         rng.moveend wdcharacter, -1 'drop trailing section break     end if         rng.exportasfixedformat strfolder & "\" & replace(strcdrs, "/", "-") & "-" & strlettertype & ".pdf", wdexportformatpdf      set rng = nothing next sec 

end sub

apologies wasting anyone's time reading - haven't changed reference section word.section, etc.

i leave testament muppetry.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -