2014-07-26 18 views
0

我試圖編寫一個宏,它將在Word文檔中插入文本框,並使用內嵌文本文本打包來格式化它們。Word VBA:添加帶有內嵌文本包裝的文本框到文檔結尾

這裏是我到目前爲止的代碼:

Sub Example() 
    Dim newTextbox As Shape 
    For I = 1 To 10 
     Set newTextbox = ActiveDocument.Shapes.AddTextbox _ 
     (Orientation:=msoTextOrientationHorizontal, _ 
     Left:=0, Top:=0, Width:=100, Height:=50) 
     newTextbox.WrapFormat.Type = wdWrapInline 
     newTextbox.TextFrame.TextRange = I 
    Next 
End Sub 

我遇到的問題是,不是每一個文本框被添加到文檔的開始,因爲目前發生的事情,我需要它被添加到結束。我明白,在我給出的例子中,我可以簡單地使用For I = 10 To 1 Step -1。但是,由於我正在使用實際項目中的文本框,因此這是不可能的。

我花了幾個小時玩代碼,但一直沒能弄明白。預先感謝您的幫助。

喬希。

回答

0

約書亞,這裏是最後的工作代碼:

Sub InsertInlineTextBox() 

' Move all the text after the cursor to a new paragraph 
' and jump to the start point of this paragraph 
Selection.InsertParagraphAfter 
Selection.MoveDown Unit:=wdParagraph, count:=1 

Dim aShape As Shape 

' Insert the shape at the current cursor position +1 point down in vertical 
' direction to prevent automatic moving the shape to the previous paragraph 
' during 'inlining' 
Set aShape = ActiveDocument.Shapes.AddTextbox(_ 
    msoTextOrientationHorizontal, _ 
    Selection.Information(wdHorizontalPositionRelativeToPage), _ 
    Selection.Information(wdVerticalPositionRelativeToPage) + 1, 400, 60) 

With aShape 
    .TextFrame.MarginBottom = 0 ' adjust text margins 
    .TextFrame.MarginLeft = 0 
    .TextFrame.MarginRight = 0 
    .TextFrame.MarginTop = 0 
    .Line.Visible = msoFalse ' don't show the border 

    ' converting to InlineShape will place 
    ' the shape at the start point of paragraph 
    .ConvertToInlineShape 
End With 

' Remove carriege return before the shape 
Selection.EndOf Unit:=wdParagraph, Extend:=wdMove 
Selection.MoveLeft Unit:=wdCharacter, count:=1 
Selection.Delete Unit:=wdCharacter, count:=1 
End Sub 

我也用這個宏在文本框中 禁用拼寫檢查,因爲通常它們包含了一堆的C++代碼示例:

Sub NoSpellCheck() 
    Selection.Range.SpellingChecked = True 
    Selection.Range.NoProofing = True 
End Sub 
+0

感謝您的回答 - 我猜你不在Word 2013中?我實際上在[另一個論壇]上提出了這個問題(http://answers.microsoft.com/zh-cn/office/forum/office_2013_release-customize/vba-add-textboxes-with-in-line-text-wrapping-to/42b4ebc5-6dde-4a5f-b4db-48e1ab0b8aa0),並從那裏的人那裏得到了一些建議,這些建議都在他們的Word 2010副本上工作,但不是我的2013年。請您確認這是否是這種情況(即您的代碼適用於單詞的早期版本)? –

+0

Joshua,我今天在一些早晨的見解後更新了代碼))。是的,這是爲Word 2013開發的,但Shape和InlineShape對象也可以在2010版本中使用。 – Evgenii

+0

赫姆,我似乎無法得到這個工作。我已經添加了下面的代碼來獲取輸入到文本框中的數字(按照原始問題): '對於Sub語句後的I = 1至10',在With語句後輸入 '.TextFrame.TextRange = I' , End Sub聲明後面的'Next', 但是我仍然遇到最左上角的文本框是10而不是1的問題。如果這在2013年適用於您,請問我在做什麼錯誤? –

相關問題