Results 1 to 5 of 5

Thread: Issue loading Document name from csv file

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2017
    Posts
    2

    Issue loading Document name from csv file

    I'm hoping someone can help figure this out.

    I'm working with a software (CLS) that I have created Word Macros for to be able to merge Word Documents from csv files. This works fine. The only issue I have is I need to figure out a VB code to open the Document by itself (not merged into another document) then do the merge. The only problem is the Document name can change with each request. The csv file has the name of the document. First row are the column names, column 1 name is LLCODE, this is the code used for Document names. This is were I need the script to pull the document name from then open it so the rest of the merge can happen.

    the csv file would be laid out like this

    (Row 1) LLCODE, FNAME, LNAME, ADD1, ADD2, CITY, STATE, ZIP, PHONE
    (Row 2) TRLEXH. TOM, STEVENS,602 e anywere street, , Eagle, MD, 25414, 6021471458

    This is just an example. Row 3 could have the same info but with a different LLCODE since that would pull a different document.

    I just need to know how to pull the document name out of the CSV, then I can use the Document_Open(*) to open the correct document and start the merge. Any help would be greatly appreciated.

    Thanks

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Issue loading Document name from csv file

    how are you reading it?


    have a look at vba functions open, close, eof, line input. Think there were a few more...

    When you got some code, and if it still doesnt work, post it up here inside code tags for people to assist you.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Issue loading Document name from csv file

    .csv file would open by default in excel, but can also be read as a text file, how are you opening the .csv to read the data?

    same question as above...........
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2017
    Posts
    2

    Re: Issue loading Document name from csv file

    Thanks for getting back to me. That was faster than I expected. Below is the code I'm currently using to pull info and create the .csv file, then open a document. When the Document opens it imports the docs I want in but formatting is all messed up and from what I've seen and read Word has this issue if you are using the Insert Object (Text from File) feature. It drives me crazy. So I wanted to get it to pull the document in via VB instead. But here is the code for the .csv and the open document:

    Code:
    'MergeLetters macro
    
    Public Sub MAIN()
    Dim Qu$
    Dim UserDir$
    Dim FormDir$
    Dim masterwin$
    Dim mergewin$
    WordBasic.FileOpen Name:="f:\clsinc\word\PW_Lettr.doc", ReadOnly:=1, AddToMru:=0
    WordBasic.DisableInput 1
    WordBasic.ScreenUpdating 0      'turn off screen updates for speedier macro
    WordBasic.ToolsOptionsSpelling AutomaticSpellChecking:=0
    WordBasic.FileClose 2
    
    Qu$ = Chr(34)
    
    Rem On Error Goto Bye
    
    ' CLS Forms must be in F:\CLSINC\WORD
    ' CLS Users must be in N:\USERS\%LOGIN_NAME%
    ' The Dos Environment Variable LOGIN_NAME must be defined
    ' User Dir Will be for example N:\USERS\MARY\
    ' Form Dir Will be F:\CLSINC\WORD\
    
        UserDir$ = "N:\USERS\" & CreateObject("wscript.shell").expandenvironmentstrings("%username%") & "\clsinc\"
        FormDir$ = "F:\CLSINC\WORD\"
    
    ' Now, do the merge routine.
    
    WordBasic.FileOpen Name:=UserDir$ + "MASTERWP.WRD", ConfirmConversions:=0, AddToMru:=0, Revert:=0
    
    Rem Replace every Pipe character ("|") with a Paragraph Mark
    WordBasic.EditReplace Find:="|", Replace:="^p", ReplaceAll:=1
    
    WordBasic.EndOfDocument
    
    WordBasic.EditClear -2
    
    WordBasic.FileSaveAs Name:=UserDir$ + "MERGEDAT.CSV", Format:=2, AddToMru:=0
    
    WordBasic.FileClose 2
    
    WordBasic.FileOpen Name:=FormDir$ + "STANDOC1.DOCX", ConfirmConversions:=0, AddToMru:=0, Revert:=0
    
    masterwin$ = WordBasic.[WindowName$]()
    
    WordBasic.MailMergeOpenDataSource Name:=UserDir$ + "MERGEDAT.CSV", LinkToSource:=1, AddToMru:=0, Revert:=0
    
    WordBasic.MailMergeToDoc
    
    WordBasic.EditSelectAll
    
    WordBasic.UnlinkFields
    
    WordBasic.StartOfDocument
    
    mergewin$ = WordBasic.[WindowName$]()
    
    WordBasic.Activate masterwin$
    WordBasic.FileClose 2
    WordBasic.Activate mergewin$
    
    Bye:
    WordBasic.ScreenRefresh
    WordBasic.ToolsOptionsSpelling AutomaticSpellChecking:=1
    WordBasic.ScreenUpdating 1
    WordBasic.DisableInput 0
    End Sub
    This creates the .csv from the file CLS created. Then I have it open the Standoc1.docx and merge based off the mergefields. But what I want is to be able to remove the standoc1.docx and have it replaced with the name of the doc being used that is listed in the .csv. Currently the Standoc1.docx has the {INCLUDETEXT "F:\\CLSINC\\WORD\\{MERGEFIELD LLCODE}.docx"} to pull the document name into it, but as I said all the formatting gets screwed up with fonts, paragraph settings and page breaks. So I was hoping that doing it via VB would solve this issue since I'm calling the actually document instead of calling a blank doc to merge the correct one in.

    Thanks for the help on this.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Issue loading Document name from csv file

    it is years since i used wordbasic, it was replaced with vba from office 97, word basic is only kept for compatibility with older versions, but wordbasic and vba can be used together

    i still do not see anywhere you are opening or otherwise using a .csv file
    as a starting point, you can try like

    Code:
    f = freefile
    open formdir$ & "somefile.csv" for input as f   '  open .csv file in forms dir, change as required
    csvlist = split(input(lof(f),#f), vbnewline)    'get lines in .csv file into an array
    close f
    for i = 1 to ubound(csvlist)
        fname = left(csvlist(i), instr(csvlist(i), ",") -1) & ".docx"   ' get the text in front of first comma
        set doc = documents.open(formdir$ & fname)
        ' here you can do whatever you want with doc (the opened document object)
    next
    as i am still not sure what you want to do with the documents when opened, i just typed this in the browser, so may contain typos or code errors

    as you are working with .docx files it would seem unlikely that you are working with word 95 or earlier and do not have vba available so the code should work, even mixed with your wordbasic
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width