文本框移动到word文档vba宏中最后一页的顶部
我正在为word文档编写vba宏。我使用vba宏生成文本框和word文档的文本。问题是文本框移动到最后一页的顶部,而不是停留在第一页上 我不知道我做错了什么我只需要将文本框保留在第一页。我真的需要将文本框包括在内 下面是我的代码和输出图像文本框移动到word文档vba宏中最后一页的顶部,vba,ms-word,Vba,Ms Word,我正在为word文档编写vba宏。我使用vba宏生成文本框和word文档的文本。问题是文本框移动到最后一页的顶部,而不是停留在第一页上 我不知道我做错了什么我只需要将文本框保留在第一页。我真的需要将文本框包括在内 下面是我的代码和输出图像 Dim wrdDoc As Object Dim tmpDoc As Object Dim WDoc As String Dim myDoc As String myDoc = "myTest" WDoc = ThisDocument.Path &
Dim wrdDoc As Object
Dim tmpDoc As Object
Dim WDoc As String
Dim myDoc As String
myDoc = "myTest"
WDoc = ThisDocument.Path & "\mydocument.docx"
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If wdApp Is Nothing Then
' no current word application
Set wdApp = CreateObject("Word.application")
Set wrdDoc = wdApp.Documents.Open(WDoc)
wdApp.Visible = True
Else
' word app running
For Each tmpDoc In wdApp.Documents
If StrComp(tmpDoc.FullName, WDoc, vbTextCompare) = 0 Then
' this is your doc
Set wrdDoc = tmpDoc
Exit For
End If
Next
If wrdDoc Is Nothing Then
' not open
Set wrdDoc = wdApp.Documents.Open(WDoc)
End If
End If
ActiveDocument.Content.Select
Selection.Delete
With wdApp
.Visible = True
.Activate
With .Selection
Dim objShape As Word.Shape
Set objShape2 = ActiveDocument.Shapes.addTextbox _
(Orientation:=msoTextOrientationHorizontal, _
Left:=400, Top:=100, Width:=250, Height:=60)
With objShape2
.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.Left = wdShapeRight
.Top = wdShapeTop
.TextFrame.TextRange = "This is nice and shine" & vbCrLf & "222"
.TextFrame.TextRange.ParagraphFormat.Alignment = wdAlignParagraphLeft
End With
End With
With .Selection
.TypeParagraph
.TypeParagraph
.TypeParagraph
.TypeParagraph
.TypeParagraph
.TypeParagraph
.TypeParagraph
For i = 1 To 40
.TypeText i
.TypeParagraph
Next i
End With
End With
Word
Shape
对象必须定位到Word文档中的字符位置。它们将始终出现在锚定字符所在的页面上,如果锚定格式不在页面上,它们将在带有锚定字符的页面上相对移动
当文档为“空”(一个单独的段落)时,会出现一种特殊情况,因此它有助于确保文档中包含多个字符。在下面的代码示例中,在将文本框添加到第一个段落之前,会插入一个附加段落
我对代码做了一些其他调整:
,以便显示错误消息。否则,调试将变得不可能
,因为在使用Word对象时不需要它
范围
对象插入内容。与Excel一样,最好尽可能不要使用选择
wrdDoc
对象,而不是ActiveDocument
Dim wrdDoc As Object
Dim tmpDoc As Object
Dim WDoc As String
Dim myDoc As String
myDoc = "myTest"
WDoc = ThisDocument.Path & "\mydocument.docx"
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
On Error GoTo 0
If wdApp Is Nothing Then
' no current word application
Set wdApp = CreateObject("Word.application")
Set wrdDoc = wdApp.Documents.Open(WDoc)
wdApp.Visible = True
Else
' word app running
For Each tmpDoc In wdApp.Documents
If StrComp(tmpDoc.FullName, WDoc, vbTextCompare) = 0 Then
' this is your doc
Set wrdDoc = tmpDoc
Exit For
End If
Next
If wrdDoc Is Nothing Then
' not open
Set wrdDoc = wdApp.Documents.Open(WDoc)
End If
End If
wdApp.Visible = True
wrdApp.Activate
Dim i As Long
Dim objShape2 As Word.Shape
Dim rng As Word.Range
Set rng = wrdDoc.Content
rng.Delete
With rng
.InsertAfter vbCr
.Collapse wdCollapseStart
Set objShape2 = ActiveDocument.Shapes.AddTextbox _
(Orientation:=msoTextOrientationHorizontal, _
Left:=400, Top:=100, Width:=250, Height:=60, Anchor:=rng)
With objShape2
.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.Left = wdShapeRight
.Top = wdShapeTop
.TextFrame.TextRange = "This is nice and shine" & vbCrLf & "222"
.TextFrame.TextRange.ParagraphFormat.Alignment = wdAlignParagraphLeft
End With
rng.Start = ActiveDocument.Content.End
For i = 1 To 40
.Text = i & vbCr
.Collapse wdCollapseEnd
Next i
End With
另一个解决方案供您参考
'12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
'========1=========2=========3=========4=========5=========6=========7=========8=========9=========A=========B=========C
Option Explicit
Sub textboxtest()
Const my_doc_name As String = "mydocument.docx"
Dim my_fso As Scripting.FileSystemObject
Dim my_doc As Word.Document
Dim my_range As Word.Range
Dim counter As Long
Dim my_text_box As Word.Shape
Dim my_shape_range As Word.ShapeRange
' There is no need to test for the Word app existing
' if this macro is in a Word template or Document
' because to run the macro Word MUST be loaded
Set my_fso = New Scripting.FileSystemObject
If my_fso.FileExists(ThisDocument.Path & "\" & my_doc_name) Then
Set my_doc = Documents.Open(ThisDocument.Path & "\" & my_doc_name)
Else
Set my_doc = Documents.Add
my_doc.SaveAs2 ThisDocument.Path & "\" & my_doc_name
End If
my_doc.Activate ' Although it should already be visible
my_doc.content.Delete
Set my_text_box = my_doc.Shapes.AddTextbox( _
Orientation:=msoTextOrientationHorizontal, _
left:=400, _
top:=100, _
Width:=250, _
Height:=60)
With my_text_box
.Name = "TextBox1"
.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.left = wdShapeRight
.top = wdShapeTop
With .TextFrame
.TextRange = "This is nice and shine" & vbCrLf & "222"
.TextRange.ParagraphFormat.Alignment = wdAlignParagraphLeft
End With
End With
Set my_range = my_text_box.Parent.Paragraphs(1).Range
'FROM
'
' https://docs.microsoft.com/en-us/office/vba/api/word.shape'
' Every Shape object is anchored to a range of text. A shape is anchored
' to the beginning of the first paragraph that contains the anchoring
' range. The shape will always remain on the same page as its anchor.
my_range.Collapse Direction:=wdCollapseEnd
With my_range
For counter = 1 To 90
.Text = counter
.InsertParagraphAfter
.Collapse Direction:=wdCollapseEnd
Next
End With
End Sub
只是一些日常工作-您发布的代码没有设置的
wdApp
变量。然而,您无论如何都不需要它,因为您已经在word中完成了vba。如果您是使用excel的vba(事实上略有不同)创建此文件的,您只会引用这样的单词。wdApp在另一个文件中声明为全局性。我非常感谢您的回复。它起作用了,但我遇到的问题是使用新程序格式化文本。请告诉我如何使用此设置文本格式(例如粗体、大小、下划线)。我已经习惯了word.Application.Selection你能不能把这个问题作为一个新问题问一下,再加上一点细节或者一个示例和示例代码,以确保我们的波长相同?我所有的代码都在word.Application.Selection中。请帮我转换上面的代码以使用它,或者告诉我如何进行文本格式化。请将其作为新问题发布!你想在当前问题中使用的格式完全没有问题!你需要花点时间在一个新问题中举例说明你需要做什么。在堆栈溢出中,这是一个主题/问题。尽管这个答案很好。如果FOR循环不用于显示其他内容,并且我们有五行.Text=”“,则只执行第五行。请帮助我。我所有的代码都是用word.application.selection完成的。如何使用此方法输入粗体或字体大小使用VBA IDE中的intellisense检查可用的选项。单击某个关键字,然后按F1键打开该关键字的MS帮助页面。在VBA IDE中,确保Tools.Options.Editor.Code设置中的所有复选框都有一个勾号。确保你在每个模块的顶部都有明确的选项。段落格式和字体效果等都是这个范围的属性。e、 g.my_range.font.Size=12或my_range.ParagraphFormat.Alignment=wdAlignParagraphCenter