简体   繁体   English

从Excel复制内容并粘贴到MS Word中的宏

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

I have an idea for a report generation macro from Excel to Word. 我有一个从Excel到Word的报告生成宏的想法。

The report format in excel is as below . Excel中的报告格式如下。

Excel中报告格式的模拟屏幕

The report in word 文字报告

There is an existing code as below to copy and paste in word . 现有以下代码可复制并粘贴到word中。 But is there a way to format it as in the above screenshot? 但是有没有办法像上面的屏幕截图一样格式化它?

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

source : http://www.rondebruin.nl/mac/mac030.htm 来源: http : //www.rondebruin.nl/mac/mac030.htm

Paste your data in. Stop your Macro ( stop command will do that), then record your formatting ( Alt + T, M, R ). 粘贴您的数据。停止Macro( stop命令将执行此操作),然后记录格式( Alt + T,M,R )。 Go get the recorded macro, and paste it into your macro, fixing the object you are working on (eg .ActiveDocument to AppWD.ActiveDocument though you probably don't have to do). 去获取录制的宏,然后将其粘贴到宏中,修复您正在处理的对象(例如,将.ActiveDocumentAppWD.ActiveDocument尽管您可能不必这样做)。

COM have changed over the years, and is now recommended to GetObject the Document object, not the Application Object. 多年来,COM发生了变化,现在建议将GetObject对象作为Document对象而不是Application对象。 Among other minor things it elininates reference counting problems on the Application object, this where the application doen't exit when closed. 它消除了Application对象上的引用计数问题,其中,应用程序在关闭时不会退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM