none
vba code to create a image file. RRS feed

  • Question

  • Hi all,,,,,,,,,

    Plzzzzzzz help me with the vba code to create a image file say(.jpg/.gif/.bmp) files....

    I have a code which would create text/word/excel files .....But I tried this code to create an image file....But I couldn't make it.....Though the file is created but i am unable to view it.....

    Plz...............help me this out..........

    The following is the code ........

    Public Sub veeru()    

    Const FILENAME = "C:\Users\310314\Desktop\myfile.xls"  

     Dim My_filenumber As Integer    

    My_filenumber = FreeFile  

    Open FILENAME For Output As #My_filenumber    

    Write #My_filenumber, "Hello"    

    Close #My_filenumber End Sub


    Veeru

    Monday, May 7, 2012 5:33 PM

Answers

  • To copy an image file, modify the following macro to suit your purpose:

    Sub CopyImage()
        Dim strSourceFile As String
        Dim strTargetFolder As String
        Dim i As Long
        strSourceFile = "C:\Images\Tulips.jpg"
        strTargetFolder = "C:\OtherFolder\"
        For i = 1 To 100
            FileCopy strSourceFile, strTargetFolder & "Tulips" & i & ".jpg"
        Next i
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by Veerusat Wednesday, May 9, 2012 7:48 AM
    Tuesday, May 8, 2012 9:14 PM
  • Here is code to save a workbook and a password-protected workbook from Excel 2007:

    Sub CreateWorkbook()
        Dim wbk As Workbook
        Set wbk = Workbooks.Add(xlWBATWorksheet)
        wbk.Worksheets(1).Cells(1, 1) = "Hello World"
        wbk.SaveAs "C:\Excel\MyWorkbook.xlsx", xlOpenXMLWorkbook
        wbk.Close
    End Sub
    
    Sub CreateProtectedWorkbook()
        Dim wbk As Workbook
        Set wbk = Workbooks.Add(xlWBATWorksheet)
        wbk.Worksheets(1).Cells(1, 1) = "Hello World"
        wbk.SaveAs "C:\Excel\MyWorkbook.xlsx", xlOpenXMLWorkbook, "TopSecret"
        wbk.Close
    End Sub

    You can modify the code as needed.

    Regards, Hans Vogelaar

    • Marked as answer by Veerusat Wednesday, May 9, 2012 7:48 AM
    Wednesday, May 9, 2012 6:23 AM
  • And code to save a document and a password-protected document from Word 2007:

    Sub CreateDocument()
        Dim doc As Document
        Set doc = Documents.Add
        doc.Content.Text = "Hello World"
        doc.SaveAs "C:\Word\MyDocument.docx", wdFormatXMLDocument
        doc.Close
    End Sub
    
    Sub CreateProtectedDocument()
        Dim doc As Document
        Set doc = Documents.Add
        doc.Content.Text = "Hello World"
        doc.SaveAs "C:\Word\MyDocument.docx", wdFormatXMLDocument, , "TopSecret"
        doc.Close
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by Veerusat Wednesday, May 9, 2012 7:47 AM
    Wednesday, May 9, 2012 6:29 AM

All replies

  • From which application do you want to create an image file?

    And what should be the contents of the image file?


    Regards, Hans Vogelaar

    Monday, May 7, 2012 8:12 PM
  • It is not necesssary for me to create a image file from any application.....I just want to create an image file with some text in it......say...."Hello World"....

    Veeru

    Tuesday, May 8, 2012 4:19 AM
  • An image file is more complicated than a text file.

    Select Start > All Programs > Accessories > Paint.

    Use the Text tool to enter some text.

    Save the file in the format that you want.


    Regards, Hans Vogelaar

    Tuesday, May 8, 2012 6:08 AM
  • Cool Q and better A. ;]

    Oskar Shon, Office System MVP

    Press if Helpful; Answer when a problem solved

    Tuesday, May 8, 2012 6:57 PM
    Answerer
  • Thanks for your patience and continuous replies Hans Vogelaar.........

    I have few doubts related to the above thread......If you could clarify it then it would be great....

    1) How can I copy a image file in a folder and create multiple copies of that image file and place it in another folder?

    2)Is it possible for me to create the following files from vda ...

    • Password protected Word Document.
    • Password protected Excel Document.
    • PST file.(Personal Folder file)
    • DOCX file
    • XLSX file...

    If so how....I hope you might help me this out...


    Veeru

    Tuesday, May 8, 2012 8:42 PM
  • To copy an image file, modify the following macro to suit your purpose:

    Sub CopyImage()
        Dim strSourceFile As String
        Dim strTargetFolder As String
        Dim i As Long
        strSourceFile = "C:\Images\Tulips.jpg"
        strTargetFolder = "C:\OtherFolder\"
        For i = 1 To 100
            FileCopy strSourceFile, strTargetFolder & "Tulips" & i & ".jpg"
        Next i
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by Veerusat Wednesday, May 9, 2012 7:48 AM
    Tuesday, May 8, 2012 9:14 PM
  • About creating Word documents: which version of Microsoft Office do you use?

    Regards, Hans Vogelaar

    Tuesday, May 8, 2012 9:33 PM
  • Hi Hans Vogelaar I use Microsoft office 2007....version

    Veeru

    Wednesday, May 9, 2012 5:59 AM
  • Here is code to save a workbook and a password-protected workbook from Excel 2007:

    Sub CreateWorkbook()
        Dim wbk As Workbook
        Set wbk = Workbooks.Add(xlWBATWorksheet)
        wbk.Worksheets(1).Cells(1, 1) = "Hello World"
        wbk.SaveAs "C:\Excel\MyWorkbook.xlsx", xlOpenXMLWorkbook
        wbk.Close
    End Sub
    
    Sub CreateProtectedWorkbook()
        Dim wbk As Workbook
        Set wbk = Workbooks.Add(xlWBATWorksheet)
        wbk.Worksheets(1).Cells(1, 1) = "Hello World"
        wbk.SaveAs "C:\Excel\MyWorkbook.xlsx", xlOpenXMLWorkbook, "TopSecret"
        wbk.Close
    End Sub

    You can modify the code as needed.

    Regards, Hans Vogelaar

    • Marked as answer by Veerusat Wednesday, May 9, 2012 7:48 AM
    Wednesday, May 9, 2012 6:23 AM
  • And code to save a document and a password-protected document from Word 2007:

    Sub CreateDocument()
        Dim doc As Document
        Set doc = Documents.Add
        doc.Content.Text = "Hello World"
        doc.SaveAs "C:\Word\MyDocument.docx", wdFormatXMLDocument
        doc.Close
    End Sub
    
    Sub CreateProtectedDocument()
        Dim doc As Document
        Set doc = Documents.Add
        doc.Content.Text = "Hello World"
        doc.SaveAs "C:\Word\MyDocument.docx", wdFormatXMLDocument, , "TopSecret"
        doc.Close
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by Veerusat Wednesday, May 9, 2012 7:47 AM
    Wednesday, May 9, 2012 6:29 AM
  • Is it possible to create the same password protected document from vb6....As there would be a mismatch in the user defined type....Can you help me.........

    And i guess vb6 forums is not in the MSDN....instead its vb6 resource center.....That is why I ask vb6 questions here.....


    Veeru

    Thursday, May 10, 2012 4:32 PM
  • You could do this in VB6:

    Sub CreateProtectedDocument()
        Dim app As Object
        Dim doc As Object
        Set app = CreateObject("Word.Application")
        Set doc = app.Documents.Add
        doc.Content.Text = "Hello World"
        doc.SaveAs "C:\Word\MyDocument.docx", 12, , "TopSecret"
        app.Quit
    End Sub


    Regards, Hans Vogelaar

    Thursday, May 10, 2012 4:38 PM
  •    

    Thanks once again....And is the following code correct for creating a password protected excel 2007 document.....But I got few errors....

        Dim app As Object
        Dim xls As Object
        Set app = CreateObject("Excel.Application")
        Set xls = Workbooks.Add(xlWBATWorksheet)
        xls.Worksheets(1).Cells(1, 1) = "Hello World"
        xls.SaveAs "C:\Word\MyDocument.xlsx", 12, , "TopSecret"
        app.Quit


    Veeru

    Friday, May 11, 2012 5:10 PM
  • It would help if you told us where the error occurs.

    Change the line

        Set xls = Workbooks.Add(xlWBATWorksheet)
    

    to

        Set xls = app.Workbooks.Add(-4167)


    Regards, Hans Vogelaar

    Friday, May 11, 2012 7:10 PM
  • The error message for the following code was

    Dim app As Object
        Dim xls As Object
        Set app = CreateObject("Excel.Application")
        Set xls = app.Workbooks.Add(-4167)
        xls.Worksheets(1).Cells(1, 1) = "Hello World"
        xls.SaveAs "C:\Word\MyDocument.xlsx", 12, , "TopSecret"
        app.Quit

    "Save As method of workbook class failed".

    And another doubt is that instead of saving it to the hard coded path over here, if I need to save it to the path which i mention in a text box in the user form, then how can this be done.


    Veeru

    Friday, May 11, 2012 7:44 PM
  • The code was intended as an example only; I assumed that you would change the path as needed (as well as the password).

    Let's say that the user enters the path and filename in a text box txtPath. You can then use

        xls.SaveAs Me.txtPath, 12, , "TopSecret"

    If the text box contains only the path, but not the filename, you can use

        xls.SaveAs Me.txtPath & "\MyWorkbook.xlsx", 12, , "TopSecret"

    where MyWorkbook is the name that you want to give the workbook. Change this as required, and change the password to something more original than "TopSecret".

    Regards, Hans Vogelaar

    Friday, May 11, 2012 7:56 PM
  • Okay let me try that and I have another method with which I can create any file...So in this method if I have a text box in my form as Text1 then what should i change in the following code.....because its throwing an error in the first line of the following code.

    Const FILENAME = Me.Text1& "\myfile.doc"

        Dim My_filenumber As Integer

        My_filenumber = FreeFile

        Open FILENAME For Output As #My_filenumber
        
        Write #My_filenumber, "Hello"

        Close #My_filenumber


    Veeru


    • Edited by Veerusat Friday, May 11, 2012 8:06 PM
    Friday, May 11, 2012 8:04 PM
  • The Open ... Write ... Close method will create a plain text file. You can't use it to create a Word document, an Excel workbook or an image file, even if you use .doc(x), .xls(x) or .jpg as extension.

    Regards, Hans Vogelaar

    Friday, May 11, 2012 8:06 PM
  • Yeah you are correct........

    But I am able to create a doc,xls,ppt and txt files but certainly not docx,xlsx,password protected files with the above code.....So i thought to use the above code for creating those documents (doc,xls,ppt and txt files) and your code for creating all the remaining docs.

    Because I have to expect your suggestions for all the rest of the docs say (ppt,txt,pptx,password protected ppt)......

    So in that case can you suggest me to correct the following line.....

    Const FILENAME = Me.Text1& "\myfile.doc"

    Or it would be great if you could say me how can I change the above code to my needs.........


    Veeru

    Friday, May 11, 2012 8:24 PM
  • I guess its confusing......

    To be clear if I could use the code

    Const FILENAME = Me.Text1& "\myfile.doc"

        Dim My_filenumber As Integer

        My_filenumber = FreeFile

        Open FILENAME For Output As #My_filenumber
        
        Write #My_filenumber, "Hello"

        Close #My_filenumber

    and create the file in the path specified in text box(Text1) then that would be great if not....

    Then can you help me with the code to create a ppt,pptx,txt and password protected ppt files in your style......if possible


    Veeru

    Friday, May 11, 2012 8:39 PM
  • I'm starting to wonder - what is the purpose of creating all those files? Are you trying to fill up your hard disk?

    Regards, Hans Vogelaar

    Friday, May 11, 2012 8:45 PM
  • Good Joke....But that's true.....its crazy.....

    But actually........Nope for the project it would be helpful for me to create n number of test datas............to test my application.........

    If you could share your personal/official mail id then I would explain it in detail (if needed).......


    Veeru

    Friday, May 11, 2012 8:52 PM
  • I think it would be easier to create one file of each type that you need manually in its own application. For example, start Word, create a new document, enter something in it, then save it (setting a password if desired).

    You can then use code to copy these files as needed. Let's say you store all these 'master' files in a folder C:\Master. To copy a Word document MyFile.docx from this folder to the folder specified in the text box Text1:

    FileCopy "C:\Master\MyFile.docx", Me.Text1 & "\MyFile.docx"


    Regards, Hans Vogelaar

    Friday, May 11, 2012 9:30 PM
  • I am still getting the error message for the following code was

    Dim app As Object
        Dim xls As Object
        Set app = CreateObject("Excel.Application")
        Set xls = app.Workbooks.Add(-4167)
        xls.Worksheets(1).Cells(1, 1) = "Hello World"
        xls.SaveAs "C:\Word\MyDocument.xlsx", 12, , "TopSecret"
        app.Quit

    "Save As method of workbook class failed".

    Could you help me..........

    Veeru

    Sunday, May 13, 2012 7:47 AM
  • Use

        xls.SaveAs "C:\Word\MyDocument.xlsx", 51, "TopSecret"
    

    You should change the path C:\Word\ to a path that exists on your computer.


    Regards, Hans Vogelaar

    Sunday, May 13, 2012 9:51 AM