Consulting

Results 1 to 11 of 11

Thread: Macro template in vba

  1. #1

    Macro template in vba

    Hi

    I am doing some reaserch on various online sources but could not find much help.

    Can a vba macro template act as a driver to open a ppt or and xls file?

    If we need to copy and paste from xls to ppt, cab the vba macro template file act as the main driver to open a ppt file and xls file? Can this be done?

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Sure. Just record a macro to see the vba syntax.

  3. #3
    I tried the macro recorder and get the below code. But opening a Powerpoint file is not possible since the vba code is an xls and in .xlsm format. Does this mean that we would need to open the Powerpoint file and then run the macro to auto open the xls file.


    [VBA]Sub Macro1()
    '
    ' Macro1 Macro
    '
    '
    ChDir "F:\Focus\Copy Paste Project\TestExcelToPPT"
    Workbooks("HFIDetails.xls").Connections.AddFromFile
    Workbooks("HFIDetails.xls").Connections.AddFromFile
    Workbooks("HFIDetails.xls").Connections.AddFromFile
    Workbooks("HFIDetails.xls").Connections.AddFromFile
    Workbooks("HFIDetails.xls").Connections.AddFromFile
    Workbooks("HFIDetails.xls").Connections.AddFromFile
    Workbooks("HFIDetails.xls").Connections.AddFromFile
    Workbooks("HFIDetails.xls").Connections.AddFromFile
    Workbooks("HFIDetails13monthYrQtrGrids.xlsm").Connections.AddFromFile
    Workbooks("HFIDetails.xls").Connections.AddFromFile
    Workbooks.Open Filename:= _
    "F:\FocusCopy Paste Project\TestExcelToPPT\HFIDetails.xls"
    End Sub[/VBA]

  4. #4
    And this worked . I was able to open the xls file. So the Powerpoint file needs to open by use manually?

    [VBA]Sub Macro1()
    '
    ' Macro1 Macro
    '
    '
    ChDir "F:\Focus\Copy Paste Project\TestExcelToPPT"

    Workbooks.Open Filename:= _
    "F:\Focus\Copy Paste Project\TestExcelToPPT\HFIDetails.xls"
    End Sub[/VBA]

  5. #5
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    You don't need ChDir().

    For ppt:
    [VBA]Sub Macro1()
    'Requires Tools > References... > Microsoft Office PowerPoint 14.0 Object Library
    Dim pPT As PowerPoint.Application
    Dim pPTopen As PowerPoint.Presentation
    Dim PptName As String
    PptName = "c:\myfiles\ppt\12SQLIII.ppt"
    Set pPT = New PowerPoint.Application
    pPT.Visible = True
    Set pPTopen = pPT.Presentations.Open(PptName)
    pPTopen.SlideShowSettings.Run
    End Sub[/VBA]

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No, it can be opened from VBA

    [vba]

    Set ppt = CreateObject("Powerpoint.Application")[/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Wow, I am talking to the genius. Kudos.

    I am able to open the xls , but I get a compile error for the ppt file and it does not open the ppt file.

    Should I give the Powerpoint file name in
    [VBA]Set pPTopen = pPT.Presentations.Open(PptName)[/VBA]

    as

    [VBA]Set pPTopen = pPT.Presentations.Open("New Orig Package.ppt")[/VBA]

    User_define type not defined.

    [VBA]Public Function open_File(file_name)
    ChDir "F:\Focus\Copy Paste Project\TestExcelToPPT"

    Workbooks.Open Filename:= _
    "F:\Focus\Copy Paste Project\TestExcelToPPT\HFIDetails.xlsm"
    End Function

    Public Function open_ppt_File(file_name)
    'Requires Tools > References... > Microsoft Office PowerPoint 14.0 Object Library
    Dim pPT As PowerPoint.Application
    Dim pPTopen As PowerPoint.Presentation
    Dim PptName As String
    PptName = "F:\Focus\Copy Paste Project\TestExcelToPPT\New Orig Package.ppt"
    'Set pPT = New PowerPoint.Application
    Set pPT = CreateObject("Powerpoint.Application")
    pPT.Visible = True
    Set pPTopen = pPT.Presentations.Open(PptName)
    pPTopen.SlideShowSettings.Run
    End Function[/VBA]

    Regards,
    macroppt123
    Last edited by Bob Phillips; 05-12-2011 at 11:05 AM. Reason: Added VBA tags

  8. #8
    Sorry, my bad. I did not have the library for Powerpoint checked. I just did but now I get a blank ppt App. I expect to see ppt file.

  9. #9
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Leave the last line out of the function if you want to just open the ppt and not run it. Of course you could add an Optional 2nd parameter to the Sub or Function to Run or Open the ppt file.

    You probably have a typo in your filename but you need an adjustment as well. I also added a Dir() to see if the filename passed exists. If you are going to use it as a function, then maybe:[vba]Sub Test_open_ppt_file()
    'run_ppt_File "F:\Focus\Copy Paste Project\TestExcelToPPT\New Orig Package.ppt"
    'run_ppt_File "c:\myfiles\ppt\12SQLIII.ppt"
    MsgBox run_ppt_File("F:\Focus\Copy Paste Project\TestExcelToPPT\New Orig Package.ppt"), , "New Orig Exists?"
    MsgBox run_ppt_File("c:\myfiles\ppt\12SQLIII.ppt"), , "12SQLIII.ppt Exists?"
    End Sub


    Public Function run_ppt_File(PptName As String) As Boolean
    'Requires Tools > References... > Microsoft Office PowerPoint 14.0 Object Library
    Dim pPT As PowerPoint.Application
    Dim pPTopen As PowerPoint.Presentation
    If Dir(PptName) = "" Then Exit Function
    'Set pPT = New PowerPoint.Application
    Set pPT = CreateObject("Powerpoint.Application")
    pPT.Visible = True
    Set pPTopen = pPT.Presentations.Open(PptName)
    pPTopen.SlideShowSettings.Run
    run_ppt_File = True
    End Function
    [/vba]

  10. #10
    I was able to open the ppt file now. I realized that I had to specify teh below in my main module.

    Question: The ppt file opens in slide show. I f we need to open in Normal view within vba code is that possible?

    open_ppt_File ("pPTopen")

  11. #11
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    To run or open:
    [VBA]Sub Test_run_ppt_file()
    'run_ppt_File "e:\City of Tulsa\Superpave Today.pptx" 'Open and play show
    run_ppt_File "e:\City of Tulsa\Superpave Today.pptx", False 'Open only
    End Sub

    Public Function run_ppt_File(PptName As String, Optional tfRun As Boolean = True) As Boolean
    'Requires Tools > References... > Microsoft Office PowerPoint 14.0 Object Library
    Dim pPT As PowerPoint.Application
    Dim pPTopen As PowerPoint.Presentation
    If Dir(PptName) = "" Then Exit Function
    'Set pPT = New PowerPoint.Application
    Set pPT = CreateObject("Powerpoint.Application")
    pPT.Visible = True
    Set pPTopen = pPT.Presentations.Open(PptName)
    If tfRun = False Then Exit Function
    pPTopen.SlideShowSettings.Run
    run_ppt_File = True
    End Function[/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •