Printing to PDF with specified filename

T

terje.eriksen

Hi,
Im working on a excel sheet and my plan is as followed:
-by pressing commandbutton, it automatically prints the file to pdf,
to a specified location, and then I want a filename specified from a
number
on one of the cells.

My code now:

Private Sub commandbutton1_Click()

Call printthis


'
End Sub

Sub printthis()


Application.ActivePrinter = "Win2PDF på Ne00:"
ActiveWindow.SelectedSheets.PrintOut From:=1, To:=1, Copies:=1, _
ActivePrinter:="Win2PDF på Ne00:", Collate:=True,
PrToFilename:="c:\filename.pdf"
End Sub

Is there someting I could insert instead of "filename.pdf" to get the
name to be the same as the number in cell E2?
 
J

Joel

MyFilenumber = CStr(ActiveSheet.Range("E2"))
MyFilename = "c:\MyString" + MyFilenumber

then in the Print statement
PrToFilename:=MyFilename
 
T

terje.eriksen

MyFilenumber = CStr(ActiveSheet.Range("E2"))
MyFilename = "c:\MyString" + MyFilenumber

then in the Print statement
PrToFilename:=MyFilename












- Vis sitert tekst -

Thanks, the only problem I got now is that I wont get the file
extention addet to the filename,
tried to use this string:
MyFilename = "c:\MyString" + MyFilenumber + .pdf, but this didn't
work.
 
T

terje.eriksen

Thanks, the only problem I got now is that I wont get the file
extention addet to the filename,
tried to use this string:
MyFilename = "c:\MyString" + MyFilenumber + .pdf, but this didn't
work.- Skjul sitert tekst -

- Vis sitert tekst -

*Bump*

Anyone who coul help me with this? I'm not getting the file extention
in the end of the filename.
 
A

Alan

File is stored as C:\MyString\MyFilenumber.pdf
MyFilename = ("C:\MyString" & MyFilenumber & ".pdf")
'MyString is an actual folder, not a variable name folder.


File is stored as C:\MyStringMyFilenumber.pdf
MyFilename = ("C:\" & MyString & MyFilenumber & ".pdf")
'MyString and MyFilenumber are both variables.
'They are combined to form one filename.


File is stored as C:\MyString\MyFilenumber.pdf
MyFilename = ("C:\" & MyString & "\" & MyFilenumber & ".pdf")
'MyString = a variable folder name.
'MyFilenumber = a variable file name

--

Regards,

Alan


Thanks, the only problem I got now is that I wont get the file
extention addet to the filename,
tried to use this string:
MyFilename = "c:\MyString" + MyFilenumber + .pdf, but this didn't
work.- Skjul sitert tekst -

- Vis sitert tekst -

*Bump*

Anyone who coul help me with this? I'm not getting the file extention
in the end of the filename.
 
J

Joel

The only problem with your original code is you forgot the duble quotes

from:
MyFilename = "c:\MyString" + MyFilenumber + .pdf

MyFilename = "c:\MyString" + MyFilenumber + ".pdf"

If MyFilenumber is a number and not a string, then you also need the following

MyFilename = "c:\MyString" + CStr(myfilenumber) + ".pdf"
 
T

terje.eriksen

The only problem with your original code is you forgot the duble quotes

from:
MyFilename = "c:\MyString" + MyFilenumber + .pdf

MyFilename = "c:\MyString" + MyFilenumber + ".pdf"

If MyFilenumber is a number and not a string, then you also need the following

MyFilename = "c:\MyString" + CStr(myfilenumber) + ".pdf"





- Vis sitert tekst -

The only problem with your original code is you forgot the duble quotes

from:
MyFilename = "c:\MyString" + MyFilenumber + .pdf

MyFilename = "c:\MyString" + MyFilenumber + ".pdf"

If MyFilenumber is a number and not a string, then you also need the following

MyFilename = "c:\MyString" + CStr(myfilenumber) + ".pdf"





- Vis sitert tekst -

Oki,
I'm getting there. Now i only need to add another cell to the
filename,
I've got a unike number on each report that is written in Excel, this
is made up from an increasing number followed by this year, like this:
SR:23-07 The number 23 is reportnumber in cell E2 and the year in cell
F2, the year number is this function =Now()
..
But im not allowed to use both in the filename, Could this be because
the number is a result of a function?

my code at the moment:

Private Sub knappen_Click()

Call printa

End Sub

Sub printa()

MyFilenumber = CStr(ActiveSheet.Range("E2") + ActiveSheet.Range("F2"))
MyFilename = "c:\test\SR-" + MyFilenumber + ".pdf"



Application.ActivePrinter = "Win2PDF på Ne00:"
ActiveWindow.SelectedSheets.PrintOut From:=1, To:=1, Copies:=1, _
ActivePrinter:="Win2PDF på Ne00:", Collate:=True,
PrToFilename:=MyFilename
End Sub




Terje
 
J

Joel

I'm a little confused about you code especially this line

MyFilenumber = CStr(ActiveSheet.Range("E2") + ActiveSheet.Range("F2"))

I'm not usre if you understand the differences in how the plus sign (+) is
used. the plus sign can be used to add two numbers together. It can also be
used to combine two strings together. You need to understand what ae string
and what are numbers. Anything between double quotes "This is a string
because it is between quotes. This is also a string "123" even though its 3
numeric digits. these three number 123 are numbers because they aren't any
quotes.

On an excel spreadsheet anything that has double quotes or anything that
starts with a single quote '123 are also strings and not numbers.

the reason I'm explaining this is because of this one line

MyFilenumber = CStr(ActiveSheet.Range("E2") + ActiveSheet.Range("F2"))


Are E2 and F2 numbers or strings.
Lets say E2 = 10 and F2 = 15

If they are numbers your answer would be MyFilenumber = 15

If they are strings your answer would be MyfileNumber = "1015".

Is the code now working???
You can add some debug code to see what Myfilename value is. Add to your code

msgbox(Myfilename)

so you can see if you ccare getting the correct results.


Private Sub knappen_Click()

Call printa

End Sub

Sub printa()

MyFilenumber = CStr(ActiveSheet.Range("E2") + ActiveSheet.Range("F2"))
MyFilename = "c:\test\SR-" + MyFilenumber + ".pdf"



Application.ActivePrinter = "Win2PDF på Ne00:"
ActiveWindow.SelectedSheets.PrintOut From:=1, To:=1, Copies:=1, _
ActivePrinter:="Win2PDF på Ne00:", Collate:=True,
PrToFilename:=MyFilename
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top