VBA代码将图像从源Word文档复制到新的Word文档。

huangapple go评论87阅读模式
英文:

VBA code to copy images from source Word document to a new Word document

问题

以下是翻译好的代码部分:

子复制图像到新文档()
    Dim 源文档 As 文档
    Set 源文档 = 活动文档
    
    ' 创建一个新文档
    Dim 新文档 As 文档
    Set 新文档 = 文档.Add
    
    ' 遍历源文档中的形状
    Dim 行内形状 As 行内形状
    For Each 行内形状 In 源文档.行内形状
        ' 检查形状是否是图像(例如图片、图表等)
        If 行内形状.Type = wdInlineShapePicture Then
            ' 复制图像到新文档
            行内形状.Select
            选择复制
            新文档.Range.Paste
            新文档.Range.InsertParagraphAfter ' 在每个图像后添加段落分隔
        End If
    Next 行内形状
    
    ' 保存并关闭新文档
    ' 根据需要修改路径和文件名
    新文档.SaveAs2 "C:\Users\xyz\Desktop\NewDocument.docx"
    新文档.Close
    
    MsgBox "图像已复制到新文档。"
End Sub
英文:

Use case: I have a few png/jpeg images that I wish to copy over from a source document to a new document.

I have put together the following code to achieve this objective. However, in my new document all I see copied over is the last image from the source document.

What am I missing here please:

Sub CopyFiguresToNewDocument()
    Dim sourceDoc As Document
    Set sourceDoc = ActiveDocument
    
    ' Create a new document
    Dim newDoc As Document
    Set newDoc = Documents.Add
    
    ' Iterate through the shapes in the source document
    Dim inlineshape As inlineshape
    For Each inlineshape In sourceDoc.InlineShapes
        ' Check if the shape is a figure (e.g., picture, chart, etc.)
        If inlineshape.Type = wdInlineShapePicture Then
            ' Copy the figure to the new document
            inlineshape.Select
            Selection.Copy
            newDoc.Range.Paste
            newDoc.Range.InsertParagraphAfter ' Add a paragraph break after each figure
        End If
    Next inlineshape
    
    ' Save and close the new document
    ' Modify the path and filename as desired
    newDoc.SaveAs2 "C:\Users\xyz\Desktop\NewDocument.docx"
    newDoc.Close
    
    MsgBox "Figures copied to the new document."
End Sub

答案1

得分: 1

这个已校正的代码在我的测试文档上有效:

Sub CopyFiguresToNewDocument()
    Dim sourceDoc As Document
    Set sourceDoc = ActiveDocument
    
    ' 创建一个新文档
    Dim newDoc As Document
    Set newDoc = Documents.Add
    
    ' 遍历源文档中的形状
    Dim s As InlineShape
    For Each s In sourceDoc.InlineShapes
        ' 检查形状是否为图形(例如图片、图表等)
        If s.Type = wdInlineShapePicture Then
            ' 复制图形到新文档
            s.Select
            Selection.Copy
            newDoc.Range(newDoc.Content.End - 1).Paste
            newDoc.Range.InsertParagraphAfter ' 在每个图形后添加段落分隔符
        End If
    Next
    
    ' 保存并关闭新文档
    ' 根据需要修改路径和文件名
    newDoc.SaveAs2 "C:\Users\H182720\Desktop\NewDocument.docx"
    newDoc.Close
    
    MsgBox "图形已复制到新文档。"
End Sub

注意这一行:newDoc.Range(newDoc.Content.End - 1).Paste,这将在当前内容的末尾粘贴图形,而不是替换整个内容,与您的原始代码不同。

英文:

This corrected code works on my test document:

Sub CopyFiguresToNewDocument()
    Dim sourceDoc As Document
    Set sourceDoc = ActiveDocument
    
    ' Create a new document
    Dim newDoc As Document
    Set newDoc = Documents.Add
    
    ' Iterate through the shapes in the source document
    Dim s As inlineshape
    For Each s In sourceDoc.InlineShapes
        ' Check if the shape is a figure (e.g., picture, chart, etc.)
        If s.Type = wdInlineShapePicture Then
            ' Copy the figure to the new document
            s.Select
            Selection.Copy
            newDoc.Range(newDoc.Content.End - 1).Paste
            newDoc.Range.InsertParagraphAfter ' Add a paragraph break after each figure
        End If
    Next
    
    ' Save and close the new document
    ' Modify the path and filename as desired
    newDoc.SaveAs2 "C:\Users\H182720\Desktop\NewDocument.docx"
    newDoc.Close
    
    MsgBox "Figures copied to the new document."
End Sub

Note the line newDoc.Range(newDoc.Content.End - 1).Paste: this will paste it at the end of the current content, instead of replacing the whole content as your original code.

答案2

得分: 1

你的代码失败是因为newDoc.Range代表整个文档的内容,所以每次循环迭代只是替换现有内容。

