PDA

View Full Version : slow code



northernstar
09-07-2007, 04:31 AM
could anyone please tell me why the following code is running slow?Sub SaveAsPDFSTANDARD()

Const Path1 As String = "x:\Mechanical\Certificates\In House Certificates\"

Dim strName As String

PromptForName:
strName = InputBox("Please enter job number", "Job Number")

If strName = "" Then
Exit Sub
End If

If UCase(Right(strName, 4)) <> ".DOC" Then
strName = strName & ".doc"
End If

If Len(Dir(Path1 & strName)) > 0 Then
If vbNo = MsgBox("The file already exists - do you want to replace it?", vbQuestion + vbYesNo + vbDefaultButton2, "Existing File") Then
GoTo PromptForName
Else
ActiveDocument.SaveAs FileName:=Path1 & strName
End If
Else
ActiveDocument.SaveAs FileName:=Path1 & strName

End If

ActivePrinter = "PDF995"

Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0

ActivePrinter = "\\DMCBRS01\HP4250 Mech Lab 1"
End Sub[VBA]

thanks

Oorang
09-07-2007, 06:19 AM
The biggest slowdowns seem to be coming from saving the file (the speed of which will depend on the file size, the speed of the drive and if it is a network drive the speed of the network), and changing the printer. The only thing I could really suggest that would net any serious improvement would be to check to see if the activeprinter needs changed before you change it. There are some other changes you could make, such as using constants and using the byte versions of certain functions (the ones with a $) but those kind of gains really don't add up to much in procedures where they are not used repetitivly. See if this makes any difference for you:Option Explicit
Sub DoJob()
Const strUsedPrinter_c As String = "5W"
Const strSaveFolder_c As String = "C:\Test\"
Const lngCancelled_c As Long = 0
Const lngMatch_c As Long = 0
Const lngFileNotFound_c As Long = 0
Const lNoValue_c As Long = 0
Const lngExtLen_c As Long = 4
Const strDoc_c As String = ".doc"
Dim strFileName As String
Dim blnPathEntered As Boolean
Dim strOriginalPrinter As String
Do Until blnPathEntered
strFileName = InputBox("Please enter job number", "Job Number")
If VBA.LenB(strFileName) = lngCancelled_c Then
Exit Sub
End If

If VBA.StrComp(VBA.Right$(strFileName, lngExtLen_c), strDoc_c, _
vbTextCompare) <> _
lngMatch_c Then
strFileName = strFileName & strDoc_c
End If

If VBA.LenB(VBA.Dir(strSaveFolder_c & strFileName)) > lngFileNotFound_c Then
blnPathEntered = Not (vbNo = VBA.MsgBox( _
"The file already exists - do you want to replace it?", _
vbQuestion + vbYesNo + vbDefaultButton2, "Existing File"))
Else
blnPathEntered = True
End If
Loop
ActiveDocument.SaveAs Filename:=strSaveFolder_c & strFileName

If ActivePrinter <> strUsedPrinter_c Then
strOriginalPrinter = ActivePrinter
ActivePrinter = strUsedPrinter_c
End If
Application.PrintOut Filename:=vbNullString, Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentContent, Copies:=lNoValue_c, _
Pages:=vbNullString, PageType:=wdPrintAllPages, _
ManualDuplexPrint:=False, Collate:=True, Background:=True, _
PrintToFile:=False, PrintZoomColumn:=lNoValue_c, _
PrintZoomRow:=lNoValue_c, PrintZoomPaperWidth:=lNoValue_c, _
PrintZoomPaperHeight:=lNoValue_c

ActivePrinter = strOriginalPrinter
End Sub

fumei
09-07-2007, 07:34 AM
1. northernstar - could you please use the VBA tags provided? Thanks.

2. Oorang - could you please use the underscore character in your code? Your post stretches three times the width of my screen. Which means I have to scroll left and right AND up down. This is a pain to read. Thanks.

Oorang
09-07-2007, 07:55 AM
I think that fixes it. (http://accessories.us.dell.com/sna/productdetail.aspx?c=us&l=en&s=dhs&cs=19&sku=222-7175) ;)

fumei
09-07-2007, 10:25 AM
Ummm, are you offering to buy me one?

Post is better. Now it stretches only 1.5 times my screen width.

Oorang
09-07-2007, 10:43 AM
Well maybe this will help (http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/display_change_screen_resolution.mspx?mfr=true) :grinhalo:

fumei
09-07-2007, 11:23 AM
Is that sarcasm, or are you trying to be funny?

I know perfectly well how to adjust my screen resolution. I remember having to use machine language code to adjust screen resolution 35 years ago. That is 10 years before there was anything like a PC.

Perhaps you should have actually read what is at that link.

"Your monitor and video adapter determine how much you can change your screen resolution. You may not be able to increase the resolution above a certain level."

Hmmmmm, maybe that could be an issue for some people? Hmmmm? Perhaps?

However, I will go on the assumption you were actually trying to be funny. Although, it is interesting that you actually bothered to search up a link.

Oorang
09-07-2007, 11:31 AM
Just givin ya a hard time :)
Although I hadn't anticipated you having such strong feelings about screen resolutions and formatting;)

fumei
09-07-2007, 12:07 PM
That is OK. As I said, I was assuming that you were just giving me a hard time.

Which is perfectly fair. Hey, I can deal out a bit hard time on others here, so I had better be able to take it.

Strong feelings on formatting? I am not sure where you are getting that. Although...it is true.

As for screen resolution, frankly, I don't give a fig about it, except where it affects me. Code windows that are so wide that it takes major mouse movement left/right just to read a single line? Yes....I do feel strongly. It is a pain in the butt. Well, the wrist actually.

But nevermind, 'tis better (although I still have to mouse scroll left/right a wee bit).

I guess - and our little conversation here is decidely off-topic to the thread - what does tick me off is people making coding assumptions. Like someone (un-named) here who posted a file with a userform SO large that I could not see any of the commandbuttons. So I could not press OK, or Cancel. So big, that even the Close "x" was not visible to me. He assumed everyone has honking big 72" monitors running at 4 billion x 3 billion pixel resolution.....just kidding. Seriously though, I had to kill Word to get out. Bad dog. Assumptions are the bane of.....well gosh, lots of things.

Oorang
09-07-2007, 12:22 PM
lol Just out of curiousity... What resolution are you running at? If you can believe this (http://www.w3schools.com/browsers/browsers_display.asp), tailoring an app to 1024X768 will get about 80% of your population. (I used to do everything to 800X600 until about 2 years ago.)

(Another interesting thing I noticed was that Mac OS is steadily gaining market share.)

fumei
09-07-2007, 01:22 PM
I am running at 1024 x 768. Your post (thread post #2...which I have to scroll left/right to see the post #) goes off screen by about 15%.


(Another interesting thing I noticed was that Mac OS is steadily gaining market share.)

:rofl:

Yeah, from 1.8% to 4.0%....in four years. Although it seems to passed Linux now.

mdmackillop
09-09-2007, 01:37 AM
I am running at 1024 x 768. Your post (thread post #2...which I have to scroll left/right to see the post #) goes off screen by about 15%.
I'm running the same, and although there is a scrollbar, there is no text actually off the page. Is it a browser thing as well? (Firefox 2.0.0.6)

fumei
09-09-2007, 01:57 PM
Ummmmm, well of course it is a browser thing. That is the whole bloody awful thing about browsers, isn't it?

Now THAT would be a "killer" app. A browser that actually always worked as intended. None of them do IMO.

IE 7

If I switch to Opera, there is a scrollbar as well, but no actual text off screen. In IE.....there is a noticeable chunk off-screen. Much smaller than originally, and acceptable. I just hate having to scroll huge amounts left/right.

Oorang
09-10-2007, 06:09 AM
Hmm maybe that is what is going on... I'm on FF2 myself... But when I hit the thread on IE7 it looks the same to me. Could be a text size thing? (I have medium on both.)

fumei
09-10-2007, 07:33 AM
Probably. And there you go. Bloody browsers. Maybe some day we will have something that will actually work properly for the Web.

As it is, it is a on-going pain trying to explain to people that there web pages "look different" on different browsers. I did a site for someone who complained endlessly that they wanted their pages to "look the same" no matter what browser someone used.

Hmmmm, so would we all.

Oorang
09-10-2007, 08:13 AM
lmao, They just have to not mind you making the entire site one big swf :-)

fumei
09-10-2007, 09:44 AM
No Flash.

No Flash.

No Flash.

I know someone (an artist friend) whose site is only Flash. 100% Flash. Looks great, but....you have to have the latest Flash.

This to me is unacceptable.

However, it depends on who you are targeting as visitors. She does not care if some people will not be able to see her site. The people that will be interested in her art work are most likely serious graphics-type people, who will (most likely) have the latest Flash.

She keeps asking me what I think of her site, and I tell her I can't see it, as I don't have Flash. And I will not. Although I have looked at it on some other machine.

I am from the dinosaur age where accessibility was paramount. EVERYONE should be able to see your pages, no matter what. This is no longer paramount. In fact, it seems like a lot of sites no longer give a crap about accessibility.

We should stop here BTW, as this has nothing to do with this thread. Although I will answer a probable question.

Why do I not have Flash?

I despise being forced to do things. I resent being forced to install software. I resist. Which is also the reason that I go through ALL Windows Services and I shut down/block/lock out any Service I do not actually use. Windows starts up a whack of Services by default. However, if I never use them, why should I have them running?

I never use Instant Messages, so why should I have any OS service running that handles them? On my separate work production machine that is not (and never will be) connected to the Internet, I shut off, and do not allow, any Service that has anything to do with TCP/IP, of DNS etc. etc. etc. Why have them run if they are never actually going to be used?

Wow. This is getting waaaaaay off-topic.

Oorang
09-10-2007, 10:28 AM
rofl Fight the power man.