none
Can't display extension of newly saved PDF file RRS feed

  • Question

  • Hello,

    I have a simple (and rudimentary) program for converting word files to pdf. It's pretty much completed except for one small part...

    The selected files are stored in listbox1; when the user clicks the 'Convert' button, the files are saved as a PDF and then the filename is displayed in listbox2. The only part I'm struggling with is displaying the new filename in the second listbox with the pdf extension. It only displays the old filename with the .docx extension (Code for the convert button is shown below).

    I'm pretty sure there is a solution for this that I'm not thinking about (having a real brain fart here) and apologize if this has been answered before.

    Thanks to all who reply!

    Private Sub Convert_Click(sender As Object, e As EventArgs) Handles Convert.Click
            'will convert file(s) to PDF
            Dim oWD As Word.Application = Nothing
            Dim oDocs As Word.Documents = Nothing
            Dim doc As Word.Document = Nothing
            Dim fileCount As Integer = Nothing
            Dim fileName As String = Nothing
            Dim newName As String = Nothing
            Dim name As String = Nothing
            fileCount = ListBox1.Items.Count
            If fileCount > 0 Then
                If fileCount = 1 Then
                    MsgBox("1 file to be converted")
                ElseIf fileCount > 1 Then
                    MsgBox("There are " & fileCount & " files to be converted")
                End If
                For fileDoc = 0 To fileCount - 1
                    fileName = ListBox1.Items(fileDoc).ToString
                    name = Path.GetFileNameWithoutExtension(fileName)
                    oWD = New Word.Application
                    oDocs = oWD.Documents
                    doc = oDocs.Open(fileName)
                    doc.SaveAs2(Path.GetDirectoryName(fileName) + "/" + name, _
                                Word.WdSaveFormat.wdFormatPDF)
                    oDocs.Close()
                    oWD.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)
                    newName = Path.GetFileName(fileName)
                    ListBox2.Items.Add(newName)
                Next
            Else
                MessageBox.Show("No files found. Make sure you select files before" & _
                                vbCr & "trying to convert to a PDF.", "File(s) not found!", _
                                MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Sub


    Join the darskide. We have cookies!

    • Moved by Marvin_Guo Friday, January 24, 2014 8:48 AM Word related
    Thursday, January 23, 2014 10:41 PM

Answers

  • Hi Darth

    Well, Word forum is better than VSTO at any rate :-)

    Yes, SaveAs2 will convert the file type (and retain the original file).

    <<When I look in the file explorer, it shows the newly saved file with a '.pdf' extension and I am able to open it in Adobe Reader without any problems. I was assuming that the following line from above:>>

    Interesting. I don't know why Path.GetFileName is not returning the extension. The Language Reference for the method indicates it should be doing so. And if you are seeing that extension in Windows it can't be that hiding the extensions in Windows might be causing the .NET Framework to reflect this.

    In that case, I really would take the question to a .NET Framework forum. Don't bother them with the Word code. Just the line with newName = Path.GetFileName(fileName) (plus how fileName is defined, previous to that, or as a hard-coded string) and see what they say. Note that they'll probably ask you for an example of fileName - information you don't provide here.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, January 24, 2014 3:52 PM
    Moderator
  • Cindy,

    Thanks again for the response. Sorry for not responding right away - I was gone for the weekend from my work PC. I adjusted the code as you suggested and everything actually seems to be running smoother and faster than before. I'm glad you pointed out my mistake - don't need to bog down a system because of unnecessary processes running.

    I'll post the issue with the pdf extension in the .NET Framework forum - i'm assuming here: .NET Framework >> Visual Basic? Thanks again for the help...I'll post a link to the thread here in case anyone should have a similar problem in the future.

    Link to new thread: http://social.msdn.microsoft.com/Forums/en-US/feb7d288-a7b1-4000-a7d4-e4cf77007dfb/pathgetfilename-is-not-returning-the-proper-extension?forum=vbgeneral


    Join the darskide. We have cookies!


    Monday, January 27, 2014 1:50 PM

All replies

  • Hi Darth

    I'm not sure where you asked this originally. If it was a .NET Framework forum that would have been the better place since the issue appears to be with the Path.GetFileName method. However, looking at your code I believe the problem is because you don't assign an extension to the file when saving as PDF, which is apparently having the effect of not assigning an extension to the file. Have you looked at the file in Windows Explorer? If you do this, does it make a difference?

     name = Path.GetFileNameWithoutExtension(fileName) & ".pdf"

    Looking at your code, I see something that you should be doing differently. You create a New instance of the Word application in every iteration of the For...Next loop, what's more, you never explicitly RELEASE this object. This is causing a major memory leak that could lead to a system crash.

    Better to put it directly after If fileCount > 0 Then, put oWd Quit after Next, followed by  oWd = Nothing


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, January 24, 2014 2:57 PM
    Moderator
  • Cindy,

    Thanks for the reply. I had originally asked this in the VSTO forum but it was moved here by Marvin. Because of saving the Word files I assumed that was the appropriate forum for it (my apologies).

    When I look in the file explorer, it shows the newly saved file with a '.pdf' extension and I am able to open it in Adobe Reader without any problems. I was assuming that the following line from above:

    doc.SaveAs2(Path.GetDirectoryName(fileName) + "/" + name, Word.WdSaveFormat.wdFormatPDF)

    was doing the converting (correct me if I'm wrong).

    In terms of opening an instance of Word for every iteration in the loop, I do understand the concern there. It was pretty late in the day when I finally got the code to work properly and had noticed that when I ran a couple of tests. I didn't really focus on it as I was trying to get the new file to display properly with the new extension. However, now that I think about it, that is a major memory leak and I wouldn't want to introduce anything of the sort in the progam (I also wasn't sure how to run this without opening a new instance each and every time).

    So based on your suggestion above, would below be the proper code:
    If fileCount > 0 Then
    .
    .
    . stuff here
      For fileDoc = 0 To fileCount - 1
      .
      .
      . more stuff
      next
      oWD.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)
      oWD = nothing
    Else
    .
    .
    End If

    Again, thanks for the assistance. 


    Join the darskide. We have cookies!


    • Edited by Darth Probius Friday, January 24, 2014 3:40 PM edited code...
    Friday, January 24, 2014 3:38 PM
  • Hi Darth

    Well, Word forum is better than VSTO at any rate :-)

    Yes, SaveAs2 will convert the file type (and retain the original file).

    <<When I look in the file explorer, it shows the newly saved file with a '.pdf' extension and I am able to open it in Adobe Reader without any problems. I was assuming that the following line from above:>>

    Interesting. I don't know why Path.GetFileName is not returning the extension. The Language Reference for the method indicates it should be doing so. And if you are seeing that extension in Windows it can't be that hiding the extensions in Windows might be causing the .NET Framework to reflect this.

    In that case, I really would take the question to a .NET Framework forum. Don't bother them with the Word code. Just the line with newName = Path.GetFileName(fileName) (plus how fileName is defined, previous to that, or as a hard-coded string) and see what they say. Note that they'll probably ask you for an example of fileName - information you don't provide here.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, January 24, 2014 3:52 PM
    Moderator
  • Cindy,

    Thanks again for the response. Sorry for not responding right away - I was gone for the weekend from my work PC. I adjusted the code as you suggested and everything actually seems to be running smoother and faster than before. I'm glad you pointed out my mistake - don't need to bog down a system because of unnecessary processes running.

    I'll post the issue with the pdf extension in the .NET Framework forum - i'm assuming here: .NET Framework >> Visual Basic? Thanks again for the help...I'll post a link to the thread here in case anyone should have a similar problem in the future.

    Link to new thread: http://social.msdn.microsoft.com/Forums/en-US/feb7d288-a7b1-4000-a7d4-e4cf77007dfb/pathgetfilename-is-not-returning-the-proper-extension?forum=vbgeneral


    Join the darskide. We have cookies!


    Monday, January 27, 2014 1:50 PM
  • Yes, I think the general VisualBsic forum would be a good place to ask :-)

    Cindy Meister, VSTO/Word MVP, my blog

    Monday, January 27, 2014 4:04 PM
    Moderator