ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Print File (https://www.excelbanter.com/excel-discussion-misc-queries/54744-print-file.html)

aftamath

Print File
 
How do I use a FileSytemObject in VBA code to print a word document from excel.

Dave Peterson

Print File
 
I'm not sure how you would.

But I saved this from a previous post.

Option Explicit
Sub testme()

'Dim WDApp As Word.Application
'Dim WDDoc As Word.Document
Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String
Dim WordWasRunning As Boolean
Dim testStr As String

myDocName = "C:\my documents\word\document1.doc"

testStr = ""
On Error Resume Next
testStr = Dir(myDocName)
On Error GoTo 0
If testStr = "" Then
MsgBox "Word file not found!"
Exit Sub
End If

WordWasRunning = True
On Error Resume Next
Set WDApp = GetObject(, "Word.Application")
If Err.Number < 0 Then
Set WDApp = CreateObject("Word.Application")
WordWasRunning = False
End If

WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
WDDoc.PrintOut '.printPreview while testing???
WDDoc.Close savechanges:=False

If WordWasRunning Then
'leave it running
Else
WDApp.Quit
End If

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub

aftamath wrote:

How do I use a FileSytemObject in VBA code to print a word document from excel.


--

Dave Peterson

aftamath

Print File
 
Here's my code.
set fs = CreateObject("Scripting.FileSystemObject")

If ComboBox3.Value < "" Then
If fs.FileExists("c:\SymmcoPrograms\" & TextBox1.Value & "\" &
ComboBox1.Value _
& "\" & ComboBox3.Value) = True Then
ActiveWorkbook.FollowHyperlink Address:="c:\SymmcoPrograms\" &
TextBox1.Value _
& "\" & ComboBox1.Value & "\" & ComboBox3.Value, NewWindow:=False
Else
MsgBox "Cannot Find Specified Document"
End If
End If

I want to replace the hyperlink section with a command to print the file in
the FileExists argument.

"Dave Peterson" wrote:

I'm not sure how you would.

But I saved this from a previous post.

Option Explicit
Sub testme()

'Dim WDApp As Word.Application
'Dim WDDoc As Word.Document
Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String
Dim WordWasRunning As Boolean
Dim testStr As String

myDocName = "C:\my documents\word\document1.doc"

testStr = ""
On Error Resume Next
testStr = Dir(myDocName)
On Error GoTo 0
If testStr = "" Then
MsgBox "Word file not found!"
Exit Sub
End If

WordWasRunning = True
On Error Resume Next
Set WDApp = GetObject(, "Word.Application")
If Err.Number < 0 Then
Set WDApp = CreateObject("Word.Application")
WordWasRunning = False
End If

WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
WDDoc.PrintOut '.printPreview while testing???
WDDoc.Close savechanges:=False

If WordWasRunning Then
'leave it running
Else
WDApp.Quit
End If

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub

aftamath wrote:

How do I use a FileSytemObject in VBA code to print a word document from excel.


--

Dave Peterson


Dave Peterson

Print File
 
You can also use the dir() function to test for existence.

myDocName = "C:\my documents\word\document1.doc"

becomes

myDocName = "c:\SymmcoPrograms\" & TextBox1.Value & "\" & ComboBox1.Value _
& "\" & ComboBox3.Value)



aftamath wrote:

Here's my code.
set fs = CreateObject("Scripting.FileSystemObject")

If ComboBox3.Value < "" Then
If fs.FileExists("c:\SymmcoPrograms\" & TextBox1.Value & "\" &
ComboBox1.Value _
& "\" & ComboBox3.Value) = True Then
ActiveWorkbook.FollowHyperlink Address:="c:\SymmcoPrograms\" &
TextBox1.Value _
& "\" & ComboBox1.Value & "\" & ComboBox3.Value, NewWindow:=False
Else
MsgBox "Cannot Find Specified Document"
End If
End If

I want to replace the hyperlink section with a command to print the file in
the FileExists argument.

"Dave Peterson" wrote:

I'm not sure how you would.

But I saved this from a previous post.

Option Explicit
Sub testme()

'Dim WDApp As Word.Application
'Dim WDDoc As Word.Document
Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String
Dim WordWasRunning As Boolean
Dim testStr As String

myDocName = "C:\my documents\word\document1.doc"

testStr = ""
On Error Resume Next
testStr = Dir(myDocName)
On Error GoTo 0
If testStr = "" Then
MsgBox "Word file not found!"
Exit Sub
End If

WordWasRunning = True
On Error Resume Next
Set WDApp = GetObject(, "Word.Application")
If Err.Number < 0 Then
Set WDApp = CreateObject("Word.Application")
WordWasRunning = False
End If

WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
WDDoc.PrintOut '.printPreview while testing???
WDDoc.Close savechanges:=False

If WordWasRunning Then
'leave it running
Else
WDApp.Quit
End If

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub

aftamath wrote:

How do I use a FileSytemObject in VBA code to print a word document from excel.


--

Dave Peterson


--

Dave Peterson

aftamath

Print File
 
When I declare myDocName as the filepath. Then tell it myDocName.Print, an
error pops up for Object Required.

"Dave Peterson" wrote:

You can also use the dir() function to test for existence.

myDocName = "C:\my documents\word\document1.doc"

becomes

myDocName = "c:\SymmcoPrograms\" & TextBox1.Value & "\" & ComboBox1.Value _
& "\" & ComboBox3.Value)



aftamath wrote:

Here's my code.
set fs = CreateObject("Scripting.FileSystemObject")

If ComboBox3.Value < "" Then
If fs.FileExists("c:\SymmcoPrograms\" & TextBox1.Value & "\" &
ComboBox1.Value _
& "\" & ComboBox3.Value) = True Then
ActiveWorkbook.FollowHyperlink Address:="c:\SymmcoPrograms\" &
TextBox1.Value _
& "\" & ComboBox1.Value & "\" & ComboBox3.Value, NewWindow:=False
Else
MsgBox "Cannot Find Specified Document"
End If
End If

I want to replace the hyperlink section with a command to print the file in
the FileExists argument.

"Dave Peterson" wrote:

I'm not sure how you would.

But I saved this from a previous post.

Option Explicit
Sub testme()

'Dim WDApp As Word.Application
'Dim WDDoc As Word.Document
Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String
Dim WordWasRunning As Boolean
Dim testStr As String

myDocName = "C:\my documents\word\document1.doc"

testStr = ""
On Error Resume Next
testStr = Dir(myDocName)
On Error GoTo 0
If testStr = "" Then
MsgBox "Word file not found!"
Exit Sub
End If

WordWasRunning = True
On Error Resume Next
Set WDApp = GetObject(, "Word.Application")
If Err.Number < 0 Then
Set WDApp = CreateObject("Word.Application")
WordWasRunning = False
End If

WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
WDDoc.PrintOut '.printPreview while testing???
WDDoc.Close savechanges:=False

If WordWasRunning Then
'leave it running
Else
WDApp.Quit
End If

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub

aftamath wrote:

How do I use a FileSytemObject in VBA code to print a word document from excel.

--

Dave Peterson


--

Dave Peterson


Dave Peterson

Print File
 
Yep.

But it's printing using this line:
WDDoc.PrintOut

This works because earlier, it used this:
Set WDDoc = WDApp.documents.Open(Filename:=myDocName)

All of the code in that original post was important.


aftamath wrote:

When I declare myDocName as the filepath. Then tell it myDocName.Print, an
error pops up for Object Required.

"Dave Peterson" wrote:

You can also use the dir() function to test for existence.

myDocName = "C:\my documents\word\document1.doc"

becomes

myDocName = "c:\SymmcoPrograms\" & TextBox1.Value & "\" & ComboBox1.Value _
& "\" & ComboBox3.Value)



aftamath wrote:

Here's my code.
set fs = CreateObject("Scripting.FileSystemObject")

If ComboBox3.Value < "" Then
If fs.FileExists("c:\SymmcoPrograms\" & TextBox1.Value & "\" &
ComboBox1.Value _
& "\" & ComboBox3.Value) = True Then
ActiveWorkbook.FollowHyperlink Address:="c:\SymmcoPrograms\" &
TextBox1.Value _
& "\" & ComboBox1.Value & "\" & ComboBox3.Value, NewWindow:=False
Else
MsgBox "Cannot Find Specified Document"
End If
End If

I want to replace the hyperlink section with a command to print the file in
the FileExists argument.

"Dave Peterson" wrote:

I'm not sure how you would.

But I saved this from a previous post.

Option Explicit
Sub testme()

'Dim WDApp As Word.Application
'Dim WDDoc As Word.Document
Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String
Dim WordWasRunning As Boolean
Dim testStr As String

myDocName = "C:\my documents\word\document1.doc"

testStr = ""
On Error Resume Next
testStr = Dir(myDocName)
On Error GoTo 0
If testStr = "" Then
MsgBox "Word file not found!"
Exit Sub
End If

WordWasRunning = True
On Error Resume Next
Set WDApp = GetObject(, "Word.Application")
If Err.Number < 0 Then
Set WDApp = CreateObject("Word.Application")
WordWasRunning = False
End If

WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
WDDoc.PrintOut '.printPreview while testing???
WDDoc.Close savechanges:=False

If WordWasRunning Then
'leave it running
Else
WDApp.Quit
End If

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub

aftamath wrote:

How do I use a FileSytemObject in VBA code to print a word document from excel.

--

Dave Peterson


--

Dave Peterson


--

Dave Peterson

aftamath

Print File
 
Sorry, I didn't realize I said to print a word document in my initial
question. I meant that the code can call on any type of file and print it.
I figured that since VBA can search a computer's file system for a file and
write it to a text file, maybe it can print the file that it finds also. It
could be anything, a PDF, BMP, DOC, XLS, DRG, etc.

"Dave Peterson" wrote:

Yep.

But it's printing using this line:
WDDoc.PrintOut

This works because earlier, it used this:
Set WDDoc = WDApp.documents.Open(Filename:=myDocName)

All of the code in that original post was important.


aftamath wrote:

When I declare myDocName as the filepath. Then tell it myDocName.Print, an
error pops up for Object Required.

"Dave Peterson" wrote:

You can also use the dir() function to test for existence.

myDocName = "C:\my documents\word\document1.doc"

becomes

myDocName = "c:\SymmcoPrograms\" & TextBox1.Value & "\" & ComboBox1.Value _
& "\" & ComboBox3.Value)



aftamath wrote:

Here's my code.
set fs = CreateObject("Scripting.FileSystemObject")

If ComboBox3.Value < "" Then
If fs.FileExists("c:\SymmcoPrograms\" & TextBox1.Value & "\" &
ComboBox1.Value _
& "\" & ComboBox3.Value) = True Then
ActiveWorkbook.FollowHyperlink Address:="c:\SymmcoPrograms\" &
TextBox1.Value _
& "\" & ComboBox1.Value & "\" & ComboBox3.Value, NewWindow:=False
Else
MsgBox "Cannot Find Specified Document"
End If
End If

I want to replace the hyperlink section with a command to print the file in
the FileExists argument.

"Dave Peterson" wrote:

I'm not sure how you would.

But I saved this from a previous post.

Option Explicit
Sub testme()

'Dim WDApp As Word.Application
'Dim WDDoc As Word.Document
Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String
Dim WordWasRunning As Boolean
Dim testStr As String

myDocName = "C:\my documents\word\document1.doc"

testStr = ""
On Error Resume Next
testStr = Dir(myDocName)
On Error GoTo 0
If testStr = "" Then
MsgBox "Word file not found!"
Exit Sub
End If

WordWasRunning = True
On Error Resume Next
Set WDApp = GetObject(, "Word.Application")
If Err.Number < 0 Then
Set WDApp = CreateObject("Word.Application")
WordWasRunning = False
End If

WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
WDDoc.PrintOut '.printPreview while testing???
WDDoc.Close savechanges:=False

If WordWasRunning Then
'leave it running
Else
WDApp.Quit
End If

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub

aftamath wrote:

How do I use a FileSytemObject in VBA code to print a word document from excel.

--

Dave Peterson


--

Dave Peterson


--

Dave Peterson


Dave Peterson

Print File
 
For me, printing means opening the application and either clicking on the print
icon or using a menu system.

And with all the variations of programs, I'm not sure if you could do this with
generic code.

Heck, there are enough programs that can open .bmp's, that I'm not even sure if
the same code would work for that extension.

Maybe someone else has a solution.

aftamath wrote:

Sorry, I didn't realize I said to print a word document in my initial
question. I meant that the code can call on any type of file and print it.
I figured that since VBA can search a computer's file system for a file and
write it to a text file, maybe it can print the file that it finds also. It
could be anything, a PDF, BMP, DOC, XLS, DRG, etc.

"Dave Peterson" wrote:

Yep.

But it's printing using this line:
WDDoc.PrintOut

This works because earlier, it used this:
Set WDDoc = WDApp.documents.Open(Filename:=myDocName)

All of the code in that original post was important.


aftamath wrote:

When I declare myDocName as the filepath. Then tell it myDocName.Print, an
error pops up for Object Required.

"Dave Peterson" wrote:

You can also use the dir() function to test for existence.

myDocName = "C:\my documents\word\document1.doc"

becomes

myDocName = "c:\SymmcoPrograms\" & TextBox1.Value & "\" & ComboBox1.Value _
& "\" & ComboBox3.Value)



aftamath wrote:

Here's my code.
set fs = CreateObject("Scripting.FileSystemObject")

If ComboBox3.Value < "" Then
If fs.FileExists("c:\SymmcoPrograms\" & TextBox1.Value & "\" &
ComboBox1.Value _
& "\" & ComboBox3.Value) = True Then
ActiveWorkbook.FollowHyperlink Address:="c:\SymmcoPrograms\" &
TextBox1.Value _
& "\" & ComboBox1.Value & "\" & ComboBox3.Value, NewWindow:=False
Else
MsgBox "Cannot Find Specified Document"
End If
End If

I want to replace the hyperlink section with a command to print the file in
the FileExists argument.

"Dave Peterson" wrote:

I'm not sure how you would.

But I saved this from a previous post.

Option Explicit
Sub testme()

'Dim WDApp As Word.Application
'Dim WDDoc As Word.Document
Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String
Dim WordWasRunning As Boolean
Dim testStr As String

myDocName = "C:\my documents\word\document1.doc"

testStr = ""
On Error Resume Next
testStr = Dir(myDocName)
On Error GoTo 0
If testStr = "" Then
MsgBox "Word file not found!"
Exit Sub
End If

WordWasRunning = True
On Error Resume Next
Set WDApp = GetObject(, "Word.Application")
If Err.Number < 0 Then
Set WDApp = CreateObject("Word.Application")
WordWasRunning = False
End If

WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
WDDoc.PrintOut '.printPreview while testing???
WDDoc.Close savechanges:=False

If WordWasRunning Then
'leave it running
Else
WDApp.Quit
End If

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub

aftamath wrote:

How do I use a FileSytemObject in VBA code to print a word document from excel.

--

Dave Peterson


--

Dave Peterson


--

Dave Peterson


--

Dave Peterson

aftamath

Print File
 
Thanks Dave, I didn't think it could be done with VBA.

"Dave Peterson" wrote:

For me, printing means opening the application and either clicking on the print
icon or using a menu system.

And with all the variations of programs, I'm not sure if you could do this with
generic code.

Heck, there are enough programs that can open .bmp's, that I'm not even sure if
the same code would work for that extension.

Maybe someone else has a solution.

aftamath wrote:

Sorry, I didn't realize I said to print a word document in my initial
question. I meant that the code can call on any type of file and print it.
I figured that since VBA can search a computer's file system for a file and
write it to a text file, maybe it can print the file that it finds also. It
could be anything, a PDF, BMP, DOC, XLS, DRG, etc.

"Dave Peterson" wrote:

Yep.

But it's printing using this line:
WDDoc.PrintOut

This works because earlier, it used this:
Set WDDoc = WDApp.documents.Open(Filename:=myDocName)

All of the code in that original post was important.


aftamath wrote:

When I declare myDocName as the filepath. Then tell it myDocName.Print, an
error pops up for Object Required.

"Dave Peterson" wrote:

You can also use the dir() function to test for existence.

myDocName = "C:\my documents\word\document1.doc"

becomes

myDocName = "c:\SymmcoPrograms\" & TextBox1.Value & "\" & ComboBox1.Value _
& "\" & ComboBox3.Value)



aftamath wrote:

Here's my code.
set fs = CreateObject("Scripting.FileSystemObject")

If ComboBox3.Value < "" Then
If fs.FileExists("c:\SymmcoPrograms\" & TextBox1.Value & "\" &
ComboBox1.Value _
& "\" & ComboBox3.Value) = True Then
ActiveWorkbook.FollowHyperlink Address:="c:\SymmcoPrograms\" &
TextBox1.Value _
& "\" & ComboBox1.Value & "\" & ComboBox3.Value, NewWindow:=False
Else
MsgBox "Cannot Find Specified Document"
End If
End If

I want to replace the hyperlink section with a command to print the file in
the FileExists argument.

"Dave Peterson" wrote:

I'm not sure how you would.

But I saved this from a previous post.

Option Explicit
Sub testme()

'Dim WDApp As Word.Application
'Dim WDDoc As Word.Document
Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String
Dim WordWasRunning As Boolean
Dim testStr As String

myDocName = "C:\my documents\word\document1.doc"

testStr = ""
On Error Resume Next
testStr = Dir(myDocName)
On Error GoTo 0
If testStr = "" Then
MsgBox "Word file not found!"
Exit Sub
End If

WordWasRunning = True
On Error Resume Next
Set WDApp = GetObject(, "Word.Application")
If Err.Number < 0 Then
Set WDApp = CreateObject("Word.Application")
WordWasRunning = False
End If

WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
WDDoc.PrintOut '.printPreview while testing???
WDDoc.Close savechanges:=False

If WordWasRunning Then
'leave it running
Else
WDApp.Quit
End If

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub

aftamath wrote:

How do I use a FileSytemObject in VBA code to print a word document from excel.

--

Dave Peterson


--

Dave Peterson


--

Dave Peterson


--

Dave Peterson


Dave Peterson

Print File
 
It might be able to be done--just not by me.

Good luck,

aftamath wrote:

Thanks Dave, I didn't think it could be done with VBA.

"Dave Peterson" wrote:

For me, printing means opening the application and either clicking on the print
icon or using a menu system.

And with all the variations of programs, I'm not sure if you could do this with
generic code.

Heck, there are enough programs that can open .bmp's, that I'm not even sure if
the same code would work for that extension.

Maybe someone else has a solution.

aftamath wrote:

Sorry, I didn't realize I said to print a word document in my initial
question. I meant that the code can call on any type of file and print it.
I figured that since VBA can search a computer's file system for a file and
write it to a text file, maybe it can print the file that it finds also. It
could be anything, a PDF, BMP, DOC, XLS, DRG, etc.

"Dave Peterson" wrote:

Yep.

But it's printing using this line:
WDDoc.PrintOut

This works because earlier, it used this:
Set WDDoc = WDApp.documents.Open(Filename:=myDocName)

All of the code in that original post was important.


aftamath wrote:

When I declare myDocName as the filepath. Then tell it myDocName.Print, an
error pops up for Object Required.

"Dave Peterson" wrote:

You can also use the dir() function to test for existence.

myDocName = "C:\my documents\word\document1.doc"

becomes

myDocName = "c:\SymmcoPrograms\" & TextBox1.Value & "\" & ComboBox1.Value _
& "\" & ComboBox3.Value)



aftamath wrote:

Here's my code.
set fs = CreateObject("Scripting.FileSystemObject")

If ComboBox3.Value < "" Then
If fs.FileExists("c:\SymmcoPrograms\" & TextBox1.Value & "\" &
ComboBox1.Value _
& "\" & ComboBox3.Value) = True Then
ActiveWorkbook.FollowHyperlink Address:="c:\SymmcoPrograms\" &
TextBox1.Value _
& "\" & ComboBox1.Value & "\" & ComboBox3.Value, NewWindow:=False
Else
MsgBox "Cannot Find Specified Document"
End If
End If

I want to replace the hyperlink section with a command to print the file in
the FileExists argument.

"Dave Peterson" wrote:

I'm not sure how you would.

But I saved this from a previous post.

Option Explicit
Sub testme()

'Dim WDApp As Word.Application
'Dim WDDoc As Word.Document
Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String
Dim WordWasRunning As Boolean
Dim testStr As String

myDocName = "C:\my documents\word\document1.doc"

testStr = ""
On Error Resume Next
testStr = Dir(myDocName)
On Error GoTo 0
If testStr = "" Then
MsgBox "Word file not found!"
Exit Sub
End If

WordWasRunning = True
On Error Resume Next
Set WDApp = GetObject(, "Word.Application")
If Err.Number < 0 Then
Set WDApp = CreateObject("Word.Application")
WordWasRunning = False
End If

WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
WDDoc.PrintOut '.printPreview while testing???
WDDoc.Close savechanges:=False

If WordWasRunning Then
'leave it running
Else
WDApp.Quit
End If

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub

aftamath wrote:

How do I use a FileSytemObject in VBA code to print a word document from excel.

--

Dave Peterson


--

Dave Peterson


--

Dave Peterson


--

Dave Peterson


--

Dave Peterson


All times are GMT +1. The time now is 07:44 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com