Opening a file in VBA with a period in the filename - vba

I'm trying to run the following code:
Dim WkOrigin As Workbook
Dim Dataname As String
Dataname = "09.22 Test"
Set WkOrigin = Workbooks.Open(Dataname)
I've tried removing the period from the name, and the code runs fine. Does anyone know how to get around issues with periods in workbook names?

I think you just need to add the extension (.xls or .xlsx) on the end and it should be fine.
Dataname = "09.22 Test.xls"
You may be best fully qualifying it too:
Dataname = "C:\09.22 Test.xls"
Or if on a network use the UNC rather than the mapped drive, this way it will work on other's computers without the mapped drive or if it is mapped to a different letter (so long as they have access rights, obviously):
Dataname = "\\ServerName\SharedName\09.22 Test.xls"

Related

Downloading files from imanage/worksite/filesite

I got this problem at work that I have to do a lot with iManage (aka FileSite, DeskSite, WorkSite etc.) and maybe you've heard of it.
Anyways, what I'm trying to do is to write a VBA code which will be able to download a particular file based on its InFin number (7-digit number that is assigned to every file when being uploaded to iManage) and then place the file somewhere; For example, on the Desktop.
I know that iManage does expose an object model and I've already set the reference to IManExtLib.dll
I believe that the command I need is the Copy.Cmd (I don't want to cut sth from WorkSite but only download a copy of the file for the performed task).
Any help would be appreciated.
Assuming you already have a DMS session, you need to get an IManDocument object for your document you're trying to get and then call the GetCopy method. As an example, the following retrieves a physical copy of document number 123456 to a temp folder. Note you'll need to add a reference to IManage.dll as opposed to IManExtLib.dll.
Dim dmsRoot As IManDMS
Dim dmsSession As IManSession
Dim dmsDatabase As IManDatabase
Dim doc As IManDocument
Dim tempDocName As String
Const ServerName As String = "YourDMS"
Const DatabaseName As String = "YourDatabaseName"
Const DocNumToFind = 123456
Const DocVerToFind = 1
tempDocName = "C:\temp\mydoc.doc"
Set dmsRoot = New ManDMS
Set dmsSession = dmsRoot.Sessions.Add(ServerName)
dmsSession.TrustedLogin
Set dmsDatabase = dmsSession.Databases.ItemByName(DatabaseName)
Set doc = dmsDatabase.GetDocument(DocNumToFind, DocVerToFind)
doc.GetCopy tempDocName, imGetCopyOptions.imNativeFormat

Excel 2007 VBA use string variable value in object variable

Good afternoon guys!
I looked around, but I'm not finding anything that addresses my particular issue, so I'll do my best to explain.
Specs:
Excel 2007, VBA, Outlook 2007
Okay, so I've been reworking some scripts that I created to automate our reports in Excel / Avaya CMS. For the past few years I've been the sole person doing our reports, so it wasn't ever necessary to set things up on anyone else's computers. However, things are changing and I'm in the process of updating the scripts / training others to use them. At the moment, when I put my scripts on their computer, I have to go into the VBA code and manually set every reference to their own relative folder paths / outlook folder paths. Painless enough, except when I do any type of changes to the scripts and have to go through the whole process again on each computer.
So this was my solution: Create a config worksheet on the automated reporting workbook, on that config worksheet store the file paths, and in the code simply use variables to reference the config worksheet. This should make it as easy as setting the variables once from the config on a new computer without having to touch any of the lines of code.
Problem: At midnight there is data that is emailed to us from another office. We use Outlook at this office, so I've simply been having it go to the folder specified in the scripts, download the attachments, and then use the downloaded data for the reports. Since everyone sets up their own outlook folders, the paths inside of outlook are different for each user. Since VBA is accessing a worksheet to grab the config information from a cell, it's returning the path for Outlook folders as a string value. However, the outlook folder variable is of Object type, and so it doesn't allow me to use the string variable as it's value, even though the string itself is the actual value the object needs (just not as a string). So is it possible to convert the string value to a value that can be used in the Object variable?
Worksheet Config Cell Value (B5)- The String Value
outNamespace.Folders("Mailbox - Some Guy").Folders("Reports").Folders("ImportantData")
Code:
'Config tab
Dim serverConfig As Worksheet
Set serverConfig = Sheets("CONFIG")
Dim dirOutlookData As String
dirOutlookData = serverConfig.Range("B5").Value
Dim outFolder As Object
Set outFolder = dirOutlookData
Any ideas? Since the string that's returning for dirOutlookData is the value needed for the object variable value, how can I convert the value from the string variable so it can be used in such a way?
Thanks in advance.
Seems too convoluted a solution. Nobody could share their VBA.
In the example provided
outNamespace.Folders("Mailbox - Some Guy").Folders("Reports").Folders("ImportantData"
you could instead use this directly in the VBA
Set mailboxFolder = outNamespace.GetDefaultFolder(olFolderInbox).Parent
You now have the "Mailbox - Some Guy" folder in a generic way not needing to specify "Some Guy".
Set myFolder = mailboxFolder.Folders("Reports").Folders("ImportantData")

Change the path of an Excel file linked to Access 2011 with VBA

I was trying something more fancy and did post on accessforums, where I got got no responses and on programmers access, where I got links to more reading material, but which did not help me - probably due to my lack of VBA expertise.
I have done lots of other programming like PHP and Arduino, but VBA is new for me, although I been watching hours of videos, they don't quite cover what I want to do.
After 4 days of researching an failed attempts, I have simplified my approach and I would appreciate some "real" help with actual code.
Scenario:
I have multiple Excel source file with 9 tabs each.
All the source files are in the same directory, (not in the same directory as the database)
Only one source is ever linked.
Each tab of the source file is a linked table within Access.
Objective:
I wish regularly switch source files.
Method:
I want to replace only the connect file property (i.e. the full file path) for each of the 9 sheets/tabs that use the particular file.
The full path must be "picked up" from my form and applied on an event e.g. on closing of form.
Progress:
I have built a form in which I can enter the file name to use and which calculates the full path to the file in question.
I have a button on the form, which is used to close the form.
Code:
Private Sub Form_Close()
Dim dbs As Database
Dim tdf As TableDef
Dim sfl As String
Dim basePath As String
Dim sName As String
Set dbs = CurrentDb
Set sfl = "SourceData_"
Set sName = "JoeSmith"
Set basePath = "D:\Databases\BOM Consolidator\data_source"
' Loop through all tables in the database.
For Each tdf In dbs.TableDefs
If InStr(10, tdf.Connect, sfl, 1) > 10 Then
tdf.Connect = ";DATABASE=" & basePath & sfl & sName & "\" & dbs
Err = 0
On Error Resume Next
tdf.RefreshLink ' Relink the table.
If Err <> 0 Then
End If
End If
Next tdf End Sub
In the above I am entering the path etc directly just to get it working first.
Access froze :(
Help would be appreciated.
Posting this before I try a restart.
After a restart it is not freezing.
It is saying I have a missing object.
The first line is highlighted in yellow, so I assume something must go in the parenthesis, but no idea what.
If it was a function, I would normally put a variable that is not declared inside the function. This being a subroutine, I was not expecting it to ask for something...
Ultimately I will turn it into a function, sothat I can provide the file name.
A clue to what is needed on the first line please...?
Oh also I am using "Code Builder" - is that the correct option to use with closing a form?

Excel VBA change just the Server name in all data connections in a workbook

Good Afternoon All,
I have been looking at various ways to change/update the data connection strings in a workbook to update the Server name only and keep the other parts of the string as they are.
Basically we have migrated servers and the server name has change and now we have a number of reports that need updating. Some have multiple connections as they connect to different databases.
I found a useful article but this replaces the string with a completely new one for all the data connections (which doesnt work due to the different databases)
excel-macro-to-change-external-data-query-connections-e-g-point-from-one-data
Has anyone had to go through this process and found a way of easily updating the connection string?
Kind Regards
assuming the database names havent changed, but just the server, will the below work for you. Not tested
Sub ChangeAllConnections()
Dim q As QueryTable, w As Worksheet, oldserverName As String, newServerName As String
Dim con As Variant
oldserverName = "onlsrvr" 'Change here
newServerName = "newsrvr" ' change here
For Each w In ThisWorkbook.Worksheets
For Each q In w.QueryTables
q.Connection = Replace(q.Connection, oldserverName, newServerName)
Next
Next
On Error Resume Next
For Each con In ThisWorkbook.Connections
con.ODBCConnection.Connection = Replace(con.ODBCConnection.Connection, oldserverName, newServerName)
con.OLEDBConnection.Connection = Replace(con.OLEDBConnection.Connection, oldserverName, newServerName)
Next
On Error GoTo 0
End Sub
I would consider placing things like this in an sameNameAsWorkbook.ini file. Keep the file in the same location as the Workbook. Then have your code read the ini file and build the connection that way. Then if it needs to change you can change it in one spot very easily.

How to create a dynamic excel link in VB.NET? (not .dll)

VB.NET level: Beginner
I made a .exe using VB.NET. In this program there are many links to excel files (There are specific folders for excel files).
My problem:
Consider an excel file named as abc.xlsx, which is on my home pc. Link to this file is as follows,
D:\work\data\abc.xlsx
now for obvious reasons, this link will not be valid when I run the .exe on my work pc.
(Later I want to run this .exe on multiple pc's)
How to solve this issue?
My thinking is to create a dynamic link which will update itself based on pc in use or to create a constant link which is independent of pc in use.
Help will be really appreciated.
Thanks in advance
so make the path relative to the .exe
C:\myapp\myapp.exe
c:\myapp\data\abc.xslx
...
So no matter where you app is, you can get to your data like this
Dim dataFolder As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly(‌​).Location)
dataFolder = System.IO.Path.Combine(dataFolder,"data")
Dim theFileIwant as String = System.IO.Path.Combine(datafolder,"abc.xslx")
Try something like this:
Public Function GetDynamicFilename(p_filename As String) As String
Dim tempPath As String
Select Case My.Computer.Name.ToUpper
Case "COMPUTER1"
tempPath = "c:\work\data"
Case "COMPUTER2"
tempPath = "d:\work\files"
End Select
Return String.Format("{0}\{1}", tempPath, p_filename)
End Function