Strange Problem: Opening Excel using VB appends 1

C

CST

I am opening an Excel file via VB. The file has vba code/macros,
which get fired off on Workbook Open. If there is an error while
opening the document, I have code that kills the particular Process ID
for the respective Excel file. I actually use the FindWindow API
function to determine the window handle, in turn killing the
corresponding id. My issue is that occassionally a 1 gets appened to
the file name (e.g., Bad1.xls) and I have no clue why. Is this
because a copy is created in memory somewhere? As you can see, I need
the exact window name to get the corresponding processid. Since I
don't understand why the 1 gets appended, I can't always assume that
it will append a 1.

Private Sub Command1_Click()
Dim oXLS As New Excel.Application
Dim oWK As New Excel.Workbook

oXLS.Visible = True
Set oWK = Workbooks.Open("C:\Temp\Bad.xls")
Set oXLS = Nothing
Set oWK = Nothing
End Sub

This behavior actually occurs when I do XWrk.PrintOut
ActivePrinter:="Acrobat Distiller" too. Is there anyway to avoid
this? Why is this happening?

TIA
Note: I posted this in VB group and am posting it here also
 
T

Tom Ogilvy

Dim oWK As New Excel.Workbook

should just be

Dim oWK As Excel.Workbook

I don't know if it has anything to do with your problem.

Usually, when a new workbook is opened with a specified template, its
default name will be the name of the template with a 1 appended to the name.
So possibly you are opening a template and that and the combination of
specifying New Excel.Workbook may cause that behavior.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top