Macos VBA-将Excel表格复制到Word-在PC上工作,在Mac上失败
我编写了一些VBA代码,当单击Excel单元格时,它会将数据从Excel复制到Word。程序如下:Macos VBA-将Excel表格复制到Word-在PC上工作,在Mac上失败,macos,excel,vba,ms-word,copy,Macos,Excel,Vba,Ms Word,Copy,我编写了一些VBA代码,当单击Excel单元格时,它会将数据从Excel复制到Word。程序如下: 1. Opens an already created word document, duplicates the word document, and then closes the original document; leaving open the copy (duplicate) for further modification. 2. The code then finds and
1. Opens an already created word document, duplicates the word document, and then closes the original document; leaving open the copy (duplicate) for further modification.
2. The code then finds and replaces some placeholder values within the word document with values from the Excel document.
3. The code then deletes 2 different tables (1 at a time), row by row, and replaces each table (1 at a time) with a table copied from Excel.
4. The code then displays a message to the user that all has been completed and exits.
所有代码在我的电脑上都能正常工作,但在同事的MAC电脑上失败,说明“错误:4605-命令不可用”,并且在wrdApp.Selection.PasteExcelTable False、False、True
代码行失败
代码如下:
Sub Copy2Word()
Dim wrdApp As Object
Dim tempDoc As Word.Document
Dim mrgDoc As Word.Document
Dim NumPay As Integer
Dim cll As Excel.Range
'GET NUMBER OF PAYMENTS SELECTED FOR USE BELOW
NumPay = Notated.Cells(Data.Range("DV2").Value, Data.Range("DV3").Value).Value
'GET LOCATION OF WORD FILE FROM USER
FName = Application.GetOpenFilename
If FName = False Then
usrErr = 1
GoTo ErrHnd
End If
'RECORD THE WORD FILE LOCATION ON HIDDEN SHEET FOR USE BY OTHER MACROS
MergeData.Range("B2").Value = FName
'CREATE WORD OBJECT
On Error Resume Next
Set wrdApp = GetObject(, "Word.Application")
On Error GoTo 0
If wrdApp Is Nothing Then
Set wrdApp = CreateObject("Word.Application")
End If
'DISPLAY WORD APPLICATION
On Error Resume Next
wrdApp.Visible = True
wrdApp.Activate
On Error GoTo 0
'OPEN THE (TEMPLATE) FILE
wrdApp.Documents.Open Filename:=FName
'SET A VARIABLE TO REFERENCE ACTIVE DOCUMENT (TEMPLATE)
Set tempDoc = wrdApp.ActiveDocument
'DUPLICATE THE DOCUMENT
wrdApp.Documents.Add wrdApp.ActiveDocument.FullName
'SET A VARIABLE TO REFERENCE THE NEW VERSION OF DOCUMENT
Set mrgDoc = wrdApp.ActiveDocument
'CLOSE THE ORIGINAL (TEMPLATE) VERSION OF DOCUMENT
tempDoc.Close SaveChanges:=False
'ACTIVATE THE NEW DOCUMENT
mrgDoc.Activate
'REPLACE PLACEHOLDER TEXT ITEMS WITH ACTUAL
For Each cll In MergeData.Range(MergeData.Cells(1, 3).Address & ":" & MergeData.Cells(1, MergeData.Range("A1").End(xlToRight).Column).Address)
If cll.Offset(1, 0).Value = "" Then
repTx = cll.Value
Else
repTx = cll.Offset(1, 0).Value
End If
With mrgDoc.Content.Find
.Text = cll.Value
.Replacement.Text = repTx
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Next cll
'REPLACE TABLE 2 ON WORD DOC
mrgDoc.Tables(2).Select
For ii = 30 To 2 Step -1
mrgDoc.Tables(2).Rows(ii).Delete
Next ii
wrdApp.Selection.TypeParagraph
'COPY AND PASTE TABLE 1 FROM EXCEL TO WORD
Application.CutCopyMode = False
EO_DOC.Range("EO_TBL_INSCOPE_1").Copy
wrdApp.Selection.PasteExcelTable False, False, True
''''REMAINDER OF CODE AND COMPLETION CONFIRMATION TO USER''''
End Sub
我尝试了很多不同的方法,比如添加DoEvents,等等,看看它是否能解决这个问题,但还没有找到解决方案
有没有MAC专家的VBA?有人吗
提前感谢。问题似乎是wrdApp.Activate的位置,它阻止Excel进行复制,阻止Word进行粘贴。在这里,在您的代码的简化版本中,我可以通过移动语句使其紧跟在PasteExcelTable行之前来实现复制/粘贴。如果必须保留行的副本,问题是(例如)在设置CutCopyMode之前立即激活Excel工作簿似乎不足以让Excel正确复制。(顺便说一句,我不是Mac VBA专家!)您使用的是哪个Office for Mac版本?我目前使用的是Windows 7和Office 2007,我相信我的同事正在使用Office 2011 for Mac谢谢您的回答。如果我将来遇到类似的情况,这将是很有帮助的,但是,我已经实现了一项工作,我能够一起删除复制/粘贴例程。