vbscript to create word file and format it

Discussion in 'Visual Basic ( VB )' started by stable, Nov 6, 2008.

  1. stable

    stable New Member

    Joined:
    Nov 6, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hello,
    I'm not sure that this is the correct forum for vbscripts. I am very new to vbscripting.

    I have searched the internet and combined snippets of code to process image files in a folder. I want to create a new Word document of all the image files and insert the image filename under the image (minus the file type extension).

    It partially works however the images are pasted on top of each other and the text is underneath the images! I need help on how to add new lines to the document so that the images appear under each other. Do I need to "go to new page" too?

    My code is below:
    Code:
    Set fsoFolder = CreateObject("Scripting.FileSystemObject")
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    Set objDoc = objWord.Documents.Add()
    Set objSelection = objWord.Selection
    Set objShape = objDoc.Shapes
    
    objSelection.Font.Name = "Arial"
    objSelection.Font.Size = "12"
    Set folder = fsoFolder.GetFolder("C:\PKW_JPG")
    Set files = folder.Files
    for each objFile in files
    	filepath = fsoFolder.GetAbsolutePathName(objFile)
    	sName = objFile.Name
    	iLen= len(sName) -4
    	sNameNew = left(sName, iLen)
    '	msgbox sName
    	objShape.AddPicture(filepath)
    	objSelection.TypeParagraph()
    
    	objSelection.TypeText sNameNew
    	objSelection.TypeParagraph()
    	objSelection.TypeParagraph()
    Next
    set files = nothing
    set folder = nothing
    set fsoFolder = nothing
    
    Any help would be much appreciated.

    cheers
    stable
     
    Last edited by a moderator: Nov 6, 2008
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Really nice question, so I decided to post the solution though it's late !!! :pleased:

    Took me too long to figure out ! :p

    The thing is, when you use objDoc.Shapes, the image ought to stay above everything else (may be text or image). You are not inserting shapes into any proper place in the doc, you are just adding it to the collection of shapes.
    What you should use here is : objSelection.InlineShapes.

    The correct script should be :
    Code:
    Set fsoFolder = CreateObject("Scripting.FileSystemObject")
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    Set objDoc = objWord.Documents.Add()
    Set objSelection = objWord.Selection
    Set objShape = objSelection.InlineShapes
    
    objSelection.Font.Name = "Arial"
    objSelection.Font.Size = "12"
    Set folder = fsoFolder.GetFolder("C:\Documents and Settings\Rajesh\Desktop\Ricky files\TestPix")
    Set files = folder.Files
    for each objFile in files
    	filepath = fsoFolder.GetAbsolutePathName(objFile)
    	sName = objFile.Name
    	iLen= len(sName) -4
    	sNameNew = left(sName, iLen)
    '	msgbox sName
    	objShape.AddPicture(filepath)
    	objSelection.TypeParagraph()
    	objSelection.TypeText sNameNew
    	objSelection.TypeParagraph()
    	objSelection.TypeParagraph()
    
    Next
    set files = nothing
    set folder = nothing
    set fsoFolder = nothing
     
    Last edited: May 9, 2009
    shabbir likes this.
  3. stable

    stable New Member

    Joined:
    Nov 6, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi SaswatPadhi,

    Thanks very much for publishing your solution. I will definitely use it next time we need to create a similar document and hope others will be able to make use of your solution.

    Once again thanks for you help.

    Cheers
    Stable
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    You are welcome !
    Glad to know that you liked it ! :smile:
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice