簡體   English   中英

從Excel復制內容並粘貼到MS Word中的宏

[英]Macro to Copy Content from Excel and paste in MS Word

我有一個從Excel到Word的報告生成宏的想法。

Excel中的報告格式如下。

Excel中報告格式的模擬屏幕

文字報告

現有以下代碼可復制並粘貼到word中。 但是有沒有辦法像上面的屏幕截圖一樣格式化它?

Sub TestingMacAndWin()
Dim appWD As Object
Dim wddoc As Object

On Error Resume Next
Set appWD = GetObject(, "Word.application")    'gives error 429 if Word is not open
If Err = 429 Then
    Set appWD = CreateObject("Word.application")    'creates a Word application
    Err.Clear
End If

Set wddoc = appWD.Documents.Add
appWD.Visible = True

With appWD.ActiveDocument.PageSetup
    .TopMargin = appWD.InchesToPoints(0.3)
    .BottomMargin = appWD.InchesToPoints(0.3)
    .LeftMargin = appWD.InchesToPoints(0.3)
    .RightMargin = appWD.InchesToPoints(0.3)
End With

Sheets("Sheet1").Range("A1:D2").CopyPicture xlScreen
appWD.Selection.Paste

appWD.Activate

End Sub

來源: http : //www.rondebruin.nl/mac/mac030.htm

粘貼您的數據。停止Macro( stop命令將執行此操作),然后記錄格式( Alt + T,M,R )。 去獲取錄制的宏,然后將其粘貼到宏中,修復您正在處理的對象(例如,將.ActiveDocumentAppWD.ActiveDocument盡管您可能不必這樣做)。

多年來,COM發生了變化,現在建議將GetObject對象作為Document對象而不是Application對象。 它消除了Application對象上的引用計數問題,其中,應用程序在關閉時不會退出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM