2017-06-03 111 views
1

我正在嘗試創建一個在Word中移動形狀的VBA宏。 例如,當用戶在Excel中輸入一個值時,userform = 43,然後克服字形中的形狀移動43步 我在Excel中試過這個,我成功地創建了代碼,但是將它移動到了單詞中我沒有找到我嘗試了很多東西,但如果有人可以幫助我這個。 這是我的代碼VBA從Excel到Word移動形狀

Sub lacro1() 
rep_count = 0 
width_variable = 10 
Do 
DoEvents 
rep_count = rep_count + 1 
width_variable = width_variable + 6 
Sheets("Feuil1").Shapes("Connecteur droit 2").Left = 
Sheets("Feuil1").Range("A2").Value 
timeout (0.01) 
Loop Until rep_count = Sheets("Feuil1").Range("A2").Value 

End Sub 
Sub timeout(duration_ms As Double) 
Start_Time = Timer 
Do 
DoEvents 
Loop Until (Timer - Start_Time) >= duration_ms 

End Sub 

我想它的基礎上在Excel

回答

1

值移動位於字形狀的.doc這對我的作品

Sub Sample() 
    Dim oWordApp As Object, oWordDoc As Object 
    Dim shp As Object 

    '~~> Establish an Word application object 
    On Error Resume Next 
    Set oWordApp = GetObject(, "Word.Application") 

    If Err.Number <> 0 Then 
     Set oWordApp = CreateObject("Word.Application") 
    End If 
    Err.Clear 
    On Error GoTo 0 

    oWordApp.Visible = True 

    Set oWordDoc = oWordApp.Documents.Open("C:\Users\Siddharth\Desktop\Sid.Docx") 

    Set shp = oWordDoc.Shapes(1) 

    With shp 
     .Left = 80 
     .Top = 40 
    End With 
End Sub 

截圖 enter image description here

+0

嗨,你再次我的保護每天,我要去嘗試它 –

+0

但H ow可以將它附加到一個特定的形狀,因爲在該文檔中有很多形狀 –

+0

您需要知道形狀的名稱才能使用它:)例如'Set shp = oWordDoc.Shapes(「NameOfShape」)' –