Results 1 to 36 of 36

Thread: [RESOLVED] Save and print PDF/WORD/Images

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Resolved [RESOLVED] Save and print PDF/WORD/Images

    Greetings all once again.
    I have created a Notifications program that a user will be able to create new Notifications and print (if he wishes the relevant report(using crystal reports)
    The issue that I'm facing is that the user might need to attach a pdf,word or image to his notification. So my question is 2scaled (I apologize in advance)
    Question 1:
    How can I attach a word,pdf and/or image on a form (at the same time save it in my access db) for the specific notification?
    Question 2:
    How can I program my crystal report to print the attachment as well?
    I am using Visual Basic 2008 pro and Access2007

    Sorry if I've posted in the wrong place but since it involves DB-Reports-Forms I didn't know where else to post.

    Thank you all in advance for your valuable time and assistance

  2. #2
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Save and print PDF/WORD/Images

    You can associate a path to a form and also save that path in the database. You won't gain much trying to associate a file itself. when you have the path you can manipulate the document how you wish.

    Can't help you with the crystal reports. But a workaround would be to print the document in the code that calls up the crystal report.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Save and print PDF/WORD/Images

    MarMan, thank you for your reply, but can you be more specific?
    Next to the textbox (where the suer will write his notification) I want to put some sort of attachment button in order for the user to attach his file (and be saved in the database for future auditing).

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Save and print PDF/WORD/Images

    In terms of saving the attachments to the database, see the "blob" articles in the VB.Net section of our Database Development FAQs/Tutorials (at the top of this forum)

    However, depending on the circumstances it may be better to follow MarMan's suggestion and just save the location of the file (as text) rather than the file itself. Doing that means that if a file is altered/deleted outside of the database, it will also be altered/deleted for your program.

  5. #5
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Save and print PDF/WORD/Images

    Let's take this one step at a time. Where does the attachment come from?
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Save and print PDF/WORD/Images

    This attachment will come from anywhere basically.
    Wherever the user has it.It will be a supplement document or image to the text that he will write. And that is why "calling it" from a hyperlink will not aid that much, since other people might want to see it. I know that this will create an extremely large database (since I'm using access) but I don't see any other way.

    Once again thank you for your time I really appreciate the effort.

    PS: I'm also checking the blob articles mentioned by si_the_geek

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Save and print PDF/WORD/Images

    I understand your reasoning, but you don't actually need to store the files in the database - you can create a folder on the network (probably a sub-folder of where the database file is stored) for the files to be copied to.

    That will achieve basically the same thing, but will mean that the database file itself wont get so large (which reduces the chances of corruption, and reaching the size limit), and that the attachments can be seen even if the database fails (but can be seen without your program, which may be bad for you). It will also mean that making a backup is slightly more awkward.

  8. #8
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Save and print PDF/WORD/Images

    If you don't know where the file will be then you will have to use the method recommended by si to store the files.

    Crystal reports can't print a word document or a pdf as far as I know. You will need to use the application that created the file, for example you will need to use word to print a word doc. Search for "automating word".
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Save and print PDF/WORD/Images

    OK
    I will try it the way you tell me to. There is only one last thing..
    1. What do I use in my form as an "attach button" so that the user will be prompt to search his computer for the file he wants to attach
    2.How do I save the files in that folder?

    Sorry if I'm asking too much..I'll understand if i'm crossing the line.

    PS-MarMan: I have seen reports built in VB include attachments before and that is why I asked ...

  10. #10

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Save and print PDF/WORD/Images

    It just came to me..sorry for the additional question..but can the folder you mentioned before, be stored in my program so when the program is compiled it will also be created and the files that are uploaded go automatically to my programs installation path?

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Save and print PDF/WORD/Images

    For a button to do anything you use a button, all that changes is the code you put inside it.

    To allow the user to select a file, you use an OpenFileDialog (the help for it contains a good example).

    To copy the file I'm not sure, as I haven't done it enough to remember!

  12. #12
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Save and print PDF/WORD/Images

    Here's how to copy a file:
    Code:
    System.IO.File.Copy("c:\original.txt", "c:\NewDir\NewFile.txt")
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  13. #13

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Save and print PDF/WORD/Images

    That's great MarMan!
    But how do I change
    Code:
    ("c:\original.txt",
    into whatever the user selects from the OpenFileDialog (or any other way that you might be using)

  14. #14
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Save and print PDF/WORD/Images

    There's a property that returns what the user selected.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  15. #15

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Save and print PDF/WORD/Images

    Seems I'm close. I have this code so far but I get an error (obviously)

    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim strFileName As String
            Dim DidWork As Integer = OpenFD.ShowDialog()
    
            OpenFD.InitialDirectory = "C:\"
            OpenFD.Title = "Open a Text File"
            OpenFD.ShowDialog()
            If DidWork = DialogResult.Cancel Then
                MsgBox("Cancel Button Clicked")
            Else
                System.IO.File.Copy(strFileName, "C:\Published Program")
                MsgBox(strFileName)
            End If
        End Sub
    I run the program, the dialog box appears and when I select the text file the following appears:
    File name cannot be null. Parameter name: sourceFileName

  16. #16

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Save and print PDF/WORD/Images

    OK, I got it to save a copy of the file in a specific folder
    Code:
    Dim strFileName As String
            Dim DidWork As Integer = OpenFD.ShowDialog()
    
            OpenFD.InitialDirectory = "C:\"
            OpenFD.Title = "Attach a Text File"
            OpenFD.ShowDialog()
            If DidWork <> DialogResult.Cancel Then
                strFileName = OpenFD.FileName
                System.IO.File.Copy(strFileName, "C:\Published Program\john.txt")
                TextBox3.Text = strFileName
                MsgBox(strFileName)
            End If
    
        End Sub
    But every time a user wants to save a file it will overwrite this one? Isn't that wrong? Is there a way to continusly create new files each time?
    It seemed that if I didn't insert the \john.txt at the end, it wouldn't work giving an error (that the destination is a directory and not a file) so by inserting that last part it created john.txt.

  17. #17
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Save and print PDF/WORD/Images

    You can use the same filename in strFileName. You will have parse the file name from the path, then you can add it to your path:
    Code:
    System.IO.File.Copy(strFileName, "C:\Published Program\" & strFileNameWithoutPath)
    Here's a function that will return a unique name (by appending a number) so you won't have to worry about overwriting:
    Code:
        Private Function ReturnUniqueFileName(ByVal strPath As String, ByVal strName As String, ByVal strExtension As String) As String
    
            Dim bExists As Boolean
            Dim intCounter As Integer
            Dim strFilePath As String = vbNullString
    
    
            Try
    
                strFilePath = strPath & "\" & strName & strExtension
                bExists = System.IO.File.Exists(strFilePath)
    
                While intCounter < 100 And bExists
                    intCounter += 1
                    strFilePath = strPath & "\" & strName & intCounter.ToString & strExtension
                    bExists = System.IO.File.Exists(strFilePath)
                End While
    
            Catch ex As Exception
    
                MsgBox("Error in ReturnUniqueFileName(" & strPath & "): " & ex.Message)
    
            End Try
    
            If Not bExists Then
                Return strFilePath
            Else
                Return Nothing
            End If
    
        End Function
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  18. #18

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Save and print PDF/WORD/Images

    Good Morning from Athens,
    I just read your msg, and thank you for the help. Unfortunatly I still haven't managed to save the file without overwriting it.

    Using your code:
    Code:
    System.IO.File.Copy(strFileName, "C:\Published Program\" & strFileNameWithoutPath)
    I have to declare
    Code:
    strFileNameWithoutPath
    , any ideas on what to declare it to?
    I'm still searching on-line for all of this so that I don't waste your time, all I could find though are outlook msg attachments.

    Once again thank you!

  19. #19
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Save and print PDF/WORD/Images

    Its a string that you fill with the filename. In your example:
    Code:
    "C:\Published Program\john.txt"
    You will have to write code to remove "john.txt" and store it in strFileNameWithoutPath.
    Its called parsing a path.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  20. #20

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Save and print PDF/WORD/Images

    Thank you for that, but can you please give me an example? I've never heard of this before..I know I should have..but I'm new and from what I can see I'm starting to get over my feet..

  21. #21
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Save and print PDF/WORD/Images

    You never mentioned what version of VB you are using?
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  22. #22

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Save and print PDF/WORD/Images

    I had on my first post. I'm using VB 2008 pro

  23. #23

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Save and print PDF/WORD/Images

    Hello to all once again.
    Hope you had a great weekend, unfortunatly I've been searching a way to solve this..but I guess the weekend wasn't enough..
    If you have anything else to add for assistance I would be more than greatful.

    Thank you all

  24. #24

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Save and print PDF/WORD/Images

    I believe that I have progressed.. but I'm still getting one error.
    My code now looks like this:
    Code:
    Function FileNameGetUnique(ByVal strFileName As String) As String
            Dim lCount As Long, lPosDot As Long
            Dim sFileNoExtension As String, sExtension As String
    
            On Error GoTo ErrFailed
            If Len(strFileName) = 0 Then
                Debug.Assert("Error: Empty File name supplied to " & FileNameGetUnique)
                Exit Function
            End If
    
            'Remove file extension
            lPosDot = InStrRev(strFileName, ".")
            If lPosDot Then
                sFileNoExtension = Microsoft.VisualBasic.Left(strFileName, lPosDot - 1)
                sExtension = Mid$(strFileName, lPosDot)
            Else
                sFileNoExtension = strFileName
            End If
    
            'Get unique file name
            Do
                lCount = lCount + 1
            Loop While Len(Dir$(sFileNoExtension & "." & CStr(lCount) & sExtension))
            FileNameGetUnique = sFileNoExtension & "." & CStr(lCount) & sExtension
            Exit Function
    
    ErrFailed:
            Debug.Print(Err.Description)
            Debug.Assert(False)
            FileNameGetUnique = ""
        End Function
    
    
    
        Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
    
            Dim strFileName As String, sUniqueFileName As String
            Dim DidWork As Integer = OpenFD.ShowDialog()
    
            OpenFD.InitialDirectory = "C:\"
            OpenFD.Title = "Attach a Text File"
            OpenFD.ShowDialog()
            If DidWork <> DialogResult.Cancel Then
                strFileName = OpenFD.FileName
                sUniqueFileName = FileNameGetUnique(strFileName)
                MsgBox("The unique file name is " & sUniqueFileName)
                System.IO.File.Create("C:\Published Program\" + sUniqueFileName)
                TextBox3.Text = strFileName
            End If
    The error that is produced is :
    NotSupportedException
    The given path's format is not supported.

    This is being reproduced at my System.IO.File.create line. I have tried alternatives but I still get this error.

    The file is recreated by adding one number, but I can't get it to be saved now.

    Am I getting closer? I could really use your guidance..

  25. #25
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Save and print PDF/WORD/Images

    Examine the path you are using, it may be incorrect.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  26. #26

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Save and print PDF/WORD/Images

    Hello MarMan,

    Which path are you referring to?
    If it's :
    Code:
    System.IO.File.Create("C:\Published Program\" + sUniqueFileName)
    Then I triple checked, and there is a folder named Publish Program in my C drive..I know this might be sounding a bit idiotic, but I'm not exactly sure on which path you are referring to.

  27. #27
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Save and print PDF/WORD/Images

    Whichever path is generating the following:
    Quote Originally Posted by leontas View Post
    The given path's format is not supported.
    Step through the program line by line, and check the line that generates the error. The problem will likely be before that.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  28. #28

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Save and print PDF/WORD/Images

    That's what freaks me out, the path that generates the error is :
    Code:
    System.IO.File.Create("C:\Published Program\" + sUniqueFileName)

  29. #29
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Save and print PDF/WORD/Images

    What is the contents of sUniqueFileName?
    1. Select everything in the parenthesis
    2. This -> "C:\Published Program\" + sUniqueFileName
    3. Press Control-C
    4. Press Control-G
    5. Type ?
    6. Press control-V
    7. Press Enter


    That should give you a clue.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  30. #30

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Save and print PDF/WORD/Images

    I don't know what that will show me, I will try it and I will let you know tomorrow.(the server is at work and it's 20:30 pm over here now)

    Once again, thank you for your time, you really rule!!!

  31. #31
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Save and print PDF/WORD/Images

    That will show you what you are sending to System.IO.File.Create. You cut and paste the argument and paste it in the immediate window (? is shorthand for print). You can also add a watch, but this method will show you the path that you are sending to Create. Helps find typos..
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  32. #32

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Save and print PDF/WORD/Images

    Good morning again.

    I followed your steps, and the outcome was :
    Code:
    Name 'sUniqueFileName' is not declared.
    Which is weird because I have it declared as you can see on my previous code snipet
    Code:
    Dim strFileName As String, sUniqueFileName As String
            Dim DidWork As Integer = OpenFD.ShowDialog()
    
            OpenFD.InitialDirectory = "C:\"
            OpenFD.Title = "Attach a Text File"
            OpenFD.ShowDialog()
            If DidWork <> DialogResult.Cancel Then
                strFileName = OpenFD.FileName
                sUniqueFileName = FileNameGetUnique(strFileName)
    Any ideas?

  33. #33
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Save and print PDF/WORD/Images

    My 2 cents.

    You're storing the path which is good... this is because you may have to resort to web page based printing (PDF in a frame or some other workaround then you send the web page to printer). And its easier to generate web pages with relative file paths compared to dumping content from database blob (you will need a dedicated web server for that).

  34. #34

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Save and print PDF/WORD/Images

    Thank you leinad31 for your response and your 2 cents

    Unfortunatly I do not have a web server in order to accomplish all that.
    I will be doing my printing via crystal reports, and the only thing that (I was hoping on) will be saved in the access db will be the file path of the attachment.

  35. #35
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Save and print PDF/WORD/Images

    I can't follow your code in pieces. Can you post the whole routine? Where sUniqueFileName is declared along with the create method that is creating the error.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  36. #36

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Save and print PDF/WORD/Images

    Mystery Solved!

    I will post the code in case someone else would like to use it (and putting his own finishing touches ofcourse)

    1 form, 2 textboxes, 2 buttons and..

    Code:
    Imports System.Windows.Forms
    Imports System.IO
    Public Class Form1
        Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim OpenFD As New OpenFileDialog()
            OpenFD.InitialDirectory = "C:\Desktop\"
            'OpenFD.Filter = "PDF Files(*.pdf)|*.pdf" if you want specific filters
            OpenFD.RestoreDirectory = True
            Try
                If OpenFD.ShowDialog() = DialogResult.OK Then
                    'Set the output fields
                    TextBox1.Text = OpenFD.FileName.ToString
                End If
            Catch Ex As Exception
                MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
            End Try
        End Sub
    
        Function FileNameGetUnique(ByVal strFileName As String) As String
            Dim lCount As Long, lPosDot As Long
            Dim sFileNoExtension As String, sExtension As String
    
            On Error GoTo ErrFailed
            If Len(strFileName) = 0 Then
                Debug.Assert("Error: Empty File name supplied to " & FileNameGetUnique)
                Exit Function
            End If
    
            'Remove file extension
            lPosDot = InStrRev(strFileName, ".")
            If lPosDot Then
                sFileNoExtension = Microsoft.VisualBasic.Left(strFileName, lPosDot - 1)
                sExtension = Mid$(strFileName, lPosDot)
            Else
                sFileNoExtension = strFileName
            End If
    
            'Get unique file name
            Do
                lCount = lCount + 1
            Loop While Len(Dir$(sFileNoExtension & "." & CStr(lCount) & sExtension))
            FileNameGetUnique = sFileNoExtension & "." & CStr(lCount) & sExtension
            Exit Function
    
    ErrFailed:
            Debug.Print(Err.Description)
            Debug.Assert(False)
            FileNameGetUnique = ""
        End Function
        Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim strFileName As String = TextBox1.Text
            Dim sUniqueFileName As String = "user" & FileNameGetUnique(strFileName)
    
            Try
                If IO.File.Exists(TextBox1.Text) Then
    
                    IO.File.Copy(TextBox1.Text, "C:\Published Program\" & IO.Path.GetFileName(sUniqueFileName))
                    TextBox2.Text = sUniqueFileName.ToString
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
        End Sub
    End Class
    I'm sure you can fill all that in one button, I haven't gone around to that, but will do today.
    The file will be saved to the desired location adding a ".1", but I'm also sure you can somehow change the code to add text instead of adding a number.

    Special thanks to MarMan and si_the_geek for all their valuable time, help and extremely helpful guidelines!!!!

    Last edited by leontas; Sep 15th, 2010 at 12:46 AM. Reason: forgot a line

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