#1
|
|||
|
|||
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. Last edited by macropod; 07-10-2015 at 07:56 PM. Reason: Added code tags & formatting |
#2
|
||||
|
||||
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
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 |
#4
|
||||
|
||||
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] |
|
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 |