2016-11-16 464 views
1

我在Word文檔中有一堆圖像,我正在使用以下腳本來調整大小。有沒有辦法讓我在每張圖片後插入回車符?理想情況下,每張圖片後都需要雙倍間距。我正在使用下面的腳本來調整圖像大小。Word VBA幫助 - 在圖像後插入回車

Sub ResizePhotos() 
    Dim pic As InlineShape 

    For Each pic In ActiveDocument.InlineShapes 
     With pic 
      .LockAspectRatio = msoFalse 
       .Height = InchesToPoints(3.33) 
       .Width = InchesToPoints(4.44) 
     End With 
    Next 
End Sub 

回答

0

一種InlineShape具有表示其在文檔中的位置的Range對象。與這一點,我們的工作可以改善馬丁的回答下列要求:

Dim pic As Word.InlineShape 

For Each pic In ActiveDocument.InlineShapes 
    With pic 
     .LockAspectRatio = msoFalse 
     .Height = InchesToPoints(3.33) 
     .Width = InchesToPoints(4.44) 
     .Range.Style = "Your Style Name" 
    End With 
Next 

或者,如果你真的需要添加額外的段落:

For Each pic In ActiveDocument.InlineShapes 
    With pic 
     .LockAspectRatio = msoFalse 
     .Height = InchesToPoints(3.33) 
     .Width = InchesToPoints(4.44) 
     .Range.InsertAfter Chr(13) 
    End With 
Next 
+0

謝謝大家。 –

0

一個可怕的解決方案,但我想我反正它張貼:

我添加了一個按鈕,我想執行動作的文檔。然後我將下面的宏分配給按鈕:

Private Sub CommandButton1_Click() 

Dim pic As InlineShape 


For Each pic In ActiveDocument.InlineShapes 
    pic.Select 
    Selection.EscapeKey 
    SendKeys "{down}" 
    Selection.Text = Chr(13) 
Next 

End Sub 

這會選擇每個圖像,以便轉義選擇。用按鍵移動到下一行(儘管,我確信這可以通過編程來完成),然後插入一個回車符。

請把這當成一個想法,我敢肯定上面的方法可以以許多方式破解。

0

而不是添加一個或圖片後幾個回車,考慮以下因素:

  1. 文檔中定義樣式,類似於通常用於標題(見下圖)中的那些具有你想要的屬性InlineShapes,或者更精確的段落。例如

    • 中心
    • 與下面的 「段落」
  2. 一個18pt間距保存您的風格yourStyleFormat

  3. 以類似的方式調整你的代碼

    Dim pic As Word.InlineShape 
    
    For Each pic In ActiveDocument.InlineShapes 
        With pic 
         .LockAspectRatio = msoFalse 
         .Height = InchesToPoints(3.33) 
         .Width = InchesToPoints(4.44) 
         .Select 
         Selection.Style = ActiveDocument.Styles("yourStyleFormat") 
        End With 
    Next 
    

優點:

  • 這不空行到您的文件,這是添加沒有沒有
  • ,你可以通過簡單的編輯風格
編輯您的所有圖片的總體佈局

缺點:

  • 使用Select是醜陋的。我用它,因爲我無法弄清楚如何將.Style應用到InlineShape。因此,我首先選擇它作爲段落。我確定有一種更優雅的方式來訪問圖像的父級段落。

enter image description here

0

希望這港島線幫助別人特別

Sub rezize_center_newline() 

Dim i As Long 
Dim shpIn As InlineShape, shp As Shape 

With ActiveDocument 
    For i = 1 To .InlineShapes.Count 
     With .InlineShapes(i) 
      .Height = InchesToPoints(4) 
      .Width = InchesToPoints(5.32) 
      .Range.InsertAfter Chr(13) 
     End With 
    Next i 
    For Each shpIn In ActiveDocument.InlineShapes 
     shpIn.Select 
     Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter 
    Next shpIn 
    For Each shp In ActiveDocument.Shapes 
     shp.Select 
     Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter 
    Next shp 
End With 

End Sub

+0

感謝您使用此代碼段,這可能會提供一些有限的即時幫助。通過展示*爲什麼*這是一個很好的解決方案,並且使它對未來的讀者更有用,一個正確的解釋[將大大提高](// meta.stackexchange.com/q/114762)其長期價值其他類似的問題。請[編輯]你的答案以添加一些解釋,包括你所做的假設。 –