Macro to PDF Problem

JERRY21

New Member
Joined
Nov 7, 2015
Messages
31
Hi All,

I do have using macro below to save my official receipt in microsoft word as PDF file to be send out to my customer,the problem is when is do have a repetition of same supplier name,the macro cant work it out to save the file individually and error message appear. So can anyone assist me on modify the macro below so that all the same supplier name official receipt can be compile as one pdf file to ease them to see.

Save then resulting document. NOTICE MODIFIED filename
ActiveDocument.SaveAs2 FileName:=Environ("userprofile") + "\Desktop\Output\" + DokName + ".pdf", FileFormat:= _
wdFormatPDF, LockComments:=False, Password:="", AddToRecentFiles _
:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False, CompatibilityMode:=14

Thanks You
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
May be if you tell us what the error says we may be able to help, it could be related to the file already existing, also I am guessing this is not the whole macro, it should be a sentence where the name of the file Dokname is set
Cheers
Sergio
 
Upvote 0
Hi Mr Sergio,
You are right this is only a part of my macro,my macro command work well if all my customer name is not same.But if my customer name are same this command does not work ,as this macro will automatically generated PDF file name is the customer name.
 
Upvote 0
I think the error is when the macro tries to save the PDF and there is a file with the same name in the folder, you could try changing the name of the file, for the second file by adding a number 2 to the name FileName:=Environ("userprofile") + "\Desktop\Output\" + DokName + "2.pdf" once you get the error, if you don't get the error do not add the number 2 to the name
Or you could add a 3 digits random number to all file names so to avoid equal names with
Environ("userprofile") + "\Desktop\Output\" + dokname + Format(Int(Rnd * 1000), "000") + ".pdf"
Chees
Sergio
 
Last edited:
Upvote 0
Hi Mr Sergio.,
Thanks alot ,your formula really work.But may I ask is it possible if all the same name
 
Upvote 0
Hi Mr Sergio.,
Thanks alot ,your formula really work.But may I ask is it possible if all the same name customer data are combined in one PDF in this macro formula
 
Upvote 0
I am glad the random number formula helped in solving the problem, it is possible that the same name customer data are combined in one PDF but for that to happen you would need to re-engineering the whole Macro which could be quite a task
Cheers
Sergio
 
Upvote 0
Insted of Format(Int(Rnd * 1000), "000") you may use Now()
With the fist part you have filenames probably different but you may be so unlucky to have same random number occurring on same customer name.
Neither NOW() is sure: if you have a fast CPU and the same customer's name is present 2 or more times consecutively you will meet your former problem.

Why not adding the receipt's number to customer's name in file name?


As Sergio pointed out building a n-Pages PDF which collects all official receipst of the same customer is another task that I prefer to solve with PDFcreator. This open source app accepts vbscripts.

I suggest to keep every receipt as a stand alone file for further reference (IMHO)

Bye
 
Upvote 0
Hi,
My official receipt number can't be entered for the file name bcoz it consist of / where error will happen. So is there are any alternative for me to put in sequence number follow by month example, 1.12,2.12 and etc.
 
Upvote 0
You could replace "/" with "-" and use the receipt number for the name using: receiptnumber = Replace(receiptnumber, "/", "-")
Cheers
Sergio
 
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,036
Members
449,205
Latest member
Eggy66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top