Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-10-2015, 11:18 AM
Errol Isenberg Errol Isenberg is offline Program-generated .pdf file unable to be opened Windows 7 64bit Program-generated .pdf file unable to be opened Office 2013
Novice
Program-generated .pdf file unable to be opened
 
Join Date: Jul 2015
Location: Florida
Posts: 2
Errol Isenberg is on a distinguished road
Default Program-generated .pdf file unable to be opened

Hello, all you Microsoft Word experts. This is from a Microsoft Excel 2013 Specialist who is looking to improve in Word.
I have a Word document that I want to save as a .pdf file.When I click on FILE č Save As č and then choose “PDF” from the Save as Type drop-down menu, I get a .pdf file that opens with no problem.
The Word document has as the first word in the title line a field with anywhere from 1 to 7 digits, which is used as the name of the file.


When I try to automate the process, the .pdf file is created but cannot be opened; the highlighted error message below is displayed when an attempt is made to open the program-generated .pdf file – I understand this is due to the extraneous byte situation.As far as I can tell there are no extraneous bytes in front of the file.
Adobe Reader could not open ‘3224412.pdf’ because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn’t correctly decoded).
The relevant files are attached; the Word document with its macro (3224412 word doc.docm), a good version of the .pdf file (3224412 word doc.pdf), and the corrupted version of the .pdf file (3224412.pdf).
The program code is shown below as well as being included in the attached file.
Code:
Sub SaveDocument()
' this program saves the document in the Suntax Uploads folder on the user's desktop
Dim rng_claimant_id As Range
Dim str_claimant_id As String
Dim str_user_name As String
str_user_name = Application.UserName
Const const1 As String = "C:\Users\"
Const const2 As String = "\Desktop\Suntax Uploads\"
Const const3 As String = ".pdf"
Set rng_claimant_id = ActiveDocument.Range(Start:=ActiveDocument.Words(1).Start, End:=ActiveDocument.Words(1).End)
If Len(rng_claimant_id) = 8 Then
  str_claimant_id = Left(rng_claimant_id, 7)
Else
  If Len(rng_claimant_id) = 7 Then
    str_claimant_id = Left(rng_claimant_id, 6)
  Else
    If Len(rng_claimant_id) = 6 Then
      str_claimant_id = Left(rng_claimant_id, 5)
    Else
      If Len(rng_claimant_id) = 5 Then
        str_claimant_id = Left(rng_claimant_id, 4)
      Else
        If Len(rng_claimant_id) = 4 Then
          str_claimant_id = Left(rng_claimant_id, 3)
        Else
          If Len(rng_claimant_id) = 3 Then
            str_claimant_id = Left(rng_claimant_id, 2)
          Else
            str_claimant_id = Left(rng_claimant_id, 1)
          End If
        End If
      End If
    End If
  End If
End If
ActiveDocument.SaveAs2 FileName:=const1 & str_user_name & const2 & str_claimant_id & const3
' ActiveDocument.SaveAs2 "C:\Users\isenbee\Desktop\Suntax Uploads\3224412.pdf"
Code:
End Sub

The operating system I am using is Windows 7 Enterprise 64-bit.
Can anyone tell me how to programmatically generate the .pdf file so that it can be opened properly? I am not allowed to modify any registry settings. Thank you very much for your help.
Attached Files
File Type: docm 3224412 word doc.docm (186.6 KB, 8 views)
File Type: pdf 3224412 word doc.pdf (259.5 KB, 6 views)
File Type: pdf 3224412.pdf (186.6 KB, 6 views)

Last edited by macropod; 07-10-2015 at 07:56 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 07-10-2015, 08:06 PM
macropod's Avatar
macropod macropod is offline Program-generated .pdf file unable to be opened Windows 7 64bit Program-generated .pdf file unable to be opened Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The main problem is that you're not specifying the file type when you save it; merely adding a PDF extension does not a PDF make. Your code could also be greatly simplified. Try:
Code:
Sub SaveDocument()
' this program saves the document in the Suntax Uploads folder on the user's desktop
Dim rng_claimant_id As Range, str_claimant_id As String
With ActiveDocument
  Set rng_claimant_id = .Range(Start:=ActiveDocument.Words(1).Start, End:=ActiveDocument.Words(1).End)
  str_claimant_id = Left(rng_claimant_id, Len(rng_claimant_id) - 1)
  .SaveAs2 FileName:="C:\Users\" & Environ("Username") & "\Desktop\Suntax Uploads\" & _
    str_claimant_id & ".pdf", Fileformat:=wdFormatPDF
End With
End Sub
PS: When posting code, please use the code tags, indicated by the # button on the posting menu.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 07-13-2015, 07:57 AM
Errol Isenberg Errol Isenberg is offline Program-generated .pdf file unable to be opened Windows 7 64bit Program-generated .pdf file unable to be opened Office 2013
Novice
Program-generated .pdf file unable to be opened
 
Join Date: Jul 2015
Location: Florida
Posts: 2
Errol Isenberg is on a distinguished road
Default

Dear Paul, this worked perfectly. Thank you very much. How do I mark my question as solved?

Errol Isenberg

Last edited by Errol Isenberg; 07-13-2015 at 07:59 AM. Reason: Incomplete reply
Reply With Quote
  #4  
Old 07-13-2015, 02:50 PM
macropod's Avatar
macropod macropod is offline Program-generated .pdf file unable to be opened Windows 7 64bit Program-generated .pdf file unable to be opened Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Errol Isenberg View Post
How do I mark my question as solved?
You can do that via the 'Thread Tools' dropdown at the top of the thread page. I've done it for this one.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Project cannot save - file cannot be opened Guloluseus Project 22 01-28-2015 07:23 AM
Word Changes File Name When Opened canonm@pacbell.net Word 1 06-11-2014 10:04 PM
2007 file opened with 2010 messed up Intruder Project 1 11-21-2013 01:04 PM
Weird message “the file ( ) should be opened as read-only unless changes to it need t Jamal NUMAN Word 6 11-13-2013 01:53 AM
'Save As' to same folder as original opened file thepuppyprince Office 0 01-18-2013 08:13 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:32 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft