从word文件

时间:2018-03-19 10:12:56

标签: vba excel-vba ms-office excel

我使用word对象来引用word文档。我正在将一些图像复制到每个页面,并将150多页的文字文件转换为PDF。

Dim wordapp As Object
Set wordapp = CreateObject("Word.Application")

将第一页添加到我正在使用的文件中

wordapp.documents.Add

要添加下一页我正在使用下一行中的代码,

wordapp.ActiveDocument.Sections.Add

上面的代码不会在word文件中插入任何空白页面,并且将其转换为pdf是成功的。

供您参考,

在我的计算机环境中是Windows 10,Office 2013.但是当我在另一台计算机上使用它时,它会引入空白页面。这里是Windows 10和Office 2010。

完整代码在这里。     页面编号 指定它是我们第一次尝试创建单词dcument。所以只添加了嵌套时间页面

If pagenumber = 1 Then
Dim wordapp As Object

Set wordapp = CreateObject("Word.Application")
wordapp.documents.Add
wordapp.Visible = False
Application.Wait (Now + TimeValue("0:00:03"))
With wordapp.ActiveDocument.PageSetup
     .LeftMargin = 36
     .RightMargin = 36
     .TopMargin = 36
     .BottomMargin = 36
End With
Else
    'add page at end AND copy the picture ahead of it
    wordapp.ActiveDocument.Sections.Add
    Application.Wait (Now + TimeValue("0:00:03"))
End If

Dim selrange As Range
Application.CutCopyMode = False
Workbooks("" & workbookname).Sheets("Tax Invoice     Format").Range("A1:L47").CopyPicture Appearance:=xlScreen, Format:=xlPicture
wordapp.Selection.Goto wdGoToPage, wdGoToAbsolute, count:=pagenumber
wordapp.Selection.Paste (ppPasteEnhancedMetafile)
wordapp.ActiveDocument.inlineshapes(pagenumber).LockAspectRatio =     msoFalse
wordapp.ActiveDocument.inlineshapes(pagenumber).Height = 735
wordapp.ActiveDocument.inlineshapes(pagenumber).Width = 540
Application.Wait (Now + TimeValue("0:00:03"))

pagenumber = pagenumber + 1
*loop continues*

1 个答案:

答案 0 :(得分:1)

您遇到困难的原因是Sections.Add不是将新网页插入Word文档的方法。一句话"部分"定义一组页面布局格式,例如纵向与横向,不同的边距设置,不同的页眉和页脚等。

有不同类型的"分节符",其中一个创建新页面。如果您没有指定分页符的类型,Word将使用本地默认设置(可以由用户确定)。所以可能在一台机器"下一页"是默认值,另一个是"连续"。

插入新页面的正确方法是

Range.InsertBreak Word.WdBreakType.wdPageBreak 'or the long equivalent: 7

此外,您的代码效率低下,否则不准确。与在Excel中一样,使用底层对象(尤其是Range对象)会更好。

以下是一些建议:

'Declare and assign these as appropriate - unsure since we don't see all the code...
Dim wordDoc as Object
Dim wordRange as Object 
Dim wordInlineShape as Object

If pagenumber = 1 Then
  Dim wordapp As Object

  Set wordapp = CreateObject("Word.Application")
  Set wordDoc = wordapp.documents.Add
  Set wordRange = wordDoc.Content
  wordapp.Visible = False
  Application.Wait (Now + TimeValue("0:00:03"))
  With wordDoc.PageSetup
     .LeftMargin = 36
     .RightMargin = 36
     .TopMargin = 36
     .BottomMargin = 36
  End With
Else
    'add page at end AND copy the picture ahead of it
    wordRange.Collapse 0 'Word.WdCollapseDirection.wdCollapseEnd
    wordRange.InsertBreak 7
    Application.Wait (Now + TimeValue("0:00:03"))
End If

Dim selrange As Range
Application.CutCopyMode = False
Workbooks("" & workbookname).Sheets("Tax Invoice Format").Range("A1:L47").CopyPicture Appearance:=xlScreen, Format:=xlPicture

wordRange.Collapse 0 'Word.WdCollapseDirection.wdCollapseEnd
wordRange.Paste (ppPasteEnhancedMetafile)
Set wordInlineShape = wordDoc.Inlineshapes(pagenumber).LockAspectRatio = msoFalse
wordInlineShape.Height = 735
wordInlineShape.Width = 540
Application.Wait (Now + TimeValue("0:00:03"))

pagenumber = pagenumber + 1
*loop continues*