在两个文档中都不需要选择任何内容,也不需要使用剪贴板。以下的代码将把图片放在新文档的末尾。

Sub CopyFiguresToNewDocument()
    Dim sourceDoc As Document
    Set sourceDoc = ActiveDocument
    
    ' 创建一个新文档
    Dim newDoc As Document
    Set newDoc = Documents.Add
    
    ' 遍历源文档中的形状
    Dim s As InlineShape
    For Each s In sourceDoc.InlineShapes
        ' 检查形状是否是图像(例如图片、图表等)
        If s.Type = wdInlineShapePicture Then
            ' 将图像复制到新文档
            newDoc.Characters.Last.FormattedText = s.Range.FormattedText
            newDoc.Range.InsertParagraphAfter ' 在每个图像后添加段落分隔
        End If
    Next
    
    ' 保存并关闭新文档
    ' 根据需要修改路径和文件名
    newDoc.SaveAs2 "C:\Users\H182720\Desktop\NewDocument.docx"
    newDoc.Close
    
    MsgBox "图片已复制到新文档。"
End Sub
英文:

Your code fails because newDoc.Range represents the entire content of the document, so each iteration of the loop simply replaces the existing content.

There is no need to select anything in either document, nor is there any need to use the clipboard. The following code will put the pictures at the end of a new document.

Sub CopyFiguresToNewDocument()
    Dim sourceDoc As Document
    Set sourceDoc = ActiveDocument
    
    ' Create a new document
    Dim newDoc As Document
    Set newDoc = Documents.Add
    
    ' Iterate through the shapes in the source document
    Dim s As InlineShape
    For Each s In sourceDoc.InlineShapes
        ' Check if the shape is a figure (e.g., picture, chart, etc.)
        If s.Type = wdInlineShapePicture Then
            ' Copy the figure to the new document
            newDoc.Characters.Last.FormattedText = s.Range.FormattedText
            newDoc.Range.InsertParagraphAfter ' Add a paragraph break after each figure
        End If
    Next
    
    ' Save and close the new document
    ' Modify the path and filename as desired
    newDoc.SaveAs2 "C:\Users\H182720\Desktop\NewDocument.docx"
    newDoc.Close
    
    MsgBox "Figures copied to the new document."
End Sub

答案3

得分: 0

子复制图像到新文档()
    Dim 源文档 As 文档
    Dim 新文档 As 文档
    Dim 源形状 As 形状
    Dim 新形状 As 形状
    
    ' 打开源文档
    Set 源文档 = 文档.打开("C:\路径\到\源文档.docx")
    
    ' 创建一个新文档
    Set 新文档 = 文档.添加
    
    ' 循环遍历源文档中的每个形状
    For Each 源形状 In 源文档.形状
        ' 检查形状是否为图像
        If 源形状.类型 = mso图片 Then
            ' 复制图像到新文档
            源形状.选择
            选择.复制
            
            ' 在新文档中粘贴图像
            新文档.范围.粘贴
            Set 新形状 = 新文档.形状(新文档.形状数)
            
            ' 如果需要,调整图像的位置和大小
            ' 例如:
            新形状.左 = 100
            新形状.顶 = 100
            新形状.锁定纵横比 = mso假
            新形状.宽度 = 200
            新形状.高度 = 200
        End If
    Next 源形状
    
    ' 保存并关闭新文档
    新文档.另存为 "C:\路径\到\新文档.docx"
    新文档.关闭
    
    ' 关闭源文档
    源文档.关闭
End Sub
英文:
Sub CopyImagesToNewDocument()
    Dim srcDoc As Document
    Dim newDoc As Document
    Dim srcShape As Shape
    Dim newShape As Shape
    
    ' Open the source document
    Set srcDoc = Documents.Open("C:\Path\To\SourceDocument.docx")
    
    ' Create a new document
    Set newDoc = Documents.Add
    
    ' Loop through each shape in the source document
    For Each srcShape In srcDoc.Shapes
        ' Check if the shape is an image
        If srcShape.Type = msoPicture Then
            ' Copy the image to the new document
            srcShape.Select
            Selection.Copy
            
            ' Paste the image in the new document
            newDoc.Range.Paste
            Set newShape = newDoc.Shapes(newDoc.Shapes.Count)
            
            ' Adjust the position and size of the image if needed
            ' For example:
            newShape.Left = 100
            newShape.Top = 100
            newShape.LockAspectRatio = msoFalse
            newShape.Width = 200
            newShape.Height = 200
        End If
    Next srcShape
    
    ' Save and close the new document
    newDoc.SaveAs "C:\Path\To\NewDocument.docx"
    newDoc.Close
    
    ' Close the source document
    srcDoc.Close
End Sub

huangapple
  • 本文由 发表于 2023年6月22日 16:55:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定