|
-
Sep 15th, 2010, 12:44 AM
#36
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|