2011-09-20 57 views
3

我有一個生成網頁的宏的工作表(代碼如下)。我想改變它,所以它會生成PDF。這可以做到嗎?如何在Excel的VB宏中發佈到PDF?

Sub publishwebpages() 

'calculate how many iterations 
    x = 0 
    Sheets("webpagegen").Select 
    Range("n1").Select 
    Range("n1").Copy 
    noofpages = ActiveCell.Value 


'step through and select each customer 

    For x = 0 To noofpages 
    'For x = 364 To 366 

    Sheets("listcust").Select 
    Range("A2").Select 
    ActiveCell.Offset(x, 0).Range("A1").Select 
    Selection.Copy 
    Sheets("webpagegen").Select 
    Range("l1").Select 
    ActiveSheet.Paste 



'name folder and filename 
    Sheets("webpagegen").Select 
    Range("ac2").Select 
    Range("ac2").Copy 
    foldername = ActiveCell.Value 


'publish pages 
    Range("d3:q80").Select 
    Application.CutCopyMode = False 
    Selection.Copy 
    ActiveWorkbook.PublishObjects.Add(xlSourceRange, "D:\Temp\" & foldername, "webpagegen", "$d$3:$q$80", xlHtmlStatic, "custweb08 current_8162", "").Publish (True) 


    Next x 

End Sub 
+0

什麼版本的Excel? –

+0

Office Excel 2010 – TruMan1

回答

2

如果您使用的是Excel 2010>,它可以讓你保存工作簿/內置在功能Excel工作表爲PDF格式,我們曾經有過報告特別在Excel正因爲你:)

運行

您可以嘗試同時試圖記錄「另存爲...」爲PDF格式,它可能是這樣的:

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ 
     "c:\Book1.pdf", Quality:= _ 
     xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _ 
     OpenAfterPublish:=True 

注:我已經嘗試過在Excel 2010中,它可能看起來在其他版本中的不同。

+1

Excel 2007也有這個功能 – waqasahmed

+0

雅但你如何在VBA宏中使用它?在「ActiveWorkbook.PublishObjects.Add(xlSource ...」 – TruMan1

+1

TruMan - 您嘗試錄製宏嗎? –