2016-11-18 37 views
0

我試圖改變WMV視頻到MP4在這個宏改變在PowerPoint西元:錯誤使用VBA

Sub mp4_to_wmv() 
    Dim src, ptrn, re, Match, Matches 
    ptr1 = "(\w+)" 

    Create the regular expression. 
    Set re = CreateObject("vbscript.regexp") 
    re.Pattern = ptr1 
    re.IgnoreCase = False 
    re.Global = True 

    For Each pptSlide In ActivePresentation.Slides 
     For Each pptShape In pptSlide.Shapes 
      'If it's a video 
      If pptShape.Type = msoMedia Then 
       Set Matches = re.Execute(pptShape.Name) 
       ' If the video is mp4 then we create the wmv video 
       If Matches(1).Value = "mp4" Then 
        ' We delete the mp4 video 
        pptShape.Delete 
        ' We create the video 
        MyDocument = ActivePresentation.Path & "\" & Matches(0).Value & ".wmv" 
        ' The insertion part is the part giving me trouble, 
        Set Test = pptShape(FileName:=myDocument, Left:=156, Top:=0, Width:=2048, Height:=922) 
        ' Once we have the new video, we have to configure it to start automatically 
        Set oEffect = myDocument.TimeLine.MainSequence.AddEffect(myDocument.Shapes(3), msoAnimEffectMediaPlay, , msoAnimTriggerWithPrevious) 
        ' I'm not sure this part works... 
        With Test.AnimationSettings.PlaySettings 
         .PlayOnEntry = True 
         .LoopUntilStopped = msoCTrue 
        End With 
       End If 
      End If 
     Next 
    Next 
End Sub 

我試圖做的是插入該工作示例進入死循環,但我我不能夠做正確

Set myDocument = ActivePresentation.Slides(1) 

myDocument.Shapes.AddMediaObject FileName:="C:\Windows\clock.avi",Left:=5, Top:=5, Width:=100, Height:=100 

我想我必須在當前文件夾中兩個視頻WMV和MP4。 任何幫助,將不勝感激。

+0

你得到了什麼樣的錯誤(數量和在哪一行)。你是否聲明瞭'myDocument'變量(以哪種方式)? –

+0

最後搞清楚了,我把AddMediaObject問題帶入循環 – Angrod

回答

0
Sub mp4_to_wmv_or_wmv_to_mp4() 
Dim src, ptrn, re, Match, Matches 
ptr1 = "(\w+)" 

' Create the regular expression. 
Set re = CreateObject("vbscript.regexp") 
re.Pattern = ptr1 
re.IgnoreCase = False 
re.Global = True 

For Each pptSlide In ActivePresentation.Slides 
    For Each pptShape In pptSlide.Shapes 
     'If it's a video 
     If pptShape.Type = msoMedia Then 
      Set Matches = re.Execute(pptShape.Name) 
      ' If the video is mp4 then we create the wmv video 
      If Matches(1).Value = "mp4" Then 
       ' We load the video 
       MyDocument = ActivePresentation.Path & "\" & Matches(0).Value & ".wmv" 
      ElseIf Matches(1).Value = "wmv" Then 
       MyDocument = ActivePresentation.Path & "\" & Matches(0).Value & ".mp4" 
      ' The insertion part is the one giving me trouble, I know this works, but I am not able to integrate it within the loop 
       'Set myDocument = ActivePresentation.Slides(1) 
       'myDocument.Shapes.AddMediaObject FileName:="C:\Windows\clock.avi",Left:=5, Top:=5, Width:=100, Height:=100 
      Set test = pptSlide.Shapes.AddMediaObject(FileName:=MyDocument, Left:=156, Top:=0, Width:=2048, Height:=922) 
      ' Once we have the new video, we have to configure it to start automatically 
      'Set oEffect = myDocument.TimeLine.MainSequence.AddEffect(myDocument.Shapes(3), msoAnimEffectMediaPlay, , msoAnimTriggerWithPrevious) 
      ' I'm not sure this part works... 
      With test.AnimationSettings.PlaySettings 
       .PlayOnEntry = msoTrue 
       .LoopUntilStopped = msoCTrue 
       '.fullScreen = True 
      End With 
      End If 
     End If 
    Next 
Next 
End Sub 
0

這將是沿着這些路線的東西:

'Add the shape on the correct slide 
    Set newVideoShape = pptSlide.Shapes.AddMediaObject(FileName:=MyDocument, Left:=156, Top:=0, Width:=2048, Height:=922) 
    'Add the effect 
    Set oEffect = pptSlide.TimeLine.MainSequence.AddEffect(newVideoShape, msoAnimEffectMediaPlay, , msoAnimTriggerWithPrevious) 
    'Make it the first animation 
    oEffect.MoveTo 1 

我會避免使用AnimationSettings對象的任何東西。當一個形狀只能有一個入口動畫時,它是一個傳統的動畫對象(PPT 2000及更早版本)。已知使用它會導致一些意想不到的結果;有時甚至會刪除幻燈片上分配的所有動畫。

+0

感謝提示,有三個問題: 1)我剛剛意識到我的代碼鏈接了視頻,我需要它們嵌入,你知道有什麼變化我必須做什麼? 2)你知道如何添加全屏效果嗎? 謝謝! 3)你知道如何添加LoopUntilStopped效果嗎? – Angrod

+0

對1的回答是使用AddMediaObject2我認爲...我會繼續挖掘全屏和循環效果。 – Angrod