vba - FileSave() Word Macro -


i have written macro when file created , little save button clicked macro triggered(as overrides default filesave function). macro extracts information table in document, converts string cleanses string of carriage returns uses file name. if statement runs checking whether hidden row in table has value of 1 , if not set value 1 , save document @ location specified new filename.

all of works great except when re-open file edit it, users do, , click save again tries run again, ignoring if statements first statement , add carriage return start of filename breaking save function sharepoint has invalid character in it. if click save again seemingly run macro normal , save reading if statement correctly. doing wrong here?

here code:

sub filesave()     dim strtext string     dim strclean string     dim strfilename string     dim strlocation string     dim strsavedname string     dim strcleansave string      strtext = activedocument.tables(1).rows(1).cells(2).range.text      strclean = application.cleanstring(strtext)      strfilename = strclean + "_" + format(date, "yyyy-mm-dd")      strlocation = "[my sharepoint site]"      if activedocument.tables(1).rows(1).cells(3).range.text = "1"         strsavedname = activedocument.name         strcleansave = application.cleanstring(strsavedname)         activedocument.saveas filename:=strsavedname         exit sub     else         activedocument.tables(1).rows(1).cells(3).range.text = "1"         activedocument.saveas filename:=strlocation & strfilename & ".docx"     end if end sub 

word table cell text ranges terminated 2 hidden chars, carriage return (ascii 13) , bell (ascii 7). if condition returns false because testing equivalence of "1" , "1" & chr(13) & chr(7).

in case can limit test first char:

if left$(activedocument.tables(1).rows(1).cells(3).range.text, 1) = "1" 

more can test visible cell contents len() - 2.

hope helps.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -