Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 09-21-2017, 06:55 PM
sas sas is offline Windows 10 Office 2010 64bit
Novice
 
Join Date: Sep 2017
Posts: 3
sas is on a distinguished road
Default New learner need help - convert tables in word format to ppt slides.

Hello all.



I am new learner in VBA, and writing some VBscript in SAS.

I want to convert tables in word format (.rtf) to ppt slides (.ppt). The following is my code, which could copy the first table in the specific word file, paste into a new ppt file and save under certain folder. I need help on these two questions.

1. How to get the number of tables in the word file?
2. Once I got the number of tables, how to write a loop and copy/paste each table into the ppt file? Each table will be on one slide of ppt file.

Thank you very much!



filename script "C:\Users\zzz\Desktop\New\wdtb2ppt.vbs";

data _null_;
file script;
put 'Set objWord = CreateObject("Word.Application")';
put 'objWord.Visible = False';
/* Slide 1*/
put 'Set objDoc = objWord.Documents.Open("' "C:\Users\zzz\Desktop\New\t_aesum_age_saf.rtf"'")' ;
put 'Set objSelection = objWord.Selection';
put 'Set objPPT = CreateObject("PowerPoint.Application")';
put 'objPPT.Visible = True';
put 'Set objPresentation = objPPT.Presentations.Add';
/* put 'objPresentation.ApplyTemplate("C:\temp\tmp3.pot") ';*/
put 'objDoc.Activate';
put 'objDoc.Tables(1).Range.Select';
put 'objSelection.Copy';
put 'Set objSlide = objPresentation.Slides.Add(1,12)';
put 'objPPT.ActiveWindow.View.Paste';

put 'objPresentation.SaveAs("C:\Users\zzz\Desktop\New\ table.ppt")';
put 'objPresentation.Close';
put 'objPPT.Quit';
put 'objDoc.Close';
put 'objWord.Quit';
run;

filename xx pipe "cscript //nologo ""C:\Users\zzz\Desktop\New\wdtb2ppt.vbs""";
data _null_;
infile xx;
input;
put _infile_;
run;
Reply With Quote
  #2  
Old 09-21-2017, 07:17 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,669
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

The table count can be obtained with:
ActiveDocument.Tables.Count

A loop in VBA might be constructed with code like:
Code:
Sub Demo1()
Dim i As along
With ActiveDocument
  For i = 1 To .Tables.Count
    'process the table
    With .Tables(i)
    'table processing code goes here
    End With
    'or, perhaps:
    .Tables(i).Range.Copy
  Next
End With
End Sub
or:
Code:
Sub Demo2()
Dim Tbl As Table
For Each Tbl In ActiveDocument.Tables
  'process the table
  With Tbl
    'table processing code goes here
  End With
  'or, perhaps:
  Tbl.Range.Copy
Next
End Sub
Note how the second one doesn't need to table count.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 09-21-2017, 07:52 PM
sas sas is offline Windows 10 Office 2010 64bit
Novice
 
Join Date: Sep 2017
Posts: 3
sas is on a distinguished road
Default

Thanks Macropod!

I updated my code as following, but this does not work.

I hightlighted the part I updated per your comments. Is this correct?


filename script "C:\Users\zzz\Desktop\New\wdtb2ppt.vbs";

data _null_;
file script;
put 'Set objWord = CreateObject("Word.Application")';
put 'objWord.Visible = False';
/* Slide 1*/
put 'Set objDoc = objWord.Documents.Open("' "C:\Users\zzz\Desktop\New\t_aesum_age_saf.rtf"'")' ;
put 'Set objSelection = objWord.Selection';
put 'Set objPPT = CreateObject("PowerPoint.Application")';
put 'objPPT.Visible = True';
put 'Set objPresentation = objPPT.Presentations.Add';
/* put 'objPresentation.ApplyTemplate("C:\temp\tmp3.pot") ';*/

put 'Sub Demo2()';
put 'Dim Tbl As Table';
put 'For Each Tbl In ActiveDocument.Tables';
put 'objDoc.Activate';
put 'With Tbl';
put 'objDoc.Tbl.Range.Select';
put 'End With';
put 'Tbl.Range.Copy';
put 'Set objSlide = objPresentation.Slides.Add(1,12)';
put 'objPPT.ActiveWindow.View.Paste';
put 'Next';
put 'End Sub';



put 'objPresentation.SaveAs("C:\Users\zzz\Desktop\New\ table.ppt")';
put 'objPresentation.Close';
put 'objPPT.Quit';
put 'objDoc.Close';
put 'objWord.Quit';
run;

filename xx pipe "cscript //nologo ""C:\Users\zzz\Desktop\New\wdtb2ppt.vbs""";
data _null_;
infile xx;
input;
put _infile_;
run;
Reply With Quote
  #4  
Old 09-21-2017, 07:56 PM
sas sas is offline Windows 10 Office 2010 64bit
Novice
 
Join Date: Sep 2017
Posts: 3
sas is on a distinguished road
Default

Here is my code in my vbs file.

Set objWord = CreateObject("Word.Application")
objWord.Visible = False
Set objDoc = objWord.Documents.Open("C:\Users\zzz\Desktop\New\t _aesum_age_saf.rtf")
Set objSelection = objWord.Selection
Set objPPT = CreateObject("PowerPoint.Application")
objPPT.Visible = True
Set objPresentation = objPPT.Presentations.Add
Sub Demo2()
Dim Tbl As Table
For Each Tbl In ActiveDocument.Tables
objDoc.Activate
With Tbl
objDoc.Tbl.Range.Select
End With
Tbl.Range.Copy
Set objSlide = objPresentation.Slides.Add(1,12)
objPPT.ActiveWindow.View.Paste
Next
End Sub
objPresentation.SaveAs("C:\Users\zzz\Desktop\New\t able.ppt")
objPresentation.Close
objPPT.Quit
objDoc.Close
objWord.Quit
Reply With Quote
  #5  
Old 09-21-2017, 08:07 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,669
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

The macros I posted are stand-alone VBA code demonstrating nothing more than how to loop through tables in VBA; they are not VBScript or SAS. You seem happy to use these terms interchangeably, regardless of the fact they refer to quite different things.

I have no idea about how SAS script works, but I very much doubt you'd insert Word VBA macro names (i.e. put 'Sub Demo2()'; ), variable declarations (i.e. put 'Dim Tbl As Table'; ) or "put 'End Sub';". Furthermore, if you're using "put 'For Each Tbl In ActiveDocument.Tables';", you wouldn't need any of:
put 'objDoc.Activate';
put 'With Tbl';
put 'objDoc.Tbl.Range.Select';
put 'End With';

Frankly, I can't see what SAS has to do with what you're trying to achieve. The principles for looping through tables in VBScript are the same as in VBA, but the code is not all the same; neither can you just dump a VBA macro into a VBScript.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #6  
Old 09-21-2017, 08:33 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,669
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Cross-posted at: http://www.vbaexpress.com/forum/show...-to-ppt-slides
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert Word Tables to PDF tcoggins Word Tables 2 06-29-2016 10:28 AM
Excel Tables in Word, format issues kamal.shah Word Tables 2 01-26-2015 03:47 AM
convert tables to fillable forms in word expert4knowledge Word 1 02-13-2014 03:06 AM
Unable to convert word to MOBI format fig000 Word 1 11-17-2013 01:46 AM
Convert a file from HTML to WORD format weblayout view gtselvam Word 0 12-02-2008 03:53 AM


All times are GMT -7. The time now is 06:45 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft