Vba 在MS Word 2016中选择并重新定位图像
我是一名科学作家。有时,我会在大型文档中重复执行任务,并且每天都有许多文档。一项任务是在每页中插入一个图像(不同页面的图像不同),并将图像放置在页面的左下角(.45英寸页面右侧;10.35英寸页面顶部下方) 在插入图像后,我尝试录制一个简单的宏以在每页上使用,但MS Word不允许我在录制宏时选择图像 我想VBA可能会帮助我,但我无法想出一种方法;我只有VBA的基本知识。我想“拍摄一张预选图像并在页面中重新定位。”Vba 在MS Word 2016中选择并重新定位图像,vba,ms-word,Vba,Ms Word,我是一名科学作家。有时,我会在大型文档中重复执行任务,并且每天都有许多文档。一项任务是在每页中插入一个图像(不同页面的图像不同),并将图像放置在页面的左下角(.45英寸页面右侧;10.35英寸页面顶部下方) 在插入图像后,我尝试录制一个简单的宏以在每页上使用,但MS Word不允许我在录制宏时选择图像 我想VBA可能会帮助我,但我无法想出一种方法;我只有VBA的基本知识。我想“拍摄一张预选图像并在页面中重新定位。” SatxJoe我通常会要求你表现出更多的努力,但我知道你不是一个软件工程师,处理
SatxJoe我通常会要求你表现出更多的努力,但我知道你不是一个软件工程师,处理图像是很麻烦的。我花了相当长的时间为自己的应用程序解决这个问题。虽然看起来很简单,但我开始的时候不是这样 试试下面的初学者工具包。您必须添加自己的错误处理以及所需的任何参数和逻辑。此外,键盘快捷键也很方便 它假定图像不是内联图像(请参见Word中的定位选项),而是在页面上“浮动”。这使得重新定位它成为可能。由于内联和非内联的代码不同,因此需要更多的代码。我已经添加了一段特定的代码,以获得一个选定的内联图像,并使其成为非内联图像,但已注释掉。使用错误处理,您可以使代码在这两种情况下都工作 从您的描述来看,似乎您需要页脚中的图像,并且每页上都有不同的图像。这并不是那么简单,但最终是可行的。这段代码只是将其重新定位到页面本身上
' TO DO: add error handling. Code assumes that the selection is the image
' If it is not then an exception is thrown
Dim selectedShape As Shape
' If the image is not an inline image
Set selectedShape = ActiveDocument.Shapes(Selection.ShapeRange.Name)
' If the image is an inline image then
' Set selectedShape = Selection.InlineShapes(1).ConvertToShape
With selectedShape
' Allow text to wrap around the image
.WrapFormat.Type = WdWrapType.wdWrapFront
' Fix the anchor (more stable repositioning)
.LockAnchor = True
' Size the picture
.LockAspectRatio = True
.Height = Application.InchesToPoints(1)
' Position the picture - use absolution position relative to page edges
.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage
.Top = Application.InchesToPoints(10.35)
.Left = ActiveDocument.PageSetup.PageWidth - Application.InchesToPoints(0.45) - .Width
End With
我通常会要求你展示更多自己的努力,但我知道你不是一个软件工程师,处理图像是很麻烦的。我花了相当长的时间为自己的应用程序解决这个问题。虽然看起来很简单,但我开始的时候不是这样 试试下面的初学者工具包。您必须添加自己的错误处理以及所需的任何参数和逻辑。此外,键盘快捷键也很方便 它假定图像不是内联图像(请参见Word中的定位选项),而是在页面上“浮动”。这使得重新定位它成为可能。由于内联和非内联的代码不同,因此需要更多的代码。我已经添加了一段特定的代码,以获得一个选定的内联图像,并使其成为非内联图像,但已注释掉。使用错误处理,您可以使代码在这两种情况下都工作 从您的描述来看,似乎您需要页脚中的图像,并且每页上都有不同的图像。这并不是那么简单,但最终是可行的。这段代码只是将其重新定位到页面本身上
' TO DO: add error handling. Code assumes that the selection is the image
' If it is not then an exception is thrown
Dim selectedShape As Shape
' If the image is not an inline image
Set selectedShape = ActiveDocument.Shapes(Selection.ShapeRange.Name)
' If the image is an inline image then
' Set selectedShape = Selection.InlineShapes(1).ConvertToShape
With selectedShape
' Allow text to wrap around the image
.WrapFormat.Type = WdWrapType.wdWrapFront
' Fix the anchor (more stable repositioning)
.LockAnchor = True
' Size the picture
.LockAspectRatio = True
.Height = Application.InchesToPoints(1)
' Position the picture - use absolution position relative to page edges
.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage
.Top = Application.InchesToPoints(10.35)
.Left = ActiveDocument.PageSetup.PageWidth - Application.InchesToPoints(0.45) - .Width
End With
您的要求并不完全清楚,因此我的假设如下:
Option Explicit
Public Sub InsertPictureBottomLeft()
Dim location As Range
Set location = Selection.Range
Dim filename As String
filename = GetPictureFileName
If filename = vbNullString Then Exit Sub
Dim picture As Shape
'add the picture
Set picture = _
ActiveDocument.Shapes.AddPicture(filename:=filename, _
LinkToFile:=False, SaveWithDocument:=True, Anchor:=location)
LayoutPictureBottomLeft picture
End Sub
Public Sub MoveSelectedPictureBottomLeft()
Dim picture As Shape
On Error Resume Next
Set picture = Selection.ShapeRange(1)
'if selected picture is InlineShape we get an error
If Err Then Set picture = Selection.InlineShapes(1).ConvertToShape
'reset error handling
On Error GoTo 0
LayoutPictureBottomLeft picture
End Sub
Private Function GetPictureFileName() As String
Dim dlgPicture As Word.Dialog
'display the picture dialog
Set dlgPicture = Dialogs(wdDialogInsertPicture)
With dlgPicture
.Display
GetPictureFileName = .Name
End With
End Function
Private Sub LayoutPictureBottomLeft(picture As Shape)
With picture
.LayoutInCell = True
.LockAspectRatio = msoTrue
.WrapFormat.Type = wdWrapTopBottom
.Left = wdShapeLeft
.RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
.Top = wdShapeBottom
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
'for placement relative to page comment out previous 4 lines
'and uncomment next 4 lines
'.Left = InchesToPoints(0.45)
'.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
'.Top = InchesToPoints(10.35)
'.RelativeVerticalPosition = wdRelativeVerticalPositionPage
End With
End Sub
您的要求并不完全清楚,因此我的假设如下:
Option Explicit
Public Sub InsertPictureBottomLeft()
Dim location As Range
Set location = Selection.Range
Dim filename As String
filename = GetPictureFileName
If filename = vbNullString Then Exit Sub
Dim picture As Shape
'add the picture
Set picture = _
ActiveDocument.Shapes.AddPicture(filename:=filename, _
LinkToFile:=False, SaveWithDocument:=True, Anchor:=location)
LayoutPictureBottomLeft picture
End Sub
Public Sub MoveSelectedPictureBottomLeft()
Dim picture As Shape
On Error Resume Next
Set picture = Selection.ShapeRange(1)
'if selected picture is InlineShape we get an error
If Err Then Set picture = Selection.InlineShapes(1).ConvertToShape
'reset error handling
On Error GoTo 0
LayoutPictureBottomLeft picture
End Sub
Private Function GetPictureFileName() As String
Dim dlgPicture As Word.Dialog
'display the picture dialog
Set dlgPicture = Dialogs(wdDialogInsertPicture)
With dlgPicture
.Display
GetPictureFileName = .Name
End With
End Function
Private Sub LayoutPictureBottomLeft(picture As Shape)
With picture
.LayoutInCell = True
.LockAspectRatio = msoTrue
.WrapFormat.Type = wdWrapTopBottom
.Left = wdShapeLeft
.RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
.Top = wdShapeBottom
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
'for placement relative to page comment out previous 4 lines
'and uncomment next 4 lines
'.Left = InchesToPoints(0.45)
'.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
'.Top = InchesToPoints(10.35)
'.RelativeVerticalPosition = wdRelativeVerticalPositionPage
End With
End Sub
非常感谢。我期待着尝试一下。我期待着学习你们所做的工作并从中学习。我感谢你花时间来帮助我。谢谢。我期待着尝试一下。我期待着学习你们所做的工作并从中学习。我知道